-
Notifications
You must be signed in to change notification settings - Fork 1
/
background.js
77 lines (63 loc) · 1.92 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
// idea for toggle active from https://github.com/cielavenir/ctouch/issues/1
var active = true;
let habra = '/geekr.vercel.app/post/$2/'
// regex here: https://regex101.com/r/JZ46fx
const regex = {
// https://habr.com/ru/post/(493192)(/#comments)
post: /\/(m?\.?habr\.com|habra\.js\.org)\/.+\/([0-9]{1,})(\/?.{1,})?/,
// https://*.habr.com/ru/post
sub_domain: /[^\m]{0,}\.habr\.com\/.+\/([0-9]{1,})(\/?.{1,})/,
// /#(comments) and /#(comment_22501886)
comment: /^\/#(comment.+)/,
sandbox: /sandbox/,
}
chrome.webRequest.onBeforeRequest.addListener(
function(details) {
if (!active || regex.sub_domain.test(details.url) || regex.sandbox.test(details.url)) {
return;
}
let redirect = details.url.replace(regex.post, habra)
let maybeComment = details.url.match(regex.post)
if (regex.comment.test(maybeComment) && maybeComment) {
redirect += 'comments' + maybeComment[3]
}
return {redirectUrl: redirect};
},
{
urls: ["*://*.habr.com/*", "*://*.habra.js.org/*"],
types: ["main_frame", "sub_frame"]//, "stylesheet", "script", "image", "object", "xmlhttprequest", "other"]
},
["blocking"]
);
function loadOptions(callback)
{
chrome.storage.local.get('activeStatus', function(data) {
if (data.activeStatus === undefined) //at first install
{
data.activeStatus = true;
saveOptions();
}
active = data.activeStatus;
if (callback != null)
callback();
});
}
function saveOptions()
{
chrome.storage.local.set({ activeStatus: active });
}
function updateUI()
{
// console.log("updateUI end, active = " + active);
var str = active? "Redirector active, click to deactivate": "Redirector disabled, click to activate";
chrome.browserAction.setTitle({title:str});
chrome.browserAction.setIcon({path:active?'icons/habra.png':'icons/disabled.png'})
}
function ToggleActive()
{
active = !active;
saveOptions();
updateUI();
}
loadOptions(updateUI);
chrome.browserAction.onClicked.addListener(ToggleActive);