From d36cd3524cace84b2b032ca0fe1bdccb94632625 Mon Sep 17 00:00:00 2001 From: schlagmichdoch Date: Tue, 30 May 2023 01:21:17 +0200 Subject: [PATCH] Fix clearBrowserHistory: url should not always be replaced by "/" as PairDrop might not always be hosted at domain root --- public/scripts/ui.js | 12 ++++--- public/scripts/util.js | 4 +++ public/service-worker.js | 34 +++++++++---------- public_included_ws_fallback/scripts/ui.js | 12 ++++--- public_included_ws_fallback/scripts/util.js | 4 +++ public_included_ws_fallback/service-worker.js | 34 +++++++++---------- 6 files changed, 58 insertions(+), 42 deletions(-) diff --git a/public/scripts/ui.js b/public/scripts/ui.js index bfbca32..cb28677 100644 --- a/public/scripts/ui.js +++ b/public/scripts/ui.js @@ -1024,7 +1024,8 @@ class PairDeviceDialog extends Dialog { const urlParams = new URLSearchParams(window.location.search); if (urlParams.has('room_key')) { this._pairDeviceJoin(urlParams.get('room_key')); - window.history.replaceState({}, "title**", '/'); //remove room_key from url + const url = getUrlWithoutArguments(); + window.history.replaceState({}, "Rewrite URL", url); //remove room_key from url } } @@ -1575,7 +1576,8 @@ class Base64ZipDialog extends Dialog { } clearBrowserHistory() { - window.history.replaceState({}, "Rewrite URL", '/'); + const url = getUrlWithoutArguments(); + window.history.replaceState({}, "Rewrite URL", url); } hide() { @@ -1791,7 +1793,8 @@ class WebShareTargetUI { } } } - window.history.replaceState({}, "Rewrite URL", '/'); + const url = getUrlWithoutArguments(); + window.history.replaceState({}, "Rewrite URL", url); } } } @@ -1815,7 +1818,8 @@ class WebFileHandlersUI { Events.fire('activate-paste-mode', {files: files, text: ""}) launchParams = null; }); - window.history.replaceState({}, "Rewrite URL", '/'); + const url = getUrlWithoutArguments(); + window.history.replaceState({}, "Rewrite URL", url); } } } diff --git a/public/scripts/util.js b/public/scripts/util.js index f6156f8..848dca7 100644 --- a/public/scripts/util.js +++ b/public/scripts/util.js @@ -402,3 +402,7 @@ const cyrb53 = function(str, seed = 0) { function onlyUnique (value, index, array) { return array.indexOf(value) === index; } + +function getUrlWithoutArguments() { + return `${window.location.protocol}//${window.location.host}${window.location.pathname}`; +} diff --git a/public/service-worker.js b/public/service-worker.js index 1927638..238e2e2 100644 --- a/public/service-worker.js +++ b/public/service-worker.js @@ -72,8 +72,7 @@ self.addEventListener('fetch', function(event) { if (event.request.method === "POST") { // Requests related to Web Share Target. event.respondWith((async () => { - let share_url = await evaluateRequestData(event.request); - share_url = event.request.url + share_url; + const share_url = await evaluateRequestData(event.request); return Response.redirect(encodeURI(share_url), 302); })()); } else { @@ -101,15 +100,16 @@ 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("allfiles"); - - +const evaluateRequestData = function (request) { return new Promise(async (resolve) => { + const formData = await request.formData(); + const title = formData.get("title"); + const text = formData.get("text"); + const url = formData.get("url"); + const files = formData.getAll("allfiles"); + + const pairDropUrl = request.url; + if (files && files.length > 0) { let fileObjects = []; for (let i=0; i { - if (i === fileObjects.length - 1) resolve('?share-target=files'); + if (i === fileObjects.length - 1) resolve(pairDropUrl + '?share-target=files'); } } } DBOpenRequest.onerror = _ => { - resolve(''); + resolve(pairDropUrl); } } else { - let share_url = '?share-target=text'; + let urlArgument = '?share-target=text'; - if (title) share_url += `&title=${title}`; - if (text) share_url += `&text=${text}`; - if (url) share_url += `&url=${url}`; + if (title) urlArgument += `&title=${title}`; + if (text) urlArgument += `&text=${text}`; + if (url) urlArgument += `&url=${url}`; - resolve(share_url); + resolve(pairDropUrl + urlArgument); } }); } diff --git a/public_included_ws_fallback/scripts/ui.js b/public_included_ws_fallback/scripts/ui.js index 971fe21..52055ad 100644 --- a/public_included_ws_fallback/scripts/ui.js +++ b/public_included_ws_fallback/scripts/ui.js @@ -1025,7 +1025,8 @@ class PairDeviceDialog extends Dialog { const urlParams = new URLSearchParams(window.location.search); if (urlParams.has('room_key')) { this._pairDeviceJoin(urlParams.get('room_key')); - window.history.replaceState({}, "title**", '/'); //remove room_key from url + const url = getUrlWithoutArguments(); + window.history.replaceState({}, "Rewrite URL", url); //remove room_key from url } } @@ -1576,7 +1577,8 @@ class Base64ZipDialog extends Dialog { } clearBrowserHistory() { - window.history.replaceState({}, "Rewrite URL", '/'); + const url = getUrlWithoutArguments(); + window.history.replaceState({}, "Rewrite URL", url); } hide() { @@ -1792,7 +1794,8 @@ class WebShareTargetUI { } } } - window.history.replaceState({}, "Rewrite URL", '/'); + const url = getUrlWithoutArguments(); + window.history.replaceState({}, "Rewrite URL", url); } } } @@ -1816,7 +1819,8 @@ class WebFileHandlersUI { Events.fire('activate-paste-mode', {files: files, text: ""}) launchParams = null; }); - window.history.replaceState({}, "Rewrite URL", '/'); + const url = getUrlWithoutArguments(); + window.history.replaceState({}, "Rewrite URL", url); } } } diff --git a/public_included_ws_fallback/scripts/util.js b/public_included_ws_fallback/scripts/util.js index 9b7548c..a5266f8 100644 --- a/public_included_ws_fallback/scripts/util.js +++ b/public_included_ws_fallback/scripts/util.js @@ -403,6 +403,10 @@ function onlyUnique (value, index, array) { return array.indexOf(value) === index; } +function getUrlWithoutArguments() { + return `${window.location.protocol}//${window.location.host}${window.location.pathname}`; +} + function arrayBufferToBase64(buffer) { var binary = ''; var bytes = new Uint8Array(buffer); diff --git a/public_included_ws_fallback/service-worker.js b/public_included_ws_fallback/service-worker.js index 347ec70..103a6f1 100644 --- a/public_included_ws_fallback/service-worker.js +++ b/public_included_ws_fallback/service-worker.js @@ -72,8 +72,7 @@ self.addEventListener('fetch', function(event) { if (event.request.method === "POST") { // Requests related to Web Share Target. event.respondWith((async () => { - let share_url = await evaluateRequestData(event.request); - share_url = event.request.url + share_url; + const share_url = await evaluateRequestData(event.request); return Response.redirect(encodeURI(share_url), 302); })()); } else { @@ -101,15 +100,16 @@ 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("allfiles"); - - +const evaluateRequestData = function (request) { return new Promise(async (resolve) => { + const formData = await request.formData(); + const title = formData.get("title"); + const text = formData.get("text"); + const url = formData.get("url"); + const files = formData.getAll("allfiles"); + + const pairDropUrl = request.url; + if (files && files.length > 0) { let fileObjects = []; for (let i=0; i { - if (i === fileObjects.length - 1) resolve('?share-target=files'); + if (i === fileObjects.length - 1) resolve(pairDropUrl + '?share-target=files'); } } } DBOpenRequest.onerror = _ => { - resolve(''); + resolve(pairDropUrl); } } else { - let share_url = '?share-target=text'; + let urlArgument = '?share-target=text'; - if (title) share_url += `&title=${title}`; - if (text) share_url += `&text=${text}`; - if (url) share_url += `&url=${url}`; + if (title) urlArgument += `&title=${title}`; + if (text) urlArgument += `&text=${text}`; + if (url) urlArgument += `&url=${url}`; - resolve(share_url); + resolve(pairDropUrl + urlArgument); } }); }