-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathbackground.js
77 lines (72 loc) · 2.38 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
// Copyright (c) 2016 H@mdi & KC.
var Website = ["ieeexplore.iee",
"link.springer",
"sciencedirect",
"dl.acm",
"onlinelibrary.wiley",
"jama.jamanetwork",
"cpr.sagepub",
"cnu.sagepub.com",
"ehjcimaging.oxfordjournals",
"europace.oxfordjournals.org",
"tandfonline.com",
"biomet.oxfordjournals",
"online.liebertpub",
"nejm.org"];
var Button_state = ["Sci-trick [enabled]", "Sci-trick [disabled]", "icon/enabled.png", "icon/disabled.png"]
function CheckWebsite(tab, changeInfo) {
var arr = tab.url.split("/");
for (i = 0; i < Website.length; i++)
if (arr[2].indexOf(Website[i]) >= 0 && -1 == arr[2].indexOf("sci-hub") && "loading" == changeInfo.status) return !0
}
function initializeDefaultValues() {
console.log("value : " + localStorage.getItem('state'));
null === localStorage.getItem("state") && localStorage.setItem("state", 1);
1 == localStorage.getItem("state") ? (chrome.browserAction.setIcon(
{
path: Button_state[2]
}), chrome.browserAction.setTitle(
{
title: Button_state[0]
})) : (chrome.browserAction.setIcon(
{
path: Button_state[3]
}), chrome.browserAction.setTitle(
{
title: Button_state[1]
}));
}
function ButtonState() {
0 == localStorage.getItem("state") ? (localStorage.setItem("state", 1), chrome.browserAction.setIcon(
{
path: Button_state[2]
}), chrome.browserAction.setTitle(
{
title: Button_state[0]
})) : (localStorage.setItem("state", 0), chrome.browserAction.setIcon(
{
path: Button_state[3]
}), chrome.browserAction.setTitle(
{
title: Button_state[1]
}));
}
chrome.tabs.onUpdated.addListener(function (tabId, changeInfo, tab) {
if (1 == localStorage.getItem("state")) {
origin_url = tab.url;
var arr = origin_url.split("/");
if (CheckWebsite(tab, changeInfo)) {
arr[2] = arr[2] + ".sci-hub.se";
var new_url = arr.join("/");
console.log("Redirecting " + tab.url + " to " + new_url), chrome.tabs.update(tab.id
, {
url: new_url
})
}
}
});
chrome.browserAction.onClicked.addListener(function (e) {
ButtonState()
}), chrome.runtime.onInstalled.addListener(function (e) {
initializeDefaultValues()
});