-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathbackground.js
More file actions
185 lines (86 loc) · 3.06 KB
/
Copy pathbackground.js
File metadata and controls
185 lines (86 loc) · 3.06 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
// background.js
console.log('Background script loaded');
chrome.runtime.onInstalled.addListener(() => {
chrome.storage.sync.set({
enabledDomains: [],
mode: 'all', // 'all', 'specific', or 'off'
usage: { all: 0, specific: 0 }
});
});
chrome.action.onClicked.addListener((tab) => {
console.log(`Action clicked: ${tab.url}`);
applyMode(tab.id, tab);
});
function applyMode(tabId, tab) {
// Check if the tab URL is an internal Chrome URL
if (tab.url.startsWith('chrome://')) {
return;
}
chrome.storage.sync.get(['mode', 'enabledDomains'], (result) => {
const { mode, enabledDomains } = result;
const domain = getDomain(tab.url);
console.log(`Applying mode: ${mode} for domain: ${domain}`);
if (mode === 'all' || (mode === 'specific' && enabledDomains.includes(domain))) {
chrome.scripting.executeScript({
target: { tabId: tabId },
files: ['content.js']
}, (response) => {
if (chrome.runtime.lastError) {
console.error('BackTab Navigator Error:', chrome.runtime.lastError.message);
} else {
console.log('BackTab Navigator enabled');
}
});
}
});
}
function getDomain(url) {
try {
const parsedUrl = new URL(url);
// Handle IP addresses and port numbers
if (parsedUrl.hostname.match(/^\d{1,3}(\.\d{1,3}){3}$/)) {
return `${parsedUrl.hostname}:${parsedUrl.port}`;
}
return parsedUrl.hostname;
} catch (error) {
console.error('Error in getDomain:', error);
return '';
}
}
chrome.runtime.onMessage.addListener((request, sender, sendResponse) => {
if (request.action === 'openInBackgroundTab') {
chrome.tabs.query({ active: true, currentWindow: true }, (tabs) => {
const currentTab = tabs[0];
chrome.tabs.create({ url: request.url, active: false, index: currentTab.index + 1 }, (newTab) => {
// Update usage statistics
chrome.storage.sync.get(['mode', 'usage'], (result) => {
const { mode, usage } = result;
if (mode === 'all' || mode === 'specific') {
usage[mode]++;
chrome.storage.sync.set({ usage });
}
});
sendResponse({ success: true });
});
});
return true; // Indicate that we'll be sending a response asynchronously
} else if (request.action === 'modeChanged') {
// Notify all tabs about the mode change
chrome.tabs.query({}, (tabs) => {
tabs.forEach(tab => {
if (!tab.url.startsWith('chrome://')) {
chrome.tabs.sendMessage(tab.id, {
action: 'updateMode',
mode: request.mode,
enabledDomains: request.enabledDomains
}).catch(() => {
// Ignore errors for inactive tabs
});
}
});
});
sendResponse({ success: true });
} else if (request.action === 'logError') {
console.error('BackTab Navigator Error:', request.error);
}
});