-
Notifications
You must be signed in to change notification settings - Fork 0
/
background.js
93 lines (77 loc) · 3.68 KB
/
background.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
// Ensure the context menu is created when the extension is installed or reloaded
browser.runtime.onInstalled.addListener(() => {
createContextMenu();
});
function createContextMenu() {
browser.contextMenus.create({
id: "uploadToZipline",
title: "Upload to Zipline",
contexts: ["image", "video", "audio"]
});
}
// Ensure context menu is available when the background script starts running
browser.runtime.onStartup.addListener(() => {
createContextMenu();
});
function showNotification(title, message) {
browser.notifications.create({
"type": "basic",
"iconUrl": browser.runtime.getURL("icon48.png"),
"title": title,
"message": message
});
}
browser.contextMenus.onClicked.addListener(async (info, tab) => {
if (info.menuItemId === "uploadToZipline") {
const mediaURL = info.srcUrl;
if (mediaURL) {
try {
// Fetch user settings
const { requestURL, authToken } = await browser.storage.sync.get(['requestURL', 'authToken']);
if (!requestURL || !authToken) {
showNotification("Configuration Required", "Please configure your Zipline settings in the extension options.");
browser.runtime.openOptionsPage();
return;
}
// Fetch the media directly
const blob = await fetch(mediaURL).then(response => response.blob());
let filename = mediaURL.split("/").pop().split("?")[0]; // Get the filename without query params
// Remove invisible characters from the filename
filename = filename.replace(/[\u200B-\u200D\uFEFF]/g, ''); // Remove zero-width and other invisible characters
const formData = new FormData();
formData.append('file', blob, filename);
// Upload the media to Zipline
const response = await fetch(requestURL, {
method: "POST",
headers: {
"Authorization": authToken,
"Zws": "true",
"Format": "name" // Ensures the original filename is kept
},
body: formData
});
if (response.ok) {
const result = await response.json();
let baseURL = result.files[0]; // Assuming result.files[0] is the base URL
// Ensure the baseURL has the correct protocol and is properly sanitized
if (requestURL.startsWith('https')) {
baseURL = baseURL.replace(/^http:/, 'https:'); // Replace http with https if needed
}
// Remove trailing slashes from baseURL
baseURL = baseURL.replace(/\/+$/, '');
// Construct the final URL
const finalURL = `${baseURL}${filename}`.replace(/[\u200B-\u200D\uFEFF\u2060]/g, '');
// Copy the URL to the clipboard
await navigator.clipboard.writeText(finalURL);
showNotification("Upload Successful", "The file was uploaded successfully and the URL has been copied to your clipboard.");
console.log("Upload successful! File URL: " + finalURL);
} else {
throw new Error("Upload failed");
}
} catch (error) {
console.error("Upload failed:", error);
showNotification("Upload Failed", "An error occurred while uploading the file. Please check your settings and try again.");
}
}
}
});