-
Notifications
You must be signed in to change notification settings - Fork 1
/
background.js
73 lines (68 loc) · 2.6 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
const strings = {
contentScript: 'index.js',
setIconRequest: 'setIcon',
clickIconRequest: 'iconClick',
localStorageItem: 'allowAnimation',
animateIcon: 'icons/resume32.png',
freezeIcon: 'icons/pause32.png',
}; // Path: background.js
chrome.runtime.onMessage.addListener((request, sender) => {
if (request[strings.setIconRequest]) {
if (request[strings.setIconRequest] === 'true') {
chrome.browserAction.setIcon({
tabId: sender.tab.id,
path: strings.animateIcon,
}); // To set the animateIcon on specified tab
function insertCode(tabId, isDark) {
chrome.tabs.insertCSS(tabId, {
code: isDark
? 'img, video, embed { -webkit-filter: contrast(30%); } '
: 'img, video, embed { -webkit-filter: none; } ',
allFrames: true,
runAt: 'document_start',
});
} // Function to insert css code
chrome.tabs.query({}, function (tabs) {
for (var i = 0; i < tabs.length; ++i) {
var tab = tabs[i];
if (tab.url && tab.url.slice(0, 4) == 'http')
insertCode(tab.id, false);
} // Looping through all tabs and inserting css code
});
} else {
chrome.browserAction.setIcon({
tabId: sender.tab.id,
path: strings.freezeIcon,
}); // To set the freezeIcon on specified tab
function insertCode(tabId, isDark) {
chrome.tabs.insertCSS(tabId, {
code: isDark
? 'img, video, embed { -webkit-filter: contrast(70%); } '
: 'img, video, embed { -webkit-filter: none; } ',
allFrames: true,
runAt: 'document_start',
}); // Function to insert css code
}
chrome.tabs.query({}, function (tabs) {
for (var i = 0; i < tabs.length; ++i) {
var tab = tabs[i];
if (tab.url && tab.url.slice(0, 4) == 'http')
insertCode(tab.id, true);
}
}); // Looping through all tabs and inserting css code
}
}
});
chrome.browserAction.onClicked.addListener((tab) => {
chrome.tabs.sendMessage(tab.id, { [strings.clickIconRequest]: true });
chrome.tabs.executeScript(tab.id, {
code: `
if (localStorage.getItem('${strings.localStorageItem}') == null) {
localStorage.setItem('${strings.localStorageItem}', false);
} else {
localStorage.setItem('${strings.localStorageItem}', localStorage.getItem('${strings.localStorageItem}') === 'true' ? false : true);
}
`,
}); // To toggle the localStorageItem
chrome.tabs.executeScript(tab.id, { file: strings.contentScript });
}); // Event Listener for click on icon