-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathbackground.js
More file actions
49 lines (39 loc) · 1.35 KB
/
Copy pathbackground.js
File metadata and controls
49 lines (39 loc) · 1.35 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
async function getAllowedOrigins() {
const { allowedOrigins = [] } = await chrome.storage.sync.get({ allowedOrigins: [] });
return Array.isArray(allowedOrigins) ? allowedOrigins : [];
}
async function shouldRunOnUrl(urlString) {
let url;
try {
url = new URL(urlString);
} catch {
return { ok: false };
}
if (url.protocol !== 'https:' && url.protocol !== 'http:') return { ok: false };
const originPattern = `${url.origin}/*`;
const allowed = await getAllowedOrigins();
if (!allowed.includes(originPattern)) return { ok: false, originPattern };
const hasPermission = await chrome.permissions.contains({ origins: [originPattern] });
if (!hasPermission) return { ok: false, originPattern };
return { ok: true, originPattern };
}
async function injectForTex(tabId) {
try {
await chrome.scripting.executeScript({
target: { tabId, allFrames: true },
files: ['quillOverride.js'],
});
} catch (_) {
// Ignore injection failures (e.g., restricted pages, navigation races)
}
}
chrome.tabs.onUpdated.addListener(async (tabId, changeInfo, tab) => {
if (changeInfo.status !== 'complete') return;
if (!tab || !tab.url) return;
const { ok } = await shouldRunOnUrl(tab.url);
if (!ok) return;
await injectForTex(tabId);
});
chrome.runtime.onInstalled.addListener(() => {
// Options page manages permissions
});