-
Notifications
You must be signed in to change notification settings - Fork 18
/
Copy pathbackground.js
186 lines (169 loc) · 5.56 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
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
// Filter out utm_* query parameters
function clean_utm(requestDetails) {
var url = new URL(requestDetails.url)
//console.log("cleanURL utm_*", url.href);
if (url.search.length > 0) {
var params = url.searchParams;
var new_params = new URLSearchParams(params);
var needs_redirect = false;
for (let p of params.keys()) {
if (p.startsWith("utm_")) {
needs_redirect = true;
new_params.delete(p);
}
}
if (needs_redirect) {
var new_url = new URL(url);
new_url.search = new_params.toString();
/*console.info("Removing utm_* params from url: ",
requestDetails.url, " and redirection to: ",
new_url.href);*/
return {
redirectUrl: new_url.href
}
}
}
return {
}
}
browser.webRequest.onBeforeRequest.addListener(
clean_utm,
{urls: ["<all_urls>"],
types:["main_frame"]},
["blocking"]
);
function clean_amazon(requestDetails) {
var url = requestDetails.url;
let slash_d_index = url.indexOf("/d");
let slash_ref_index = url.indexOf("/ref=", slash_d_index + 2);
if (slash_ref_index > 0 && url.length > slash_ref_index + 1) {
var new_url = url.substring(0, slash_ref_index + 1);
if (new_url != url) { // try to avoid infinite redirect loops that might arise
//console.info("Redirecting from: ", url, " to:", new_url);
return {redirectUrl: new_url};
}
} else {
url = new URL(url);
if (url.search.length > 0) {
url.search = "";
//console.info("Clean url to:", url.href);
return {redirectUrl: url.href};
}
}
}
browser.webRequest.onBeforeRequest.addListener(
clean_amazon,
{urls: [
"*://*.amazon.com/d/*",
"*://*.amazon.ca/d/*",
"*://*.amazon.co.jp/d/*",
"*://*.amazon.co.uk/d/*",
"*://*.amazon.cn/d/*",
"*://*.amazon.de/d/*",
"*://*.amazon.fr/d/*",
"*://*.amazon.in/d/*",
"*://*.amazon.it/d/*",
"*://*.amazon.com.mx/d/*",
"*://*.amazon.com.au/d/*",
"*://*.amazon.com.br/d/*",
"*://*.amazon.com/dp/*",
"*://*.amazon.ca/dp/*",
"*://*.amazon.co.jp/dp/*",
"*://*.amazon.co.uk/dp/*",
"*://*.amazon.cn/dp/*",
"*://*.amazon.de/dp/*",
"*://*.amazon.fr/dp/*",
"*://*.amazon.in/dp/*",
"*://*.amazon.it/dp/*",
"*://*.amazon.com.mx/dp/*",
"*://*.amazon.com.au/dp/*",
"*://*.amazon.com.br/dp/*",
"*://*.amazon.com/gp/aw/d/*",
"*://*.amazon.ca/gp/aw/d/*",
"*://*.amazon.co.jp/gp/aw/d/*",
"*://*.amazon.co.uk/gp/aw/d/*",
"*://*.amazon.cn/gp/aw/d/*",
"*://*.amazon.de/gp/aw/d/*",
"*://*.amazon.fr/gp/aw/d/*",
"*://*.amazon.in/gp/aw/d/*",
"*://*.amazon.it/gp/aw/d/*",
"*://*.amazon.com.mx/gp/aw/d/*",
"*://*.amazon.com.au/gp/aw/d/*",
"*://*.amazon.com.br/gp/aw/d/*",
"*://*.amazon.com/*/dp/*",
"*://*.amazon.ca/*/dp/*",
"*://*.amazon.co.jp/*/dp/*",
"*://*.amazon.co.uk/*/dp/*",
"*://*.amazon.cn/*/dp/*",
"*://*.amazon.de/*/dp/*",
"*://*.amazon.fr/*/dp/*",
"*://*.amazon.in/*/dp/*",
"*://*.amazon.it/*/dp/*",
"*://*.amazon.com.mx/*/dp/*",
"*://*.amazon.com.au/*/dp/*",
"*://*.amazon.com.br/*/dp/*",
"*://*.amazon.com/gp/product/*",
"*://*.amazon.ca/gp/product/*",
"*://*.amazon.co.jp/gp/product/*",
"*://*.amazon.co.uk/gp/product/*",
"*://*.amazon.cn/gp/product/*",
"*://*.amazon.de/gp/product/*",
"*://*.amazon.fr/gp/product/*",
"*://*.amazon.in/gp/product/*",
"*://*.amazon.it/gp/product/*",
"*://*.amazon.com.mx/gp/product/*",
"*://*.amazon.com.au/gp/product/*",
"*://*.amazon.com.br/gp/product/*",
], types: ["main_frame"]},
["blocking"]
);
function remove_searchparams(requestDetails) {
var url = new URL(requestDetails.url)
if (url.search.length > 0) {
url.search = "";
//console.info("Clean url to:", url.href);
return {redirectUrl: url.href};
}
}
browser.webRequest.onBeforeRequest.addListener(
remove_searchparams,
{
urls: [
"*://*.aliexpress.com/item/*.html*",
"*://*.aliexpress.com/store/product/*.html*",
], types: ["main_frame"]
}, ["blocking"]
);
function build_redirect_to_query_param(query_param_name){
const redirect_to_get_param = function(requestDetails){
const search_params = new URLSearchParams(new URL(requestDetails.url).search);
const real_url_from_param = search_params.get(query_param_name);
if (real_url_from_param){
// console.log('Redirecting to ' + real_url_from_param);
return {redirectUrl: real_url_from_param};
}
}
return redirect_to_get_param;
}
const urls_to_param_mappers = [
{
urls: ["*://l.facebook.com/*"],
param_name: 'u'
},
{
urls: ["*://out.reddit.com/*"]
},
{
urls: ["*://steamcommunity.com/linkfilter/*"]
}
];
urls_to_param_mappers.forEach(function(listenerConfig) {
const param_name = listenerConfig.param_name ? listenerConfig.param_name : 'url';
// console.log('Mapping ' + listenerConfig.urls + ' to param name ' + param_name);
browser.webRequest.onBeforeRequest.addListener(
build_redirect_to_query_param(param_name), {
urls: listenerConfig.urls,
types: ["main_frame"]
}, ["blocking"]
);
});