From d0b2c8158286ad50015d59673ebd3ac9baec713d Mon Sep 17 00:00:00 2001 From: schlagmichdoch Date: Tue, 28 Mar 2023 19:07:33 +0200 Subject: [PATCH] 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); } }); }