Repository navigation
Expand file tree
/
Copy pathbackground.js
More file actions
97 lines (84 loc) · 3.15 KB
/
Copy pathbackground.js
File metadata and controls
97 lines (84 loc) · 3.15 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
// Default list of domains to redirect to external browser
let redirectDomains = ["x.ai", "grok.com"];
// Load saved domains from storage on startup
browser.storage.local.get("redirectDomains").then((result) => {
if (result.redirectDomains && result.redirectDomains.length > 0) {
redirectDomains = result.redirectDomains;
console.log("Loaded redirect domains:", redirectDomains);
} else {
// Save default domains if none exist
browser.storage.local.set({ redirectDomains: redirectDomains });
}
});
// Listen for storage changes (when user updates domain list)
browser.storage.onChanged.addListener((changes, area) => {
if (area === "local" && changes.redirectDomains) {
redirectDomains = changes.redirectDomains.newValue;
console.log("Updated redirect domains:", redirectDomains);
}
});
// Check if a URL matches any of the redirect domains
function shouldRedirect(url) {
try {
const urlObj = new URL(url);
const hostname = urlObj.hostname.toLowerCase();
return redirectDomains.some((domain) => {
const normalizedDomain = domain.toLowerCase().replace(/^www\./, "");
const normalizedHostname = hostname.replace(/^www\./, "");
return (
normalizedHostname === normalizedDomain ||
normalizedHostname.endsWith("." + normalizedDomain)
);
});
} catch (e) {
console.error("Error parsing URL:", e);
return false;
}
}
// Intercept web requests before they load
browser.webRequest.onBeforeRequest.addListener(
function (details) {
// Only handle main frame navigation (not iframes, images, scripts, etc.)
if (details.type !== "main_frame") {
return {};
}
// Check if this URL should be redirected
if (shouldRedirect(details.url)) {
console.log("Intercepting URL for external browser:", details.url);
// Try to open in external browser via native messaging
browser.runtime
.sendNativeMessage("ioswitch", { url: details.url })
.then(() => {
console.log("Successfully sent URL to external browser:", details.url);
// Close the tab after successful redirect
browser.tabs.remove(details.tabId).catch(() => {});
})
.catch((error) => {
console.error("Native messaging failed:", error);
// Fallback: Show redirect page with manual instructions
browser.tabs.update(details.tabId, {
url:
browser.runtime.getURL("redirect.html") +
"?url=" +
encodeURIComponent(details.url),
});
});
// Block the original request in Firefox
return { cancel: true };
}
return {};
},
{ urls: ["<all_urls>"] },
["blocking"]
);
// Also handle when user types URL in address bar or clicks links
browser.webNavigation.onBeforeNavigate.addListener((details) => {
// Only handle main frame
if (details.frameId !== 0) {
return;
}
if (shouldRedirect(details.url)) {
console.log("Navigation intercepted:", details.url);
}
});
console.log("IO Switch extension loaded. Redirecting domains:", redirectDomains);