-
Notifications
You must be signed in to change notification settings - Fork 1
/
background.js
40 lines (31 loc) · 1.42 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
const redirectedTabs = new Set();
chrome.runtime.onMessage.addListener((message, sender, sendResponse) => {
if (message.action === "checkQuery") {
const query = message.query;
// Check if this tab has already been redirected
if (redirectedTabs.has(sender.tab.id)) {
return;
}
// Check query length
const wordCount = query.trim().split(/\s+/).length;
let redirectUrl;
if (wordCount <= 3) {
// Redirect to Google for queries with fewer than 3 words
redirectUrl = `https://www.google.com/search?q=${encodeURIComponent(query)}`;
} else {
// Redirect to ChatGPT for queries with 3 or more words
redirectUrl = `https://chatgpt.com/?q=${encodeURIComponent(query)}&hints=search`;
}
// Mark this tab as redirected to avoid a loop
redirectedTabs.add(sender.tab.id);
// Update the current tab with the chosen URL
chrome.tabs.update(sender.tab.id, { url: redirectUrl });
// Update the current tab with the chosen URL
chrome.tabs.update(sender.tab.id, { url: redirectUrl }, () => {
// Remove the tab from the set after redirection to allow new searches in the future
setTimeout(() => {
redirectedTabs.delete(sender.tab.id);
}, 1000); // 1-second delay for cleanup
});
}
});