-
Notifications
You must be signed in to change notification settings - Fork 0
/
xens.js
62 lines (60 loc) · 1.71 KB
/
xens.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
function logError(e) {
console.log('xens: Error: ' + e);
}
chrome.alarms.onAlarm.addListener(() => {
chrome.action.setIcon({ path: 'xens.png' });
});
chrome.action.onClicked.addListener(async (tab) => {
fetch('https://xens.org/n.json', {
method: 'POST',
headers: {
'Content-Type': 'application/json'
},
body: JSON.stringify({ url: tab.url })
}).then((res) => {
if (res.ok) {
return res.json();
} else {
chrome.scripting.executeScript({
target: { tabId: tab.id },
args: [res.url + ' ' + res.statusText],
func: logError
});
return { error: true };
}
}).then((data) => {
if (data.error) {
chrome.action.setIcon({ path: 'xens-error.png' });
chrome.alarms.create({ when: Date.now() + 1000 });
} else {
chrome.scripting.executeScript({
target: { tabId: tab.id },
args: [data],
func: (data) => {
let input = document.createElement('textarea');
input.style.position = 'fixed';
input.style.bottom = 0;
input.style.left = 0;
document.body.appendChild(input);
input.value = data.url;
input.focus();
input.select();
document.execCommand('copy');
input.remove();
}
});
chrome.action.setIcon({ path: 'xens-success.png' });
setTimeout(function() {
chrome.action.setIcon({ path: 'xens.png' });
}, 1000);
}
}).catch(function(err) {
chrome.scripting.executeScript({
target: { tabId: tab.id },
args: [err.message],
func: logError
});
chrome.action.setIcon({ path: 'xens-error.png' });
chrome.alarms.create({ when: Date.now() + 1000 });
});
})