-
-
Notifications
You must be signed in to change notification settings - Fork 8
/
background.js
74 lines (67 loc) · 1.82 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
/**
* @file
* The service worker that does background thread work per manifest v3.
*/
/**
* If the native chrome event completes, this fires separately.
*
* @param tabId
* The tab Id, primarily to address messaging.
*/
const handleTabUpdate = function (tabId) {
"use strict";
// Define locals.
let ext = {
"matches": false,
"orientation": "top",
"bgcolor": "#ff0000"
};
/**
* Determine whether to do anything after tab is retrieved.
*
* @param tab
* The tab we are acting on.
*/
function tabsGetCallback(tab) {
/**
* Grab the settings from chrome storage.
*/
chrome.storage.sync.get('rows', function (items) {
// This is sort of inefficient, but should scale linearly-ish.
if (items && items.rows && items.rows.length) {
for (let i = 0; i < items.rows.length; i++) {
if (tab.url.match(items.rows[i].pattern) || tab.title.match(items.rows[i].pattern)) {
ext.matches = true;
ext.orientation = items.rows[i].orientation;
ext.bgcolor = items.rows[i].bgcolor;
break;
}
}
}
// Only send the trigger to proceed on the tab if there's a regex match.
if (ext.matches) {
chrome.tabs.sendMessage(tabId, {
"action": "applyMatch",
"data": {
'ext': ext,
'favIconUrl': tab.favIconUrl,
}
});
}
});
}
// Trigger the callback, based on the tabId sent to this handler.
chrome.tabs.get(tabId, tabsGetCallback);
};
/**
* This listener kicks everything off.
*
* It conditionally dispatches tasks to other listeners if the tab update
* is complete.
*/
chrome.tabs.onUpdated.addListener(function (tabId, props) {
"use strict";
if (props && props.status === "complete") {
handleTabUpdate(tabId);
}
});