-
Notifications
You must be signed in to change notification settings - Fork 9
/
Copy pathbackground.js
113 lines (98 loc) · 4.87 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
106
107
108
109
110
111
112
113
let bangs = [];
// Get current list of Bangs from DuckDuckGo
(async () => {
const res = await fetch("https://duckduckgo.com/bang.js");
bangs = await res.json();
})();
chrome.webRequest.onBeforeRequest.addListener(({ url }) => {
// Get search query from URL
const urlObj = new URL(url);
const query = urlObj.searchParams.get('q');
// Check if query has a bang anywhere in it
if (query && query.includes('!') && bangs.some((bang) => {
const bangStartIdx = query.indexOf(`!${bang.t}`);
const prevCharIdx = bangStartIdx - 1;
const nextCharIdx = bangStartIdx + bang.t.length + 1;
return bangStartIdx > -1 && (nextCharIdx === query.length || query[nextCharIdx] === ' ') && (prevCharIdx === -1 || query[prevCharIdx] === ' ');
})) {
// Redirect to DuckDuckGo
return {
redirectUrl: `https://www.duckduckgo.com/?q=${encodeURIComponent(query)}`,
};
}
}, {
urls: [
"http://www.google.com/search*", "http://www.google.co.jp/search*", "http://www.google.co.uk/search*", "http://www.google.es/search*", "http://www.google.ca/search*", "http://www.google.de/search*", "http://www.google.it/search*", "http://www.google.fr/search*", "http://www.google.com.au/search*", "http://www.google.com.tw/search*", "http://www.google.nl/search*", "http://www.google.com.br/search*", "http://www.google.com.tr/search*", "http://www.google.be/search*", "http://www.google.com.gr/search*", "http://www.google.co.in/search*", "http://www.google.com.mx/search*", "http://www.google.dk/search*", "http://www.google.com.ar/search*", "http://www.google.ch/search*", "http://www.google.cl/search*", "http://www.google.at/search*", "http://www.google.co.kr/search*", "http://www.google.ie/search*", "http://www.google.com.co/search*", "http://www.google.pl/search*", "http://www.google.pt/search*", "http://www.google.com.pk/search*", "https://www.google.com/search*", "https://www.google.co.jp/search*", "https://www.google.co.uk/search*", "https://www.google.es/search*", "https://www.google.ca/search*", "https://www.google.de/search*", "https://www.google.it/search*", "https://www.google.fr/search*", "https://www.google.com.au/search*", "https://www.google.com.tw/search*", "https://www.google.nl/search*", "https://www.google.com.br/search*", "https://www.google.com.tr/search*", "https://www.google.be/search*", "https://www.google.com.gr/search*", "https://www.google.co.in/search*", "https://www.google.com.mx/search*", "https://www.google.dk/search*", "https://www.google.com.ar/search*", "https://www.google.ch/search*", "https://www.google.cl/search*", "https://www.google.at/search*", "https://www.google.co.kr/search*", "https://www.google.ie/search*", "https://www.google.com.co/search*", "https://www.google.pl/search*", "https://www.google.pt/search*", "https://www.google.com.pk/search*"
],
}, [
"blocking" // Needed so we can redirect the request to DuckDuckGo - we aren't really blocking anything
]);
// Omnibox Auto-Complete
chrome.omnibox.onInputChanged.addListener((text, addSuggestions) => {
let filterText = text.trim();
if (filterText.indexOf("!") === 0) {
filterText = filterText.substr(1)
}
chrome.omnibox.setDefaultSuggestion({
description: 'Use Bang'
});
let results = [];
for (let bang of bangs) {
if (
bang.t.startsWith(filterText) ||
bang.s.includes(filterText) ||
bang.d.includes(filterText)
) {
// Shorter Bang => More relevant
let lengthRelevance = (10 - bang.t.length);
if (lengthRelevance > 0){
lengthRelevance = 0;
}
// Relevance based on the type of match
let typeRelevance = 0;
if (bang.t === filterText) {
// Exact match - set as default suggestion
chrome.omnibox.setDefaultSuggestion({
description: `!${bang.t} (${bang.s})`
});
continue;
} else if (bang.t.startsWith(filterText)) {
typeRelevance = 100;
} else if (bang.s.includes(filterText)) {
typeRelevance = 2;
}
const relevance = lengthRelevance + typeRelevance + bang.r;
results.push({
content: bang.t,
description: `!${bang.t} (${bang.s}), Rel: ${relevance}`,
relevance,
});
}
}
// Sort based on relevance
results = results.sort((a, b) => b.relevance - a.relevance).slice(0, 10).map((item) => {
return {
content: item.content,
description: item.description,
};
});
addSuggestions(results);
});
chrome.omnibox.onInputEntered.addListener((text, disposition) => {
let bang = text.trim();
if (bang.indexOf("!") === 0) {
bang = bang.substr(1)
}
const url = `https://www.duckduckgo.com/?q=${encodeURIComponent(`!${bang}`)}`;
switch (disposition) {
case "currentTab":
chrome.tabs.update({url});
break;
case "newForegroundTab":
chrome.tabs.create({url});
break;
case "newBackgroundTab":
chrome.tabs.create({url, active: false});
break;
}
});