-
Notifications
You must be signed in to change notification settings - Fork 0
/
background.js
102 lines (91 loc) · 3.23 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
"use strict";
// set this cookie to make Reddit not show the mobile site on mobile devices
const noMobileCookie = {
url: "https://.reddit.com",
name: "mweb-no-redirect",
value: "1",
secure: true
};
function onError(error) {
console.error(error);
}
function onSuccess(result) {
console.log(result);
}
/**
* Sets a cookie to prevent reddit.com from loading a mobile-friendly version
* of its site on mobile devices.
*/
function setCookie() {
browser.cookies.getAllCookieStores()
.then((cookieStores) => {
return Promise.all(cookieStores.map((store) => {
// set a cookie per open cookie store (default, private, etc.)
noMobileCookie.storeId = store.id;
return browser.cookies.set(noMobileCookie);
}));
}).then((cookies) => { console.log(cookies); })
.catch(onError);
}
function toggleRedirect(doRedirect) {
if (doRedirect) {
browser.webRequest.onBeforeRequest.addListener(
redirect,
{
urls: [
'https://www.reddit.com/*',
'https://reddit.com/*',
'http://www.reddit.com/*',
'http://reddit.com/*'
]
},
[ 'blocking' ]);
// flush in-memory cache to prevent fetchs from cache w/o redirect
browser.webRequest.handlerBehaviorChanged()
.then(() => { console.log('In-memory cache flush'); })
.catch(onError);
} else {
if (browser.webRequest.onBeforeRequest.hasListener(redirect)) {
browser.webRequest.onBeforeRequest.removeListener(redirect);
}
}
}
/**
* Listener to redirect www.reddit.com requests to old.reddit.com
*/
function redirect(request) {
console.log(request.url);
const redirectUrl = request.url.replace(
/^(?:https|http):\/\/(?:www\.)?reddit\.com/,
'https://old.reddit.com');
return Promise.resolve({ redirectUrl });
}
(function () {
// set the cookie on profile/extension load and for every new tab
browser.runtime.onInstalled.addListener(setCookie);
browser.runtime.onStartup.addListener(setCookie);
browser.tabs.onCreated.addListener(setCookie);
// check if redirecting is enabled, default to true
browser.storage.local.get({ redirect: true })
.then((obj) => { toggleRedirect(obj.redirect); })
.catch(onError);
// enable/disable redirect on storage change
browser.storage.onChanged.addListener((changes) => {
if ('redirect' in changes) {
if (changes.redirect.oldValue !== changes.redirect.newValue) {
toggleRedirect(changes.redirect.newValue);
}
}
});
// open preference page onInstall, once
browser.runtime.onInstalled.addListener(() => {
browser.storage.local.get({ firstInstall: true })
.then((obj) => {
if (obj.firstInstall) return browser.runtime.openOptionsPage();
}).then(() => {
return browser.storage.local.set({ firstInstall: false });
}).then(() => { return browser.storage.local.get(); })
.then((obj) => { console.log(obj); })
.catch(onError);
});
})();