-
-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathbackground.js
More file actions
194 lines (164 loc) · 5.93 KB
/
Copy pathbackground.js
File metadata and controls
194 lines (164 loc) · 5.93 KB
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
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
const browser_action = chrome.action;
function insertCSS(tabId, css) {
chrome.scripting.insertCSS({
target: { tabId: tabId },
css: css
}).catch(error => console.error(error));
}
function removeCSS(tabId, css) {
chrome.scripting.removeCSS({
target: { tabId: tabId },
css: css
}).catch(error => console.error(error));
}
function onError(error) {
console.log(`Error:${error}`);
}
function matchRuleShort(str, rule) {
var escapeRegex = (str) => str.replace(/([.*+?^=!:${}()|\[\]\/\\])/g, "\\$1");
return new RegExp("^" + rule.split("*").map(escapeRegex).join(".*") + "$").test(str);
}
async function restoreOptions(tab) {
if (!tab || !tab.url) return; // Guard against undefined URLs
var storage = await chrome.storage.local.get();
// Use session storage instead of a global variable to survive Service Worker restarts
var session = await chrome.storage.session.get('injectedCSS');
var injectedCache = session.injectedCSS || {};
if (injectedCache[tab.id]) {
removeCSS(tab.id, injectedCache[tab.id]);
}
try {
var url = new URL(tab.url);
var host = url.host;
} catch (e) {
return; // Abort if URL is completely malformed
}
const generateCSS = (config) => {
// Handle the discrepancy: custom options use 'background', default uses 'background_color'
const bg = config.background || config.background_color;
// Explicitly set to 'none' if deactivated to overwrite previous injections
const shadow = config.shadowActivated
? `text-shadow: ${config.shadowColor} 0px 0px ${config.shadowBlur}px !important;`
: 'text-shadow: none !important;';
const decoration = config.decorationActivated
? `text-decoration: ${config.decorationType} ${config.decorationColor} !important;`
: 'text-decoration: none !important;';
return `::selection { background: ${bg} !important; color: ${config.color} !important; ${shadow} ${decoration} }`;
};
let css;
let injected = false;
if (storage.customOptions) {
const matchedConfig = storage.customOptions.find((element) => matchRuleShort(host, element.url));
if (matchedConfig) {
injected = true;
css = generateCSS(matchedConfig);
}
}
// Fallback to default settings if no custom rule matched and the URL is permitted
if (!injected && storage.witness && !/^((chrome:\/\/|chrome-extension:\/\/|about:).*|$|https:\/\/chrome\.google\.com\/webstore.*|https:\/\/addons\.mozilla\.org.*)/.test(tab.url)) {
css = generateCSS(storage);
}
if (css) {
insertCSS(tab.id, css);
// Update the session storage cache
injectedCache[tab.id] = css;
await chrome.storage.session.set({ injectedCSS: injectedCache });
}
}
async function update_action_icon(tabin) {
try {
var tab = await chrome.tabs.get(tabin.tabId);
} catch {
return; // Tab might have closed before this executes
}
var storage = await chrome.storage.local.get();
// Guard against internal pages or missing URLs
if (!tab.url || /^((chrome:\/\/|chrome-extension:\/\/|about:).*|$|https:\/\/chrome\.google\.com\/webstore.*|https:\/\/addons\.mozilla\.org.*)/.test(tab.url)) {
browser_action.setIcon({path: './images/icondisabled.png', tabId: tab.id}); // Isolate to tabId
return;
}
try {
var taburl = new URL(tab.url);
} catch {
return;
}
var injected = false;
if (storage.customOptions) {
storage.customOptions.forEach((element) => {
if (matchRuleShort(taburl.host, element.url)) {
injected = true;
browser_action.setIcon({path: './images/iconcustom.png', tabId: tab.id});
}
});
}
if (!injected) {
browser_action.setIcon({path: './images/icon.png', tabId: tab.id});
}
}
chrome.tabs.onUpdated.addListener((tabId, changeInfo, tab) => {
// Only run the heavy logic when the page has actually finished loading
if (changeInfo.status === 'complete') {
restoreOptions(tab);
update_action_icon({ tabId: tabId }); // Target the specific tab that updated, not just the active one
}
});
chrome.runtime.onMessage.addListener((message, sender) => {
switch(message.request) {
case "inject-css-all":
chrome.tabs.query({}).then((result) => {
result.forEach((tab) => {
restoreOptions(tab);
});
});
break;
case "update-action-icon":
chrome.tabs.query({ active: true, currentWindow: true }, tabs => {
if (tabs.length > 0) update_action_icon({ tabId: tabs[0].id });
});
break;
}
});
chrome.tabs.onActivated.addListener(update_action_icon);
chrome.runtime.onInstalled.addListener(async details => {
switch (details.reason) {
case "install":
chrome.storage.local.set({
background_color: "#007EF333",
color: "#007EF3FF",
shadowActivated: false,
shadowColor: "#007EF3FF",
shadowBlur: "0",
decorationActivated: false,
decorationType: "underline",
decorationColor: "#007EF3FF",
witness: true
});
break;
case "update":
var storage = await chrome.storage.local.get();
if (!storage.hasOwnProperty('decorationActivated')) {
chrome.storage.local.set({
decorationActivated: false,
decorationType: "underline",
decorationColor: "#007EF3FF"
});
}
if (storage.customOptions) {
var customs = storage.customOptions;
customs.forEach((element) => {
try {
let cururl = new URL(element.url);
element.url = cururl.host;
if (!element.hasOwnProperty('decorationActivated')) {
element.decorationActivated = false;
element.decorationType = "underline";
element.decorationColor = "#007EF3FF";
}
} catch (error) {}
});
chrome.storage.local.set({ customOptions: customs });
}
break;
}
});
chrome.runtime.setUninstallURL("https://forms.gle/uRLUAXrwUa7bbBRH8");