forked from KevinPayravi/indie-wiki-buddy
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbackground.js
223 lines (198 loc) · 8.1 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
if (typeof importScripts !== 'undefined') {
importScripts('scripts/common-functions.js');
}
let cachedStorage = {};
async function updateCachedStorage() {
let localStorage = await chrome.storage.local.get();
let syncStorage = await chrome.storage.sync.get();
cachedStorage = {...localStorage, ...syncStorage};
}
async function getCachedStorage() {
if (Object.keys(cachedStorage).length === 0) {
await updateCachedStorage();
}
return cachedStorage;
}
// Cache Chrome's storage in memory so that we don't need to make repeated calls
updateCachedStorage();
// Capture web requests
chrome.webRequest.onBeforeSendHeaders.addListener(
async (event) => {
if (event.documentLifecycle !== 'prerender') {
if (event.frameType === 'sub_frame') {
let tabInfo = await chrome.tabs.get(event.tabId);
main(tabInfo.url, event.tabId);
} else {
main(event.url, event.tabId);
}
}
},
{ urls: ['*://*.fandom.com/*', '*://*.wiki.fextralife.com/*', '*://*.neoseeker.com/wiki/*'], types: ['main_frame', 'sub_frame'] }
);
// Listen for user turning extension on or off, to update icon
chrome.runtime.onMessage.addListener((msg, sender, sendResponse) => {
if (msg.action === 'updateIcon') {
setPowerIcon(msg.value);
} else if (msg.action === 'getStorage') {
getCachedStorage().then((res) => {
sendResponse(res)
});
return true;
}
});
// Listen for browser starting, to set initial icon state
chrome.runtime.onStartup.addListener(() => {
chrome.storage.local.get({ 'power': 'on' }, (item) => {
setPowerIcon(item.power);
});
});
// Listen for changes to stored data, and updated our cached data
chrome.storage.onChanged.addListener(() => {
updateCachedStorage();
})
// Listen for extension installed/updating
chrome.runtime.onInstalled.addListener(async (detail) => {
// Set initial icon state
chrome.storage.local.get({ 'power': 'on' }, (item) => {
setPowerIcon(item.power);
});
// If new install, open settings with starter guide
if (detail.reason === 'install') {
chrome.tabs.create({ url: 'pages/settings/index.html?newinstall=true' });
}
// If update, open changelog if setting is enabled
chrome.storage.sync.get({ 'openChangelog': 'off' }, (item) => {
if (item.openChangelog === 'on' && detail.reason === 'update') {
chrome.tabs.create({ url: 'https://getindie.wiki/changelog/?updated=true', active: false });
}
});
// Temporary functions for 3.0 migration
if (detail.reason === 'update') {
commonFunctionMigrateToV3();
}
});
function setPowerIcon(status) {
const manifestVersion = chrome.runtime.getManifest().manifest_version;
if (status === 'on') {
if (manifestVersion === 2) {
chrome.browserAction.setIcon({ path: "/images/logo-128.png" });
} else {
chrome.action.setIcon({ path: "/images/logo-128.png" });
}
} else {
if (manifestVersion === 2) {
chrome.browserAction.setIcon({ path: "/images/logo-off.png" });
} else {
chrome.action.setIcon({ path: "/images/logo-off.png" });
}
}
}
function redirectToBreezeWiki(storage, tabId, url) {
function processRedirect(host) {
// Extract article from URL
const urlFormatted = new URL(url);
urlFormatted.search = '';
const subdomain = urlFormatted.hostname.split(".")[0];
const article = String(urlFormatted).split('fandom.com/wiki/')[1].replaceAll('%20', '_');
// Perform redirect
if (article) {
chrome.tabs.update(tabId, { url: host + '/' + subdomain + '/wiki/' + article });
} else {
chrome.tabs.update(tabId, { url: host + '/' + subdomain });
}
// Increase BreezeWiki stat count
chrome.storage.sync.set({ 'countBreezeWiki': (storage.countBreezeWiki ?? 0) + 1 });
if ((storage.notifications ?? 'on') === 'on') {
// Notify that user is being redirected to BreezeWiki
let notifID = 'independent-wiki-redirector-notification-' + Math.floor(Math.random() * 1E16);
chrome.notifications.create(notifID, {
"type": "basic",
"iconUrl": 'images/logo-48.png',
"title": "You've been redirected to BreezeWiki!",
"message": "Indie Wiki Buddy has sent you to BreezeWiki for a cleaner, ad-free experience on Fandom."
});
// Self-clear notification after 6 seconds
setTimeout(() => { chrome.notifications.clear(notifID); }, 6000);
}
}
if (url.includes('fandom.com/wiki/') && !url.includes('fandom=allow')) {
if (!(storage.breezewikiHost ?? null)) {
fetch('https://bw.getindie.wiki/instances.json')
.then((response) => {
if (response.ok) {
return response.json();
}
throw new Error('Indie Wiki Buddy failed to get BreezeWiki data.');
}).then((breezewikiHosts) => {
breezewikiHosts = breezewikiHosts.filter(host =>
chrome.runtime.getManifest().version.localeCompare(host.iwb_version,
undefined,
{ numeric: true, sensitivity: 'base' }
) >= 0
);
// Check if BreezeWiki's main site is available
let breezewikiMain = breezewikiHosts.filter(host => host.instance === 'https://breezewiki.com');
if (breezewikiMain.length > 0) {
chrome.storage.sync.set({ 'breezewikiHost': breezewikiMain[0].instance });
} else {
// If BreezeWiki.com is not available, set to a random mirror
try {
chrome.storage.sync.set({ 'breezewikiHost': breezewikiHosts[Math.floor(Math.random() * breezewikiHosts.length)].instance });
} catch (e) {
console.log('Indie Wiki Buddy failed to get BreezeWiki data: ' + e);
}
}
chrome.storage.sync.set({ 'breezewikiHostOptions': breezewikiHosts });
chrome.storage.sync.set({ 'breezewikiHostFetchTimestamp': Date.now() });
processRedirect(host);
}).catch((e) => {
console.log('Indie Wiki Buddy failed to get BreezeWiki data: ' + e);
chrome.storage.sync.set({ 'breezewikiHost': 'https://breezewiki.com' });
});
} else {
if (storage.breezewikiHost === 'CUSTOM') {
processRedirect(storage.breezewikiCustomHost || 'https://breezewiki.com');
} else {
processRedirect(storage.breezewikiHost);
}
}
}
}
async function main(url, tabId) {
let storage = await getCachedStorage();
if ((storage.power ?? 'on') === 'on') {
let crossLanguageSetting = storage.crossLanguage || 'off';
let matchingSite = await commonFunctionFindMatchingSite(url, crossLanguageSetting);
if (matchingSite) {
// Get user's settings for the wiki
let settings = await commonFunctionDecompressJSON(storage.wikiSettings) || {};
let id = matchingSite['id'];
let siteSetting = settings[id] || storage.defaultWikiAction || 'alert';
// Check if redirects are enabled for the site
if (siteSetting === 'redirect') {
let newURL = commonFunctionGetNewURL(url, matchingSite);
// Perform redirect
chrome.tabs.update(tabId, { url: newURL });
// Increase redirect count
chrome.storage.sync.set({ 'countRedirects': (storage.countRedirects ?? 0) + 1 });
// Notify if enabled
if ((storage.notifications ?? 'on') === 'on') {
// Notify that user is being redirected
let notifID = 'independent-wiki-redirector-notification-' + Math.floor(Math.random() * 1E16);
chrome.notifications.create(notifID, {
"type": "basic",
"iconUrl": 'images/logo-48.png',
"title": "You've been redirected!",
"message": "Indie Wiki Buddy has sent you from " + matchingSite['origin'] + " to " + matchingSite['destination']
});
// Self-clear notification after 6 seconds
setTimeout(() => { chrome.notifications.clear(notifID); }, 6000);
}
} else if ((storage.breezewiki ?? 'off') === 'on' || (storage.breezewiki ?? 'off') === 'redirect') {
redirectToBreezeWiki(storage, tabId, url);
}
} else if ((storage.breezewiki ?? 'off') === 'on' || (storage.breezewiki ?? 'off') === 'redirect') {
redirectToBreezeWiki(storage, tabId, url);
}
}
}