-
-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathredirector.js
112 lines (98 loc) · 3.21 KB
/
redirector.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
/*
* Copyright © 2017–2022 Daniel Aleksandersen <https://www.daniel.priv.no/>
* SPDX-License-Identifier: MIT
* License-Filename: LICENSE
*/
// Bing AMP Viewer
// Convert https://www.bing.com/amp/s/www.example.com/amp/document
// to https://www.example.com/amp/document
// Google AMP Viewer
// Convert https://www.google.com/amp/www.example.com/amp/document
// to http://www.example.com/amp/document
var browserapp = (typeof browser !== 'undefined') ? browser : chrome;
function amp_viewer_redirector(requestDetails)
{
var amp_viewer = new URL(requestDetails.url);
var redirection = amp_viewer.pathname.replace(/^\/amp\/s\//, 'https://').replace(/^\/amp\//, 'http://');
// Google AMP Viewer sometimes URI encode query parameters, handle optimistically
if (!redirection.includes('?') && redirection.includes('%3f'))
{
var uriComponents = redirection.split('%3f');
var uri = uriComponents[0];
// this can make a mess, but it’s not of our making
var query = decodeURIComponent(uriComponents.slice(1).join('%3f'));
redirection = uri.concat('?', query);
}
// Unhandled case: <viewer-url>%f3<page-query>?<google-query>
// It’s impossible to distinguish page-query and google-query, as we never know beforehand if
// the URL has been encoded or not (the escaped question mark can also be in any part of the URL).
//
// Incorrectly handled cases:
// * question mark in path, e.g.
// example.com/is-it-good%f3/sure-why-not
return {
redirectUrl: redirection
};
}
browserapp.webRequest.onBeforeRequest.addListener(
amp_viewer_redirector,
{
urls: [
'https://www.bing.com/amp/*',
'https://www.google.com/amp/*'
]
},
["blocking"]
);
// to http://www.example.com/amp/document
// Google AMP Cache
// Convert https://cdn.ampproject.org/c/s/www.example.com/amp/document
// to https://www.example.com/amp/document
// Convert https://www-example-com.cdn.ampproject.org/c/s/www.example.com/amp/document
// to https://www.example.com/amp/document
// Bing AMP Cache
// Convert https://bing-amp.com/c/s/www.example.com/amp/document
// to https://www.example.com/amp/document
// Convert https://www-example-com.bing-amp.com/c/www.example.com/amp/document
// to http://www.example.com/amp/document
function amp_cache_redirector(requestDetails)
{
var amp_cache = new URL(requestDetails.url);
var redirection = amp_cache.pathname.replace(/^\/c\/s\//, 'https://').replace(/^\/c\//, 'http://');
if (navigator.userAgent.includes('Firefox'))
return {
redirectUrl: redirection
};
}
browserapp.webRequest.onBeforeRequest.addListener(
amp_cache_redirector,
{
urls: [
'https://cdn.ampproject.org/c/*',
'https://*.cdn.ampproject.org/c/*',
'https://bing-amp.com/c/*',
'https://*.bing-amp.com/c/*'
]
},
["blocking"]
);
// Twitter Shortlinks
// Convert https://t.co/abc123?amp=1
// to https://t.co/abc123
function t_co_redirector(requestDetails)
{
var redirection = new URL(requestDetails.url);
redirection.search = '';
return {
redirectUrl: redirection.toString()
};
}
browserapp.webRequest.onBeforeRequest.addListener(
t_co_redirector,
{
urls: [
'*://t.co/*?amp=1'
]
},
["blocking"]
);