-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbackground.js
105 lines (99 loc) · 3.41 KB
/
background.js
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
// Taken in large part from Claude 3.7 lmao
// Taken in large part from short-d/short-ext :)
const prefix = "bp/";
const searchProviders = [
"google.com/search",
"bing.com/search",
"duckduckgo.com/",
"ecosia.org/search",
];
// Set up event listener for tab updates to handle search engine queries
// and direct address bar inputs
chrome.tabs.onUpdated.addListener((tabId, changeInfo, tab) => {
if (changeInfo.status === 'loading' && tab.url) {
try {
// Check if URL is from a search engine
if (searchProviders.some(provider => tab.url.includes(provider))) {
const url = new URL(tab.url);
const searchQuery = url.searchParams.get("q") || url.searchParams.get("p") || url.searchParams.get("query");
if (searchQuery && searchQuery.includes(prefix)) {
// Extract the path after the prefix
const path = searchQuery.substring(searchQuery.lastIndexOf(prefix) + prefix.length).trim();
const redirectUrl = `https://go.calblueprint.org/?q=${encodeURIComponent(path)}`;
// Redirect the tab
chrome.tabs.update(tabId, { url: redirectUrl });
}
}
// Check for direct "bp/" input in the address bar
// This handles cases where the user types bp/something directly
else if (tab.url.startsWith(prefix) || tab.url.includes(`://${prefix}`)) {
const path = tab.url.substring(tab.url.indexOf(prefix) + prefix.length).trim();
const redirectUrl = `https://go.calblueprint.org/?q=${encodeURIComponent(path)}`;
chrome.tabs.update(tabId, { url: redirectUrl });
}
} catch (error) {
console.error("Error processing URL:", error);
}
}
});
// Set up declarativeNetRequest rules for handling URLs with bp/ format
async function setupRules() {
// Clear existing rules
const existingRules = await chrome.declarativeNetRequest.getDynamicRules();
await chrome.declarativeNetRequest.updateDynamicRules({
removeRuleIds: existingRules.map(rule => rule.id)
});
// Add rules to capture various formats of bp/ URLs
await chrome.declarativeNetRequest.updateDynamicRules({
addRules: [
// Rule for when user types "bp/query" directly in the address bar
// Chrome might interpret this as a search if it's not a valid URL
{
id: 1,
priority: 1,
action: {
type: "redirect",
redirect: {
url: "https://go.calblueprint.org/?q="
}
},
condition: {
urlFilter: "bp/*",
resourceTypes: ["main_frame"]
}
},
// Rule for when Chrome prepends "http://" to the bp/ input
{
id: 2,
priority: 1,
action: {
type: "redirect",
redirect: {
regexSubstitution: "https://go.calblueprint.org/?q=\\1"
}
},
condition: {
regexFilter: "^http://bp/(.*)$",
resourceTypes: ["main_frame"]
}
},
// Rule for when Chrome prepends "https://" to the bp/ input
{
id: 3,
priority: 1,
action: {
type: "redirect",
redirect: {
regexSubstitution: "https://go.calblueprint.org/?q=\\1"
}
},
condition: {
regexFilter: "^https://bp/(.*)$",
resourceTypes: ["main_frame"]
}
}
]
});
}
// Initialize the extension
setupRules().catch(err => console.error("Error setting up rules:", err));