forked from morganstedmanms/JoJoWikiRedirector2.0
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbackground.js
54 lines (46 loc) · 2.56 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
// Simple extension to redirect all requests to JoJo Fandom to JoJo Wiki
(function(){
'use strict';
let isPluginDisabled = false; // Variable storing whether or not the plugin is disabled.
let storage = chrome.storage; // Make sure we have a storage API.
const WIKIA_REGEX = /^jojo\.(wikia|fandom)\.com$/i; // Used to match the domain of the old wikia/fandom to make sure we are redirecting the correct domain.
// Listen to before anytime the browser attempts to navigate to the old Wikia/Fandom sites.
chrome.webNavigation.onBeforeNavigate.addListener(
function(info) {
if(isPluginDisabled) { // Ignore all navigation requests when the extension is disabled.
console.log("JoJo Fandom intercepted, ignoring because plugin is disabled.");
return;
}
// Create a native URL object to more easily determine the path of the url and the domain.
const url = new URL(info.url);
const isWikia = WIKIA_REGEX.test(url.host); // Check to ensure the redirect is occurring on either the fandom/wikia domain.
// If domain isn't subdomain of wikia.com, ignore, also if it's not in the redirect filter
if (!isWikia) return;
// Generate new url
const host = 'jojowiki';
const redirectUrl = `https://${host}.com${url.pathname.replace(/^\/wiki\//i,"/")}`; // Create the redirect URL
console.log(`JoJo Fandom intercepted: ${info.url}\nRedirecting to ${redirectUrl}`);
// Redirect the old wikia request to new wiki
chrome.tabs.update(info.tabId,{url:redirectUrl});
});
function updateIcon(){
// Change the icon to match the state of the plugin.
chrome.action.setIcon({ path: isPluginDisabled?"icon32_black.png":"icon32.png" });
}
chrome.storage.local.get(['isDisabled'],(result)=>{
// Get the initial condition of whether or not the extension is disabled
isPluginDisabled= result ? result.isDisabled : false;
updateIcon(); // Update icon to match new state
});
// Anytime the state of the plugin changes, update the internal state of the background script.
chrome.storage.onChanged.addListener(
function(changes, areaName) {
// If isDisabled changed, update isPluginDisabled
if(changes["isDisabled"]!==undefined && changes["isDisabled"].newValue!=changes["isDisabled"].oldValue) {
console.log(`JoJo Fandom Redirector is now ${changes["isDisabled"].newValue?'disabled':'enabled'}`);
isPluginDisabled=changes["isDisabled"].newValue;
updateIcon();
}
}
);
})();