-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathbackground.js
More file actions
37 lines (36 loc) · 1.54 KB
/
Copy pathbackground.js
File metadata and controls
37 lines (36 loc) · 1.54 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
// Background script for Terminal X Web Companion
chrome.tabs.onUpdated.addListener((tabId, changeInfo, tab) => {
if (changeInfo.status === 'complete' && tab.url) {
chrome.storage.local.get(['terminalXScripts'], (result) => {
const scripts = result.terminalXScripts || [];
scripts.forEach(script => {
if (script.enabled && matchesPattern(tab.url, script.pattern)) {
console.log(`Injecting script: ${script.name} into ${tab.url}`);
chrome.scripting.executeScript({
target: { tabId: tabId },
func: (code) => {
try {
const s = document.createElement('script');
s.textContent = code;
(document.head || document.documentElement).appendChild(s);
s.remove();
} catch (e) {
console.error("Terminal X Injection Error:", e);
}
},
args: [script.code]
});
}
});
});
}
});
function matchesPattern(url, pattern) {
if (pattern === '<all_urls>' || pattern === '*') return true;
try {
const regex = new RegExp('^' + pattern.split('*').map(s => s.replace(/[.+^${}()|[\]\\]/g, '\\$&')).join('.*') + '$');
return regex.test(url);
} catch (e) {
return false;
}
}