-
Notifications
You must be signed in to change notification settings - Fork 54
/
background.js
113 lines (107 loc) · 3.08 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
import {
getProtocol,
saveProtocol,
getModifyPages,
saveModifyPages
} from './api/storage.js';
import {createExtensionMenu} from './api/menu.js';
const handleInstalled = () => {
const manifest = chrome.runtime.getManifest();
const uninstallUrl = `https://www.jetbrains.com/toolbox-app/uninstall/extension/?version=${manifest.version}`;
chrome.runtime.setUninstallURL(uninstallUrl).catch(e => {
console.error('Failed to set uninstall URL: %s', e.message);
});
createExtensionMenu();
};
// eslint-disable-next-line complexity
const handleMessage = (message, sender, sendResponse) => {
switch (message.type) {
case 'enable-page-action':
chrome.action.setIcon({
tabId: sender.tab.id,
path: {128: 'icons/icon-128.png'}
});
const {
project,
https,
ssh
} = message;
const url = encodeURI(`popups/clone.html?project=${project}&https=${https}&ssh=${ssh}`);
chrome.action.setPopup(
{
tabId: sender.tab.id,
popup: chrome.runtime.getURL(url)
}
);
break;
case 'disable-page-action':
chrome.action.setIcon({
tabId: sender.tab.id,
path: {128: 'icons/icon-disabled-128.png'}
});
chrome.action.setPopup(
{
tabId: sender.tab.id,
popup: chrome.runtime.getURL('popups/disabled.html')
}
);
break;
case 'get-protocol':
getProtocol().then(protocol => {
sendResponse({protocol});
});
return true;
case 'save-protocol':
saveProtocol(message.protocol).
then(() => {
// sync options page if it is open
chrome.runtime.sendMessage({
type: 'protocol-changed',
newValue: message.protocol
}).catch(() => {
// do nothing
});
chrome.tabs.query({}, tabs => {
tabs.forEach(t => {
chrome.tabs.sendMessage(t.id, {
type: 'protocol-changed',
newValue: message.protocol
}).catch(() => {
// TODO: re-register the content scripts, probably the extension was updated
});
});
});
}).
catch(() => {
// do nothing
});
break;
case 'get-modify-pages':
getModifyPages().then(allow => {
sendResponse({allow});
});
return true;
case 'save-modify-pages':
saveModifyPages(message.allow).
then(() => {
chrome.tabs.query({}, tabs => {
tabs.forEach(t => {
chrome.tabs.sendMessage(t.id, {
type: 'modify-pages-changed',
newValue: message.allow
}).catch(() => {
// TODO: re-register the content scripts, probably the extension was updated
});
});
});
}).
catch(() => {
// do nothing
});
break;
// no default
}
return undefined;
};
chrome.runtime.onInstalled.addListener(handleInstalled);
chrome.runtime.onMessage.addListener(handleMessage);