forked from fej-snikduj/URLColors
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathurlColorsServiceWorker.js
executable file
·151 lines (133 loc) · 4.78 KB
/
urlColorsServiceWorker.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
let snoozeTimeout;
const updateValue = (property, value) => {
chrome.storage.local.set({[property]: value}, () => {
console.log(`Updated ${property} to: `, value);
});
};
const handleSnooze = (snoozeTime) => {
clearTimeout(snoozeTimeout);
const diffInTime = snoozeTime - Date.now();
if (diffInTime > 0) {
snoozeTimeout = setTimeout(() => {
updateValue('snoozeUntil', '');
sendUpdateMessageToAllTabs('handleSnooze timeout expired');
}, diffInTime);
} else {
updateValue('snoozeUntil', '');
sendUpdateMessageToAllTabs('handleSnooze timeout expired');
}
}
const injectContentScript = (tabId, callback)=> {
chrome.scripting.executeScript({
target: {tabId: tabId},
files: ['urlColorsContentScript.js']
}, () => {
if (chrome.runtime.lastError) {
console.log(`Could not inject script into tab ${tabId}: ${chrome.runtime.lastError.message}`);
} else {
console.log(`Successfully injected the content script into tab ${tabId}`);
// Call the callback function if provided
if (typeof callback === "function") {
callback(tabId);
}
}
});
}
const injectContentScriptOnAllTabs = () => {
chrome.tabs.query({}, (tabs) => {
tabs.forEach((tab) => {
if (tab.url.startsWith('http://') || tab.url.startsWith('https://')) {
injectContentScript(tab.id);
}
});
});
}
const sendMessageToTab = (tabId) => {
chrome.tabs.sendMessage(tabId, {action: "updateTab"}, response => {
if (chrome.runtime.lastError) {
console.error(chrome.runtime.lastError);
} else {
console.log('Sent message to update tab with id: ', tabId, ' with response:', response);
}
});
}
const attemptToSendMessage = (tabId) => {
chrome.tabs.sendMessage(tabId, {action: "ping"}, () => {
if (chrome.runtime.lastError) {
// No response indicates the script isn't there, inject and then send message
injectContentScript(tabId, sendMessageToTab);
} else {
// Got a response, so the content script is already there, just send the message
console.log('Content script already injected, sending updateTab message now to tab: ', tabId);
sendMessageToTab(tabId);
}
});
}
const sendUpdateMessageToAllTabs = (originator) => {
console.log('sendUpdateMessageToAllTabs called with originator:', originator);
chrome.tabs.query({}, (tabs) => {
console.log('tabs', tabs);
tabs.forEach((tab) => {
attemptToSendMessage(tab.id);
});
});
};
const setBadge = (text, color, title) => {
chrome.action.setBadgeText({text});
chrome.action.setBadgeBackgroundColor({color});
chrome.action.setTitle({title});
}
const setNeedUpdateBadge = () => {
setBadge('!', 'red', 'Please open the popup to migrate your settings to URLColors V2.');
}
const setSuccessfulUpdateBadge = () => {
setBadge("✅", "green", "Settings have been migrated successfully");
}
const removeBadge = () => {
setBadge('', '', '');
}
chrome.runtime.onStartup.addListener(() => {
injectContentScriptOnAllTabs();
// On startup, if snooze is still active, set a timeout to clear it when it expires.
chrome.storage.local.get(['snoozeUntil'], (result) => {
if (result.snoozeUntil) {
handleSnooze(result.snoozeUntil);
}
});
});
chrome.runtime.onInstalled.addListener((details) => {
injectContentScriptOnAllTabs();
if (details.reason === "update" && details.previousVersion === "1.1.2") {
// Notify the user to open the popup for completing the migration
setNeedUpdateBadge();
}
});
chrome.tabs.onUpdated.addListener(
(tabId, changeInfo, tab) => {
// Only update if URL has changed and status is complete.
chrome.tabs.sendMessage(tab.id, { action: "updateTab" }, (response) => {
if (chrome.runtime.lastError) {
console.error(chrome.runtime.lastError);
} else {
console.log('Sent message to update tab with id: ', tab.id, ' with response:', response);
}
});
}
);
chrome.storage.onChanged.addListener((changes, namespace) => {
if ((changes.prefs || changes.snoozeUntil || changes.active) && namespace === 'local') {
if (changes.snoozeUntil && changes.snoozeUntil.newValue) {
handleSnooze(changes.snoozeUntil.newValue);
}
sendUpdateMessageToAllTabs('storage.onChanged listener');
}
});
// Message receiving
chrome.runtime.onMessage.addListener((message, sender, sendResponse) => {
if (message.action === "settingsMigrated") {
setSuccessfulUpdateBadge();
}
if (message.action === 'removeBadge') {
removeBadge();
}
});