try to fix share target api
This commit is contained in:
parent
1bb8a63eed
commit
251df2fbff
4 changed files with 128 additions and 60 deletions
|
@ -1617,17 +1617,24 @@ class WebShareTargetUI {
|
|||
console.log('Shared Target Text:', '"' + shareTargetText + '"');
|
||||
Events.fire('activate-paste-mode', {files: [], text: shareTargetText})
|
||||
} else if (share_target_type === "files") {
|
||||
const openRequest = window.indexedDB.open('pairdrop_store')
|
||||
openRequest.onsuccess( db => {
|
||||
let openRequest = window.indexedDB.open('pairdrop_store')
|
||||
openRequest.onsuccess = e => {
|
||||
const db = e.target.result;
|
||||
const tx = db.transaction('share_target_files', 'readwrite');
|
||||
const store = tx.objectStore('share_target_files');
|
||||
const request = store.getAll();
|
||||
request.onsuccess = _ => {
|
||||
Events.fire('activate-paste-mode', {files: request.result, text: ""})
|
||||
const fileObjects = request.result;
|
||||
let filesReceived = [];
|
||||
for (let i=0; i<fileObjects.length; i++) {
|
||||
filesReceived.push(new File([fileObjects[i].buffer], fileObjects[i].name));
|
||||
}
|
||||
console.debug(filesReceived)
|
||||
Events.fire('activate-paste-mode', {files: filesReceived, text: ""})
|
||||
const clearRequest = store.clear()
|
||||
clearRequest.onsuccess = _ => db.close();
|
||||
}
|
||||
})
|
||||
}
|
||||
}
|
||||
window.history.replaceState({}, "Rewrite URL", '/');
|
||||
}
|
||||
|
@ -1684,7 +1691,7 @@ class PersistentStorage {
|
|||
PersistentStorage.logBrowserNotCapable();
|
||||
return;
|
||||
}
|
||||
const DBOpenRequest = window.indexedDB.open('pairdrop_store', 2);
|
||||
const DBOpenRequest = window.indexedDB.open('pairdrop_store', 3);
|
||||
DBOpenRequest.onerror = (e) => {
|
||||
PersistentStorage.logBrowserNotCapable();
|
||||
console.log('Error initializing database: ');
|
||||
|
@ -1710,7 +1717,10 @@ class PersistentStorage {
|
|||
}
|
||||
|
||||
try {
|
||||
db.createObjectStore('share_target_files');
|
||||
if (db.objectStoreNames.contains('share_target_files')) {
|
||||
db.deleteObjectStore('share_target_files');
|
||||
}
|
||||
db.createObjectStore('share_target_files', {autoIncrement: true});
|
||||
} catch (error) {
|
||||
console.log("Object store named 'share_target_files' already exists")
|
||||
}
|
||||
|
|
|
@ -71,30 +71,12 @@ const update = request =>
|
|||
self.addEventListener('fetch', function(event) {
|
||||
if (event.request.method === "POST") {
|
||||
// Requests related to Web Share Target.
|
||||
event.respondWith(
|
||||
(async () => {
|
||||
const formData = await event.request.formData();
|
||||
const title = formData.get("title");
|
||||
const text = formData.get("text");
|
||||
const url = formData.get("url");
|
||||
const files = formData.get("files");
|
||||
let share_url = "/";
|
||||
if (files.length > 0) {
|
||||
share_url = "/?share-target=files";
|
||||
const db = await window.indexedDB.open('pairdrop_store');
|
||||
const tx = db.transaction('share_target_files', 'readwrite');
|
||||
const store = tx.objectStore('share_target_files');
|
||||
for (let i=0; i<files.length; i++) {
|
||||
await store.add(files[i]);
|
||||
}
|
||||
await tx.complete
|
||||
db.close()
|
||||
} else if (title.length > 0 || text.length > 0 || url.length) {
|
||||
share_url = `/?share-target=text&title=${title}&text=${text}&url=${url}`;
|
||||
}
|
||||
return Response.redirect(encodeURI(share_url), 303);
|
||||
})()
|
||||
);
|
||||
evaluateRequestData(event.request).then(share_url => {
|
||||
console.debug(share_url);
|
||||
event.respondWith(
|
||||
Response.redirect(encodeURI(share_url), 302)
|
||||
);
|
||||
})
|
||||
} else {
|
||||
// Regular requests not related to Web Share Target.
|
||||
event.respondWith(
|
||||
|
@ -119,3 +101,45 @@ self.addEventListener('activate', evt =>
|
|||
})
|
||||
)
|
||||
);
|
||||
|
||||
const evaluateRequestData = async function (request) {
|
||||
const formData = await request.formData();
|
||||
const title = formData.get("title");
|
||||
const text = formData.get("text");
|
||||
const url = formData.get("url");
|
||||
const files = formData.getAll("files");
|
||||
console.debug(files)
|
||||
let fileObjects = [];
|
||||
for (let i=0; i<files.length; i++) {
|
||||
fileObjects.push({
|
||||
name: files[i].name,
|
||||
buffer: await files[i].arrayBuffer()
|
||||
});
|
||||
}
|
||||
|
||||
return new Promise((resolve, reject) => {
|
||||
if (fileObjects?.length > 0) {
|
||||
const DBOpenRequest = indexedDB.open('pairdrop_store');
|
||||
DBOpenRequest.onsuccess = (e) => {
|
||||
const db = e.target.result;
|
||||
for (let i = 0; i < fileObjects.length; i++) {
|
||||
const transaction = db.transaction('share_target_files', 'readwrite');
|
||||
const objectStore = transaction.objectStore('share_target_files');
|
||||
|
||||
const objectStoreRequest = objectStore.add(fileObjects[i]);
|
||||
objectStoreRequest.onsuccess = _ => {
|
||||
if (i === fileObjects.length - 1) resolve('/?share-target=files');
|
||||
}
|
||||
}
|
||||
}
|
||||
DBOpenRequest.onerror = _ => {
|
||||
resolve('/');
|
||||
}
|
||||
} else if (title?.length > 0 || text?.length > 0 || url?.length > 0) {
|
||||
console.debug(title || text || url);
|
||||
resolve(`/?share-target=text&title=${title}&text=${text}&url=${url}`);
|
||||
} else {
|
||||
resolve('/');
|
||||
}
|
||||
});
|
||||
}
|
||||
|
|
|
@ -1618,17 +1618,24 @@ class WebShareTargetUI {
|
|||
console.log('Shared Target Text:', '"' + shareTargetText + '"');
|
||||
Events.fire('activate-paste-mode', {files: [], text: shareTargetText})
|
||||
} else if (share_target_type === "files") {
|
||||
const openRequest = window.indexedDB.open('pairdrop_store')
|
||||
openRequest.onsuccess( db => {
|
||||
let openRequest = window.indexedDB.open('pairdrop_store')
|
||||
openRequest.onsuccess = e => {
|
||||
const db = e.target.result;
|
||||
const tx = db.transaction('share_target_files', 'readwrite');
|
||||
const store = tx.objectStore('share_target_files');
|
||||
const request = store.getAll();
|
||||
request.onsuccess = _ => {
|
||||
Events.fire('activate-paste-mode', {files: request.result, text: ""})
|
||||
const fileObjects = request.result;
|
||||
let filesReceived = [];
|
||||
for (let i=0; i<fileObjects.length; i++) {
|
||||
filesReceived.push(new File([fileObjects[i].buffer], fileObjects[i].name));
|
||||
}
|
||||
console.debug(filesReceived)
|
||||
Events.fire('activate-paste-mode', {files: filesReceived, text: ""})
|
||||
const clearRequest = store.clear()
|
||||
clearRequest.onsuccess = _ => db.close();
|
||||
}
|
||||
})
|
||||
}
|
||||
}
|
||||
window.history.replaceState({}, "Rewrite URL", '/');
|
||||
}
|
||||
|
@ -1685,7 +1692,7 @@ class PersistentStorage {
|
|||
PersistentStorage.logBrowserNotCapable();
|
||||
return;
|
||||
}
|
||||
const DBOpenRequest = window.indexedDB.open('pairdrop_store', 2);
|
||||
const DBOpenRequest = window.indexedDB.open('pairdrop_store', 3);
|
||||
DBOpenRequest.onerror = (e) => {
|
||||
PersistentStorage.logBrowserNotCapable();
|
||||
console.log('Error initializing database: ');
|
||||
|
@ -1711,7 +1718,10 @@ class PersistentStorage {
|
|||
}
|
||||
|
||||
try {
|
||||
db.createObjectStore('share_target_files');
|
||||
if (db.objectStoreNames.contains('share_target_files')) {
|
||||
db.deleteObjectStore('share_target_files');
|
||||
}
|
||||
db.createObjectStore('share_target_files', {autoIncrement: true});
|
||||
} catch (error) {
|
||||
console.log("Object store named 'share_target_files' already exists")
|
||||
}
|
||||
|
|
|
@ -71,30 +71,12 @@ const update = request =>
|
|||
self.addEventListener('fetch', function(event) {
|
||||
if (event.request.method === "POST") {
|
||||
// Requests related to Web Share Target.
|
||||
event.respondWith(
|
||||
(async () => {
|
||||
const formData = await event.request.formData();
|
||||
const title = formData.get("title");
|
||||
const text = formData.get("text");
|
||||
const url = formData.get("url");
|
||||
const files = formData.get("files");
|
||||
let share_url = "/";
|
||||
if (files.length > 0) {
|
||||
share_url = "/?share-target=files";
|
||||
const db = await window.indexedDB.open('pairdrop_store');
|
||||
const tx = db.transaction('share_target_files', 'readwrite');
|
||||
const store = tx.objectStore('share_target_files');
|
||||
for (let i=0; i<files.length; i++) {
|
||||
await store.add(files[i]);
|
||||
}
|
||||
await tx.complete
|
||||
db.close()
|
||||
} else if (title.length > 0 || text.length > 0 || url.length) {
|
||||
share_url = `/?share-target=text&title=${title}&text=${text}&url=${url}`;
|
||||
}
|
||||
return Response.redirect(encodeURI(share_url), 303);
|
||||
})()
|
||||
);
|
||||
evaluateRequestData(event.request).then(share_url => {
|
||||
console.debug(share_url);
|
||||
event.respondWith(
|
||||
Response.redirect(encodeURI(share_url), 302)
|
||||
);
|
||||
})
|
||||
} else {
|
||||
// Regular requests not related to Web Share Target.
|
||||
event.respondWith(
|
||||
|
@ -119,3 +101,45 @@ self.addEventListener('activate', evt =>
|
|||
})
|
||||
)
|
||||
);
|
||||
|
||||
const evaluateRequestData = async function (request) {
|
||||
const formData = await request.formData();
|
||||
const title = formData.get("title");
|
||||
const text = formData.get("text");
|
||||
const url = formData.get("url");
|
||||
const files = formData.getAll("files");
|
||||
console.debug(files)
|
||||
let fileObjects = [];
|
||||
for (let i=0; i<files.length; i++) {
|
||||
fileObjects.push({
|
||||
name: files[i].name,
|
||||
buffer: await files[i].arrayBuffer()
|
||||
});
|
||||
}
|
||||
|
||||
return new Promise((resolve, reject) => {
|
||||
if (fileObjects?.length > 0) {
|
||||
const DBOpenRequest = indexedDB.open('pairdrop_store');
|
||||
DBOpenRequest.onsuccess = (e) => {
|
||||
const db = e.target.result;
|
||||
for (let i = 0; i < fileObjects.length; i++) {
|
||||
const transaction = db.transaction('share_target_files', 'readwrite');
|
||||
const objectStore = transaction.objectStore('share_target_files');
|
||||
|
||||
const objectStoreRequest = objectStore.add(fileObjects[i]);
|
||||
objectStoreRequest.onsuccess = _ => {
|
||||
if (i === fileObjects.length - 1) resolve('/?share-target=files');
|
||||
}
|
||||
}
|
||||
}
|
||||
DBOpenRequest.onerror = _ => {
|
||||
resolve('/');
|
||||
}
|
||||
} else if (title?.length > 0 || text?.length > 0 || url?.length > 0) {
|
||||
console.debug(title || text || url);
|
||||
resolve(`/?share-target=text&title=${title}&text=${text}&url=${url}`);
|
||||
} else {
|
||||
resolve('/');
|
||||
}
|
||||
});
|
||||
}
|
||||
|
|
Loading…
Reference in a new issue