-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathbackground.js
More file actions
34 lines (30 loc) · 1.02 KB
/
background.js
File metadata and controls
34 lines (30 loc) · 1.02 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
// Run on startup
chrome.runtime.onStartup.addListener(getCurrentDomain);
chrome.runtime.onInstalled.addListener(getCurrentDomain);
// Listen for tab changes
chrome.tabs.onUpdated.addListener((tabId, changeInfo, tab) => {
if (changeInfo.status === 'complete' && tab.active) {
updateCurrentDomain(tab);
}
});
chrome.tabs.onActivated.addListener((activeInfo) => {
chrome.tabs.get(activeInfo.tabId, updateCurrentDomain);
});
function getCurrentDomain() {
chrome.tabs.query({active: true, currentWindow: true}, (tabs) => {
if (tabs[0]) updateCurrentDomain(tabs[0]);
});
}
function updateCurrentDomain(tab) {
if (tab && tab.url && !tab.url.startsWith('chrome://') && !tab.url.startsWith('chrome-extension://')) {
try {
const url = new URL(tab.url);
const domain = url.hostname;
chrome.storage.local.set({currentDomain: domain}, () => {
console.log('Current domain set in storage:', domain);
});
} catch (e) {
console.error('Error parsing URL or setting domain:', e);
}
}
}