This repository has been archived by the owner on Jun 21, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 2
/
actual-teddit-redirect.js
91 lines (79 loc) · 3.5 KB
/
actual-teddit-redirect.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
function redirectHandler(request) {
const initialTargetUrl = request.url;
let redirectUrl = "https://teddit.net";
// browser.storage is accessed asynchronously and returns a
// promise for the value
// https://developer.mozilla.org/en-US/docs/Mozilla/Add-ons/WebExtensions/API/storage
let storage_promise = browser.storage.local.get("user_teddit_preference");
// when the promise returns, do the following:
// 1. check for a user defined Teddit instance (from within addon preferences)
// 2. assign a default instance if none
// 3. ensure text protocol
// 4. replace Reddit with Teddit in request string
// 5. assign replaced
return storage_promise.then((storage) => {
let redirectUrl = storage.user_teddit_preference;
if (!redirectUrl) {
redirectUrl = "https://teddit.net"
}
if (redirectUrl.substring(0, 4) != "http") {
redirectUrl = "https://" + redirectUrl;
}
const redirectedTargetUrl = initialTargetUrl.replace(
/^http(?:s)?:\/\/(?:(?:www|old).)?reddit\.com(?:\/)?(.*)/,
redirectUrl.replace(/(\/+)$/, "") + "/$1"
);
// The listener for onBeforeRequest must return a blockingResponse object.
// https://developer.mozilla.org/en-US/docs/Mozilla/Add-ons/WebExtensions/API/webRequest/onBeforeRequest
blockingResponse = {};
// to redirect the request, include a property redirectUrl with
// the value set to the URL to which you want to redirect.
//
// https://developer.mozilla.org/en-US/docs/Mozilla/Add-ons/WebExtensions/API/webRequest/onBeforeRequest
blockingResponse.redirectUrl = redirectedTargetUrl;
return blockingResponse;
});
}
// filter
//
// webRequest.RequestFilter. A filter that restricts the events that will be sent to this listener.
//
// https://developer.mozilla.org/en-US/docs/Mozilla/Add-ons/WebExtensions/API/webRequest/onBeforeRequest#parameters
//
// urls
//
// array of string. An array of match patterns. The listener will
// only be called for requests whose targets match any of the given
// patterns. Only requests made using HTTP or HTTPS will trigger
// events, other protocols (such as data: and file:) supported by
// pattern matching do not trigger events. view-source: requests
// may be matched based on its inner URL.
//
// https://developer.mozilla.org/en-US/docs/Mozilla/Add-ons/WebExtensions/API/webRequest/RequestFilter#type
webRequestFilter = {
urls: [
"*://reddit.com/*",
"*://www.reddit.com/*",
"*://old.reddit.com/*"
]
}
// This event is triggered when a request is about to be made, and
// before headers are available. This is a good place to listen if you
// want to cancel or redirect the request.
//
// To cancel or redirect the request, first include "blocking" in the
// extraInfoSpec array argument to addListener().
//
// browser.webRequest.onBeforeRequest.addListener(
// listener, // function
// filter, // object
// extraInfoSpec // optional array of strings
// )
//
// If you use "blocking", you must have the "webRequestBlocking" API
// permission in your manifest.json.
//
// https://developer.mozilla.org/en-US/docs/Mozilla/Add-ons/WebExtensions/API/webRequest
// https://developer.mozilla.org/en-US/docs/Mozilla/Add-ons/WebExtensions/API/webRequest/onBeforeRequest
browser.webRequest.onBeforeRequest.addListener(
redirectHandler, webRequestFilter, ["blocking"]);