-
Notifications
You must be signed in to change notification settings - Fork 0
/
background.js
134 lines (116 loc) · 3.42 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
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
chrome.runtime.onInstalled.addListener(() => {
chrome.contextMenus.create({
id: "link-selector",
title: chrome.i18n.getMessage("contextMenuLinkSelectTitle"),
contexts: ["link"]
});
});
chrome.contextMenus.onClicked.addListener(function(info, tab) {
if (info.menuItemId === "link-selector") {
(async () => {
try {
let url = await fetchData(info.linkUrl, -1); // negative mode
if (!/^https?:\/\//i.test(url)) {
url = 'https://' + url;
}
chrome.tabs.create({ url: url });
} catch (error) {
console.error(`Failed to fetch data: ${error}`);
}
})();
}
});
function getPassphrase() {
return new Promise((resolve, reject) => {
chrome.storage.local.get('passphrase', (data) => {
if (chrome.runtime.lastError) {
return reject(chrome.runtime.lastError);
}
resolve(data.passphrase);
});
});
}
function getKeyboardLayout() {
return new Promise((resolve, reject) => {
chrome.storage.local.get('keyboardLayout', (data) => {
if (chrome.runtime.lastError) {
return reject(chrome.runtime.lastError);
}
resolve(data.keyboardLayout);
});
});
}
async function fetchData(tabUrl, mode) {
const apiEndpoint = 'https://downsize.link/ify';
let passphrase = await getPassphrase();
let keyboard = await getKeyboardLayout();
try {
const requestData = {
url: tabUrl,
mode: mode,
keyboard: keyboard,
passphrase: passphrase
};
console.log("Request data: " + JSON.stringify(requestData))
const response = await fetch(apiEndpoint, {
method: 'POST',
headers: {
'Content-Type': 'application/json'
},
body: JSON.stringify(requestData)
});
const data = await response.text();
let short_url = data.substring(1, data.length - 1); // Remove quotes
return short_url;
} catch (error) {
console.error('Error fetching data:', error);
return null;
}
}
chrome.runtime.onMessage.addListener((request, sender, sendResponse) => {
fetchData(request.tabUrl, request.mode)
.then((data) => {
sendResponse({ success: !!data, data });
})
.catch((error) => {
console.error('Error fetching data:', error);
sendResponse({ success: false });
});
return true;
});
chrome.commands.onCommand.addListener(async (command) => {
if (command === 'copy_clipboard') {
// Get the active tab in the current window
chrome.tabs.query({ active: true, currentWindow: true }, async (tabs) => {
const response = await fetchData(tabs[0].url, 1); // tab/mode
// Inject a content script into the active tab and send the response to it
if (response) {
chrome.scripting.executeScript({
target: { tabId: tabs[0].id },
function: copyToClipboard,
args: [response]
});
}
});
}
});
function copyToClipboard(data) {
navigator.clipboard.writeText(data).then(() => {
console.log('Copied to clipboard: ' + data);
}, (err) => {
console.error('Failed to copy text: ', err);
});
}
chrome.runtime.onMessage.addListener((request, sender, sendResponse) => {
if (request.message === 'getPassphrase') {
getPassphrase()
.then(passphrase => {
sendResponse({ passphrase: passphrase });
})
.catch(error => {
sendResponse({ error: error });
});
// Indicate that response is async
return true;
}
});