-
-
Notifications
You must be signed in to change notification settings - Fork 23
/
Copy pathbackground.js
224 lines (210 loc) · 7.13 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
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
importScripts('/js/shared.js');
// Global variables
const isChrome = Boolean(navigator.userAgent.includes('Chrome'))
const isFirefox = Boolean(navigator.userAgent.includes('Firefox'))
var wikiLangArray = []
var wikiPrefixArray = []
var userLanguage = ''
var multiLang = ''
var siteVersion = ''
var activeLanguage = ''
var currentRequest = null
// Load data and settings when Omnibox search is activated
chrome.omnibox.onInputStarted.addListener(function () {
updateDefaultSuggestion('', activeLanguage)
chrome.storage.local.get(function (data) {
userLanguage = data.userLanguage
multiLang = data.multiLang
wikiLangArray = data.wikiLangArray
wikiPrefixArray = data.wikiPrefixArray
siteVersion = data.siteVersion
})
})
chrome.omnibox.onInputChanged.addListener(async function (text, suggest) {
// If the first word in the query matches a known Wikipedia language, and multi-language is enabled, change the active search to that language
var firstWord = text.split(' ')[0]
if ((multiLang === true) && text.startsWith(firstWord + ' ') && (wikiPrefixArray.includes(firstWord))) {
activeLanguage = firstWord
text = text.replace(firstWord + ' ', '')
} else {
activeLanguage = userLanguage
}
updateDefaultSuggestion(text, activeLanguage)
if (text.length > 0) {
var localCurrentRequest = suggests(text)
currentRequest = localCurrentRequest
localCurrentRequest.then(function (data) {
if (localCurrentRequest !== currentRequest) {
return
}
// Set the maximum number of suggestion slots, and leave one for the settings option
var results = []
if (isFirefox) {
// Firefox supports 4 suggestions
num = 4
} else if (isChrome) {
// Chrome can do 8 suggestions
num = 8
}
for (var i = 0; i < num; i++) {
var content = data[1][i]
if (content) {
results.push({
content: content,
description: content
})
}
}
// Add settings suggestion
if (isFirefox) {
// Firefox doesn't support <dim>
results.push({
content: "settings",
description: "Change default search language (currently set to " + wikiLangArray[wikiPrefixArray.indexOf(userLanguage)] + ")"
})
} else {
results.push({
content: "settings",
description: "<dim>Change default search language (currently set to " + wikiLangArray[wikiPrefixArray.indexOf(userLanguage)] + ")</dim>"
})
}
suggest(results)
})
}
})
function resetDefaultSuggestion() {
chrome.omnibox.setDefaultSuggestion({
description: ' '
})
}
resetDefaultSuggestion()
function updateDefaultSuggestion(text, activeLanguage) {
// Remove language prefix from live results
if (text.startsWith(activeLanguage + ' ')) {
text = text.replace(activeLanguage + ' ', '')
}
// Add default suggestion
if (isFirefox) {
// Firefox doesn't support <dim>
chrome.omnibox.setDefaultSuggestion({
description: text + ' — ' + wikiLangArray[wikiPrefixArray.indexOf(activeLanguage)]
})
} else {
chrome.omnibox.setDefaultSuggestion({
description: text + ' <dim>- ' + wikiLangArray[wikiPrefixArray.indexOf(activeLanguage)] + '</dim>'
})
}
}
chrome.omnibox.onInputCancelled.addListener(function () {
resetDefaultSuggestion()
})
async function suggests(query) {
return new Promise(async function (resolve, reject) {
const url = "https://" + activeLanguage + ".wikipedia.org/w/api.php?action=opensearch&namespace=0&suggest=&search=" + encodeURIComponent(query)
const response = await fetch(url)
if (!response.ok) {
console.log('Could not obtain data from Wikipedia API.')
resolve(null)
}
const json = await response.json()
resolve(json)
})
}
chrome.omnibox.onInputEntered.addListener(function (text) {
if (text == "settings") {
chrome.runtime.openOptionsPage()
} else {
// If a search prefix is being used, exclude it from the text string
if (text.startsWith(activeLanguage + ' ')) {
text = text.replace(activeLanguage + ' ', '')
}
if (siteVersion === 'desktop') {
chrome.tabs.update(null, { url: "https://" + activeLanguage + ".wikipedia.org/w/index.php?search=" + encodeURIComponent(text) })
} else if (siteVersion === 'mobile') {
chrome.tabs.update(null, { url: "https://" + activeLanguage + ".m.wikipedia.org/w/index.php?search=" + encodeURIComponent(text) })
} else if (siteVersion === 'wikiwand') {
chrome.tabs.update(null, { url: "https://www.wikiwand.com/" + activeLanguage + "/" + encodeURIComponent(text) })
}
}
})
// Load settings and languages from storage when the extension is initialized
chrome.storage.local.get(async function (data) {
// Add languages to storage if they are not there
if (!data.wikiPrefixArray || !data.wikiLangArray) {
await getWikis()
}
if (typeof data.multiLang == 'undefined') {
chrome.storage.local.set({
multiLang: false
})
}
if (typeof data.siteVersion == 'undefined') {
chrome.storage.local.set({
siteVersion: 'desktop'
})
}
if (data.userLanguage) {
console.log("Language already set to '" + data.userLanguage + "' (" + defaultLangArray[defaultPrefixArray.indexOf(data.userLanguage)] + ")")
} else {
// Detect system language and set it as the default
resetToSystemLanguage()
}
})
// Initialize welcome message and context menu entry on extension load
chrome.runtime.onInstalled.addListener(function (details) {
// Initialize context menu
chrome.contextMenus.create({
id: "search-wikipedia",
title: 'Search Wikipedia for \"%s\"',
contexts: ['selection']
})
// Show welcome message
if (details.reason === 'install' || details.reason === 'update') {
// Set message
const notification = {
type: 'basic',
iconUrl: chrome.runtime.getURL('img/icon128.png'),
title: 'Wikipedia Search ' + chrome.runtime.getManifest().version + ' installed!',
buttons: [
{
title: 'Open Settings'
},
{
title: 'Join Discord'
}
],
message: "Click here to see what's new in this version."
}
// Send notification
chrome.notifications.create(notification, () => {
// Handle notification click
chrome.notifications.onClicked.addListener(function() {
chrome.tabs.create({ url: 'https://corbin.io/wikipedia-search-11' })
})
// Handle notification button clicks
chrome.notifications.onButtonClicked.addListener(function(_, buttonIndex) {
if (buttonIndex === 0) {
chrome.runtime.openOptionsPage()
} else if (buttonIndex === 1) {
// Open Discord
chrome.tabs.create({ url: 'https://discord.com/invite/59wfy5cNHw' })
}
})
})
}
})
// Function for context menu search
chrome.contextMenus.onClicked.addListener(function (info, tab) {
if (info.menuItemId == "search-wikipedia") {
chrome.storage.local.get(function (data) {
if (data.siteVersion === 'desktop') {
var url = 'https://' + data.userLanguage + '.wikipedia.org/w/index.php?title=Special:Search&search=' + encodeURIComponent(info.selectionText)
} else if (data.siteVersion === 'mobile') {
var url = 'https://' + data.userLanguage + '.m.wikipedia.org/w/index.php?title=Special:Search&search=' + encodeURIComponent(info.selectionText)
} else if (data.siteVersion === 'wikiwand') {
var url = 'https://www.wikiwand.com/' + data.userLanguage + '/' + encodeURIComponent(info.selectionText)
}
chrome.tabs.create({ url: url })
})
}
})