Tidy up code

This commit is contained in:
schlagmichdoch 2023-03-28 19:07:33 +02:00
parent 10a0aaf896
commit d0b2c81582
4 changed files with 50 additions and 46 deletions

View file

@ -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<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();
Events.fire('activate-paste-mode', {files: filesReceived, text: ""})
}
}
}

View file

@ -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<files.length; i++) {
fileObjects.push({
name: files[i].name,
buffer: await files[i].arrayBuffer()
});
}
return new Promise((resolve, reject) => {
if (fileObjects?.length > 0) {
return new Promise(async (resolve) => {
if (files && files.length > 0) {
let fileObjects = [];
for (let i=0; i<files.length; i++) {
fileObjects.push({
name: files[i].name,
buffer: await files[i].arrayBuffer()
});
}
const DBOpenRequest = indexedDB.open('pairdrop_store');
DBOpenRequest.onsuccess = (e) => {
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);
}
});
}

View file

@ -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<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();
Events.fire('activate-paste-mode', {files: filesReceived, text: ""})
}
}
}

View file

@ -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<files.length; i++) {
fileObjects.push({
name: files[i].name,
buffer: await files[i].arrayBuffer()
});
}
return new Promise((resolve, reject) => {
if (fileObjects?.length > 0) {
return new Promise(async (resolve) => {
if (files && files.length > 0) {
let fileObjects = [];
for (let i=0; i<files.length; i++) {
fileObjects.push({
name: files[i].name,
buffer: await files[i].arrayBuffer()
});
}
const DBOpenRequest = indexedDB.open('pairdrop_store');
DBOpenRequest.onsuccess = (e) => {
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);
}
});
}