-
Notifications
You must be signed in to change notification settings - Fork 0
/
background.js
65 lines (60 loc) · 1.83 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
/**
* Get the currently active tab
*/
function getTab() {
return browser.tabs.query({ active: true, currentWindow: true });
}
/**
* Get the currently active tab details and the matching cookies
*
* @returns {{tabId:number; url:string; cookies: cookies.Cookie[]}}
*/
function getTabAndCookies() {
return getTab().then(tabs => {
if (!tabs[0]) {
return;
}
return browser.cookies
.getAll({
url: tabs[0].url
})
.then(cookies => {
return { tabId: tabs[0].id, url: tabs[0].url, cookies };
});
});
}
// Register the browser action click listener
browser.browserAction.onClicked.addListener(() => {
getTabAndCookies()
.then(({ tabId, url, cookies }) => {
let command = 'curl ';
const cookieString = cookies.map(c => `${c.name}=${c.value}`).join('; ');
if (cookieString) {
command += `--cookie \\"${cookieString}\\" `;
}
command += url;
browser.tabs
.executeScript(tabId, {
code: `
(function() {
let input = document.createElement('input');
input.setAttribute('type', 'text');
input.value = "${command}";
document.body.appendChild(input);
input.select();
!document.execCommand('copy');
input.remove();
})();
`
})
.then(() => {
browser.browserAction.setBadgeText({ tabId, text: '✓' });
browser.browserAction.setBadgeBackgroundColor({ tabId, color: 'green' });
setTimeout(() => {
browser.browserAction.setBadgeText({ tabId, text: '' });
browser.browserAction.setBadgeBackgroundColor({ tabId, color: '' });
}, 500);
})
.then(() => console.log('copied command', command), error => console.error(error));
});
});