-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbackground.js
68 lines (62 loc) · 3.47 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
"use strict";
// these dicks would redirect you to the main page if you visited the link directly
// so you have to do extra clicks just to get to the login page
// so they count that you will read their articles and watch their ads
// in a webapp we all paid for, disgusting
function appendReferer(e) {
e.requestHeaders.push({name: "Referer", value: "https://portal.librus.pl/rodzina"});
return {requestHeaders: e.requestHeaders};
}
let appendReferer_extraInfoSpec = ["blocking", "requestHeaders"];
if (chrome.webRequest.OnBeforeSendHeadersOptions.hasOwnProperty('EXTRA_HEADERS')) appendReferer_extraInfoSpec.push('extraHeaders'); // Chrome needs this apparently...
browser.webRequest.onBeforeSendHeaders.addListener(appendReferer,
{urls: [
"https://portal.librus.pl/rodzina/synergia/loguj", "https://portal.librus.pl/szkola/synergia/loguj",
"https://portal.librus.pl/rodzina/synergia/loguj#*", "https://portal.librus.pl/szkola/synergia/loguj#*"
]},
appendReferer_extraInfoSpec
);
browser.webRequest.onBeforeRequest.addListener(e => {
//let redirectUrl = "data:application/json;charset=utf-8," + encodeURIComponent('{}'); // lol that triggers CORS (and only in Firefox apparently), but we don't really care, we just need to block the response
//return {redirectUrl};
return {cancel: true};
},
{urls: ["https://portal.librus.pl/ad*"]},
["blocking"]
);
// this login link would redirect user to the main page, so catch it and redirect directly to the actual login page instead
browser.webRequest.onBeforeRequest.addListener(e => {
let appendee = '';
let moveToUri = '';
if (e.url === 'https://synergia.librus.pl/loguj' && e.originUrl && e.originUrl.startsWith('https://synergia.librus.pl/')) {
// this will only work in Firefox
moveToUri = e.originUrl.replace('https://synergia.librus.pl', '');
}
else if (e.url && e.url.startsWith('https://synergia.librus.pl/loguj/przenies/')) {
moveToUri = e.url.replace('https://synergia.librus.pl/loguj/przenies', '');
}
moveToUri = moveToUri.replace(/\\\//g, '/');
if (moveToUri
&& moveToUri !== '/rodzic/index' && moveToUri !== '/uczen/index' // we don't care about main page redirect, especially that mixing up "rodzic" and "uczen" would log us out again
&& moveToUri !== '/loguj' && !moveToUri.startsWith('/loguj')
)
appendee = '#' + moveToUri;
return {redirectUrl: 'https://portal.librus.pl/rodzina/synergia/loguj' + appendee};
},
{urls: ["https://synergia.librus.pl/loguj", "https://synergia.librus.pl/loguj/przenies*"]},
["blocking"]
);
// redirect to the login page directly, it is the same page as the one we redirect from, except that the main page has some introduction crap instead of login page (you can create account from login page too)
browser.webRequest.onBeforeRequest.addListener(e => {
if (e.originUrl === 'https://portal.librus.pl/rodzina/login') return {}; // don't redirect again if user pressed back button on the login page (doesn't work in Chromium -- their fault)
return {redirectUrl: 'https://portal.librus.pl/rodzina/login'};
},
{urls: ["https://portal.librus.pl/rodzina"]},
["blocking"]
);
browser.runtime.onMessage.addListener((request, sender, sendResponse) => {
if (request.contentScriptQuery && request.contentScriptQuery === 'resolveLiblinkFromNetwork') {
return fetch('https://liblink.pl/'+request.liblinkID, {mode: 'cors'}).then(response => response.text());
}
return false;
});