-
Notifications
You must be signed in to change notification settings - Fork 39
/
Copy pathbackground.js
153 lines (120 loc) · 3.9 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
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
const
state = {
currentTabId: 0,
previousTabId: 0,
loglevel: !('update_url' in chrome.runtime.getManifest()) ? 'debug' : null
},
RELOAD_TRIGGER_HOSTNAME = 'reload.extensions';
function reloadExtensions() {
// find all unpacked extensions and reload them
chrome.management.getAll(async function (extensions) {
for (const ext of extensions) {
if ((ext.installType === 'development') &&
(ext.enabled === true) &&
(ext.name !== 'Extensions Reloader')) {
const extensionId = ext.id;
const extensionType = ext.type;
await chrome.management.setEnabled(extensionId, false);
await chrome.management.setEnabled(extensionId, true);
// re-launch packaged app
if (extensionType === 'packaged_app') {
chrome.management.launchApp(extensionId);
}
console.log(ext.name + ' reloaded');
}
}
});
// Reload the current tab based on option value
chrome.storage.sync.get('reloadPage', async function (item) {
if (!item.reloadPage) { return; }
const tab = await getCurrentTab() || {};
const currentUrl = tab.url ? new URL(tab.url) : {};
if (currentUrl.hostname !== RELOAD_TRIGGER_HOSTNAME) {
chrome.tabs.reload(state.currentTabId);
}
});
// show an "OK" badge
chrome.browserAction.setBadgeText({ text: 'OK' });
chrome.browserAction.setBadgeBackgroundColor({ color: '#4cb749' });
setTimeout(() => chrome.browserAction.setBadgeText({ text: '' }), 1000);
}
chrome.windows.onFocusChanged.addListener(function (windowId) {
if (windowId !== chrome.windows.WINDOW_ID_NONE) {
refreshState();
}
});
chrome.tabs.onActivated.addListener(function ({ tabId }) {
setCurrentTab(tabId);
});
async function getCurrentTab() {
return new Promise(function (resolve) {
chrome.tabs.query({ active: true, currentWindow: true }, function (tabs) {
resolve((tabs.length > 0) ? tabs[0] : null);
});
});
}
async function getCurrentTabId() {
const tab = await getCurrentTab();
return tab ? tab.id : 0;
}
function setCurrentTab(id) {
if (state.currentTabId === id) {
log('skipped setCurrentTab');
return;
}
state.previousTabId = state.currentTabId;
state.currentTabId = id;
log('previousTabId', state.previousTabId, 'currentTabId', state.currentTabId);
}
chrome.commands.onCommand.addListener(function (command) {
if (command === 'reload') {
reloadExtensions();
}
});
// intercept url
chrome.webRequest.onBeforeRequest.addListener(
function (details) {
if (details.url.indexOf(`http://${RELOAD_TRIGGER_HOSTNAME}`) >= 0) {
chrome.tabs.get(details.tabId, async function (tab) {
const pendingURL = tab.pendingUrl ? new URL(tab.pendingUrl) : {};
const isSafeToCloseTab =
pendingURL.hostname === RELOAD_TRIGGER_HOSTNAME &&
!tab.url; // tab has not yet committed => a newly created tab
if (isSafeToCloseTab) {
let tabId = await getCurrentTabId();
log('before close: ', tabId);
await chrome.tabs.remove(details.tabId);
tabId = await getCurrentTabId();
log('after close: ', tabId);
if (tabId !== state.currentTabId) {
setCurrentTab(state.previousTabId);
await chrome.tabs.update(state.currentTabId, { highlighted: true });
tabId = await getCurrentTabId();
log('after focus tab: ', tabId);
}
await refreshState();
}
reloadExtensions();
});
}
return { cancel: true };
},
{
urls: [`http://${RELOAD_TRIGGER_HOSTNAME}/`],
types: ['main_frame']
},
['blocking']
);
chrome.browserAction.onClicked.addListener(function (/*tab*/) {
reloadExtensions();
});
function log(...args) {
if (state.loglevel === 'debug') {
console.log(...args);
}
}
async function refreshState() {
const tabId = await getCurrentTabId();
setCurrentTab(tabId);
}
refreshState();