-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbackground.js
100 lines (93 loc) · 2.86 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
chrome.action.onClicked.addListener((tab) => {
const tabId = tab.id;
chrome.storage.local.get([tabId.toString()], (result) => {
if (result[tabId]) {
chrome.storage.local.remove(tabId.toString(), () => {
console.log(`Tab ${tabId} is now inactive`);
});
chrome.tabs.sendMessage(tabId, { action: 'stop' });
}
else {
chrome.storage.local.set({ [tabId]: true }, () => {
console.log(`Tab ${tabId} is now active`);
});
chrome.tabs.sendMessage(tabId, { action: 'run' });
}
});
});
chrome.runtime.onMessage.addListener((message, sender, sendResponse) => {
const tabId = sender.tab.id;
chrome.storage.local.get([tabId.toString()], (result) => {
if (result[tabId]) {
switch (message.action) {
case 'insertCSS':
chrome.scripting.insertCSS({
target: { tabId: tabId },
files: [message.style]
});
break;
case 'isActive':
chrome.tabs.sendMessage(tabId, { action: result[tabId] ? 'run' : 'stop' });
break;
case 'update':
chrome.tabs.sendMessage(tabId, { action: 'updateComponents', location: message.location });
break;
case 'run':
chrome.tabs.sendMessage(tabId, { action: 'run', location: message.location });
break;
case 'runComponents':
chrome.tabs.sendMessage(tabId, { action: 'runComponents' });
break;
case 'updateComponents':
chrome.tabs.sendMessage(tabId, { action: 'updateComponents' });
break;
case 'finishedComponents':
chrome.tabs.sendMessage(tabId, { action: 'finishedComponents', component: message.component });
break;
case 'fetchImageSize':
fetchImageSize(message.src, tabId);
break;
}
}
});
});
function fetchImageSize(src, tabId) {
fetch(src, {
method: 'HEAD'
})
.then((res) => {
if (!res.ok) {
fetch(src).then(res2 => {
if (!res2.ok)
chrome.tabs.sendMessage(tabId, {
action: "fetchImageSizeResponse",
src: src,
size: 0
});
else
chrome.tabs.sendMessage(tabId, {
action: "fetchImageSizeResponse",
src: src,
size: res2.headers.get("Content-Length")
});
});
} else
chrome.tabs.sendMessage(tabId, {
action: "fetchImageSizeResponse",
src: src,
size: res.headers.get("Content-Length")
});
})
.catch(function (error) {
chrome.tabs.sendMessage(tabId, {
action: "fetchImageSizeResponse",
src: src,
size: 0
});
});
}
chrome.tabs.onRemoved.addListener((tabId) => {
chrome.storage.local.remove(tabId.toString(), () => {
console.log(`Tab ${tabId} is now inactive`);
});
});