From 251df2fbff22b5acc767875ad960353c69c67dfd Mon Sep 17 00:00:00 2001 From: schlagmichdoch Date: Thu, 9 Mar 2023 17:03:44 +0100 Subject: [PATCH 1/4] try to fix share target api --- public/scripts/ui.js | 22 ++++-- public/service-worker.js | 72 ++++++++++++------- public_included_ws_fallback/scripts/ui.js | 22 ++++-- public_included_ws_fallback/service-worker.js | 72 ++++++++++++------- 4 files changed, 128 insertions(+), 60 deletions(-) diff --git a/public/scripts/ui.js b/public/scripts/ui.js index fb44352..cc2a91b 100644 --- a/public/scripts/ui.js +++ b/public/scripts/ui.js @@ -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 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") } diff --git a/public/service-worker.js b/public/service-worker.js index d286eb9..e6d1548 100644 --- a/public/service-worker.js +++ b/public/service-worker.js @@ -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 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 { + 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('/'); + } + }); +} diff --git a/public_included_ws_fallback/scripts/ui.js b/public_included_ws_fallback/scripts/ui.js index 8a28a19..53b418b 100644 --- a/public_included_ws_fallback/scripts/ui.js +++ b/public_included_ws_fallback/scripts/ui.js @@ -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 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") } diff --git a/public_included_ws_fallback/service-worker.js b/public_included_ws_fallback/service-worker.js index 25bc379..d8f7ece 100644 --- a/public_included_ws_fallback/service-worker.js +++ b/public_included_ws_fallback/service-worker.js @@ -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 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 { + 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('/'); + } + }); +} From 34ebd603048602b3209ed085270ab66881d72801 Mon Sep 17 00:00:00 2001 From: Daniel Pham Date: Mon, 27 Mar 2023 21:49:33 +0200 Subject: [PATCH 2/4] Update service worker - files array now matches manifest files name - fixed handling fetch redirect --- public/service-worker.js | 12 ++++++------ public_included_ws_fallback/service-worker.js | 12 ++++++------ 2 files changed, 12 insertions(+), 12 deletions(-) diff --git a/public/service-worker.js b/public/service-worker.js index e6d1548..f49cc5f 100644 --- a/public/service-worker.js +++ b/public/service-worker.js @@ -71,12 +71,12 @@ const update = request => self.addEventListener('fetch', function(event) { if (event.request.method === "POST") { // Requests related to Web Share Target. - evaluateRequestData(event.request).then(share_url => { + event.respondWith((async () => { + let share_url = await evaluateRequestData(event.request); + share_url = event.request.url + share_url.substring(1); console.debug(share_url); - event.respondWith( - Response.redirect(encodeURI(share_url), 302) - ); - }) + return Response.redirect(encodeURI(share_url), 302); + })()); } else { // Regular requests not related to Web Share Target. event.respondWith( @@ -107,7 +107,7 @@ const evaluateRequestData = async function (request) { const title = formData.get("title"); const text = formData.get("text"); const url = formData.get("url"); - const files = formData.getAll("files"); + const files = formData.getAll("allfiles"); console.debug(files) let fileObjects = []; for (let i=0; i self.addEventListener('fetch', function(event) { if (event.request.method === "POST") { // Requests related to Web Share Target. - evaluateRequestData(event.request).then(share_url => { + event.respondWith((async () => { + let share_url = await evaluateRequestData(event.request); + share_url = event.request.url + share_url.substring(1); console.debug(share_url); - event.respondWith( - Response.redirect(encodeURI(share_url), 302) - ); - }) + return Response.redirect(encodeURI(share_url), 302); + })()); } else { // Regular requests not related to Web Share Target. event.respondWith( @@ -107,7 +107,7 @@ const evaluateRequestData = async function (request) { const title = formData.get("title"); const text = formData.get("text"); const url = formData.get("url"); - const files = formData.getAll("files"); + const files = formData.getAll("allfiles"); console.debug(files) let fileObjects = []; for (let i=0; i Date: Tue, 28 Mar 2023 00:42:30 +0200 Subject: [PATCH 3/4] Fix passed arguments for sharing text --- public/service-worker.js | 2 +- public_included_ws_fallback/service-worker.js | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/public/service-worker.js b/public/service-worker.js index f49cc5f..7e43687 100644 --- a/public/service-worker.js +++ b/public/service-worker.js @@ -137,7 +137,7 @@ const evaluateRequestData = async function (request) { } } 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}`); + resolve(`/?share-target=text${title ? `&title=${title}` : ''}${text ? `&text=${text}` : ''}${url ? `&url=${url}` : ''}`); } else { resolve('/'); } diff --git a/public_included_ws_fallback/service-worker.js b/public_included_ws_fallback/service-worker.js index acb7225..6e815eb 100644 --- a/public_included_ws_fallback/service-worker.js +++ b/public_included_ws_fallback/service-worker.js @@ -137,7 +137,7 @@ const evaluateRequestData = async function (request) { } } 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}`); + resolve(`/?share-target=text${title ? `&title=${title}` : ''}${text ? `&text=${text}` : ''}${url ? `&url=${url}` : ''}`); } else { resolve('/'); } From d0b2c8158286ad50015d59673ebd3ac9baec713d Mon Sep 17 00:00:00 2001 From: schlagmichdoch Date: Tue, 28 Mar 2023 19:07:33 +0200 Subject: [PATCH 4/4] Tidy up code --- public/scripts/ui.js | 7 ++-- public/service-worker.js | 41 ++++++++++--------- public_included_ws_fallback/scripts/ui.js | 7 ++-- public_included_ws_fallback/service-worker.js | 41 ++++++++++--------- 4 files changed, 50 insertions(+), 46 deletions(-) diff --git a/public/scripts/ui.js b/public/scripts/ui.js index cc2a91b..94542bb 100644 --- a/public/scripts/ui.js +++ b/public/scripts/ui.js @@ -1607,14 +1607,13 @@ class WebShareTargetUI { let shareTargetText; if (url) { - shareTargetText = url; // We share only the Link - no text. Because link-only text becomes clickable. + shareTargetText = url; // we share only the link - no text. } else if (title && text) { shareTargetText = title + '\r\n' + text; } else { shareTargetText = title + text; } - console.log('Shared Target Text:', '"' + shareTargetText + '"'); Events.fire('activate-paste-mode', {files: [], text: shareTargetText}) } else if (share_target_type === "files") { let openRequest = window.indexedDB.open('pairdrop_store') @@ -1629,10 +1628,10 @@ class WebShareTargetUI { for (let i=0; i db.close(); + + Events.fire('activate-paste-mode', {files: filesReceived, text: ""}) } } } diff --git a/public/service-worker.js b/public/service-worker.js index 7e43687..24c8d08 100644 --- a/public/service-worker.js +++ b/public/service-worker.js @@ -73,8 +73,7 @@ self.addEventListener('fetch', function(event) { // Requests related to Web Share Target. event.respondWith((async () => { let share_url = await evaluateRequestData(event.request); - share_url = event.request.url + share_url.substring(1); - console.debug(share_url); + share_url = event.request.url + share_url; return Response.redirect(encodeURI(share_url), 302); })()); } else { @@ -108,19 +107,20 @@ const evaluateRequestData = async function (request) { const text = formData.get("text"); const url = formData.get("url"); const files = formData.getAll("allfiles"); - console.debug(files) - let fileObjects = []; - for (let i=0; i { - if (fileObjects?.length > 0) { + + return new Promise(async (resolve) => { + if (files && files.length > 0) { + let fileObjects = []; + for (let i=0; i { + DBOpenRequest.onsuccess = e => { const db = e.target.result; for (let i = 0; i < fileObjects.length; i++) { const transaction = db.transaction('share_target_files', 'readwrite'); @@ -128,18 +128,21 @@ const evaluateRequestData = async function (request) { const objectStoreRequest = objectStore.add(fileObjects[i]); objectStoreRequest.onsuccess = _ => { - if (i === fileObjects.length - 1) resolve('/?share-target=files'); + if (i === fileObjects.length - 1) resolve('?share-target=files'); } } } DBOpenRequest.onerror = _ => { - resolve('/'); + resolve(''); } - } else if (title?.length > 0 || text?.length > 0 || url?.length > 0) { - console.debug(title || text || url); - resolve(`/?share-target=text${title ? `&title=${title}` : ''}${text ? `&text=${text}` : ''}${url ? `&url=${url}` : ''}`); } else { - resolve('/'); + let share_url = '?share-target=text'; + + if (title) share_url += `&title=${title}`; + if (text) share_url += `&text=${text}`; + if (url) share_url += `&url=${url}`; + + resolve(share_url); } }); } diff --git a/public_included_ws_fallback/scripts/ui.js b/public_included_ws_fallback/scripts/ui.js index 53b418b..9c72d12 100644 --- a/public_included_ws_fallback/scripts/ui.js +++ b/public_included_ws_fallback/scripts/ui.js @@ -1608,14 +1608,13 @@ class WebShareTargetUI { let shareTargetText; if (url) { - shareTargetText = url; // We share only the Link - no text. Because link-only text becomes clickable. + shareTargetText = url; // we share only the link - no text. } else if (title && text) { shareTargetText = title + '\r\n' + text; } else { shareTargetText = title + text; } - console.log('Shared Target Text:', '"' + shareTargetText + '"'); Events.fire('activate-paste-mode', {files: [], text: shareTargetText}) } else if (share_target_type === "files") { let openRequest = window.indexedDB.open('pairdrop_store') @@ -1630,10 +1629,10 @@ class WebShareTargetUI { for (let i=0; i db.close(); + + Events.fire('activate-paste-mode', {files: filesReceived, text: ""}) } } } diff --git a/public_included_ws_fallback/service-worker.js b/public_included_ws_fallback/service-worker.js index 6e815eb..ea0886d 100644 --- a/public_included_ws_fallback/service-worker.js +++ b/public_included_ws_fallback/service-worker.js @@ -73,8 +73,7 @@ self.addEventListener('fetch', function(event) { // Requests related to Web Share Target. event.respondWith((async () => { let share_url = await evaluateRequestData(event.request); - share_url = event.request.url + share_url.substring(1); - console.debug(share_url); + share_url = event.request.url + share_url; return Response.redirect(encodeURI(share_url), 302); })()); } else { @@ -108,19 +107,20 @@ const evaluateRequestData = async function (request) { const text = formData.get("text"); const url = formData.get("url"); const files = formData.getAll("allfiles"); - console.debug(files) - let fileObjects = []; - for (let i=0; i { - if (fileObjects?.length > 0) { + + return new Promise(async (resolve) => { + if (files && files.length > 0) { + let fileObjects = []; + for (let i=0; i { + DBOpenRequest.onsuccess = e => { const db = e.target.result; for (let i = 0; i < fileObjects.length; i++) { const transaction = db.transaction('share_target_files', 'readwrite'); @@ -128,18 +128,21 @@ const evaluateRequestData = async function (request) { const objectStoreRequest = objectStore.add(fileObjects[i]); objectStoreRequest.onsuccess = _ => { - if (i === fileObjects.length - 1) resolve('/?share-target=files'); + if (i === fileObjects.length - 1) resolve('?share-target=files'); } } } DBOpenRequest.onerror = _ => { - resolve('/'); + resolve(''); } - } else if (title?.length > 0 || text?.length > 0 || url?.length > 0) { - console.debug(title || text || url); - resolve(`/?share-target=text${title ? `&title=${title}` : ''}${text ? `&text=${text}` : ''}${url ? `&url=${url}` : ''}`); } else { - resolve('/'); + let share_url = '?share-target=text'; + + if (title) share_url += `&title=${title}`; + if (text) share_url += `&text=${text}`; + if (url) share_url += `&url=${url}`; + + resolve(share_url); } }); }