-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathbackground.js
More file actions
129 lines (113 loc) · 5.21 KB
/
Copy pathbackground.js
File metadata and controls
129 lines (113 loc) · 5.21 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
console.log("Container Redirector extension loaded");
browser.action.onClicked.addListener(() => {
browser.runtime.openOptionsPage();
});
async function handleRedirect(details) {
// Only process main frame navigations (when a new page is being loaded).
if (details.type !== 'main_frame') {
return { cancel: false };
}
try {
const rules = await browser.storage.sync.get("redirectRules");
if (!rules.redirectRules) {
return { cancel: false };
}
// Determine if the originating tab is a 'moz-extension://' page, 'about:newtab', or 'about:blank'
let shouldCloseOriginatingTab = false;
let originatingTabIdToClose = null;
let targetIndex = undefined;
if (details.tabId) { // Check if the request originated from an existing tab
try {
const originatingTab = await browser.tabs.get(details.tabId);
if (originatingTab) {
// Check if the originating tab's URL matches any of the specified types
if (originatingTab.url &&
(originatingTab.url.startsWith("moz-extension://") ||
originatingTab.url === "about:newtab" ||
originatingTab.url === "about:blank")) {
shouldCloseOriginatingTab = true;
originatingTabIdToClose = originatingTab.id;
// Replace the current tab's position
targetIndex = originatingTab.index;
} else {
// Open next to the current tab
targetIndex = originatingTab.index + 1;
}
}
} catch (error) {
console.error("Error getting originating tab details:", error);
// Continue execution even if we can't get tab details
}
}
for (const rule of rules.redirectRules) {
try {
let regex;
try {
regex = new RegExp(rule.urlPattern);
} catch (error) {
console.error("Invalid regular expression:", rule.urlPattern, error);
continue;
}
if (regex.test(details.url)) {
const ruleName = rule.name || "Unnamed Rule";
console.log(`Matched Rule: "${ruleName}" for URL: ${details.url}`);
const containers = await browser.contextualIdentities.query({});
const targetContainer = containers.find(c => c.name === rule.containerName);
if (!targetContainer) {
console.warn(`Target container not found: ${rule.containerName}`);
continue;
}
// If the request is already in the target container, do nothing.
if (details.cookieStoreId === targetContainer.cookieStoreId) {
return { cancel: false };
}
// Always create a new tab in the target container for main_frame navigations.
try {
const createProperties = {
url: details.url,
cookieStoreId: targetContainer.cookieStoreId,
active: true // Make the new tab active
};
// Apply the calculated index if available
if (targetIndex !== undefined) {
createProperties.index = targetIndex;
}
await browser.tabs.create(createProperties);
// If the originating tab is a detected new tab page, close it.
if (shouldCloseOriginatingTab && originatingTabIdToClose) {
try {
await browser.tabs.remove(originatingTabIdToClose);
}
catch(e){
console.error("Failed to close tab:", e);
}
}
}
catch(e){
console.error("Failed to open new tab:", e);
return {cancel: false}; // If new tab creation fails, don't cancel the original request
}
// Cancel the original request to prevent it from loading in the old tab.
return { cancel: true };
}
} catch (error) {
console.error("Error processing rule:", rule, error);
}
}
} catch (error) {
console.error("Error retrieving redirect rules:", error);
}
return { cancel: false };
}
browser.webRequest.onBeforeRequest.addListener(
handleRedirect,
{ urls: ["<all_urls>"] },
["blocking"]
);
browser.runtime.onInstalled.addListener(() => {
browser.storage.sync.get("redirectRules").then(result => {
if (!result.redirectRules) {
browser.storage.sync.set({ redirectRules: [] });
}
});
});