-
Notifications
You must be signed in to change notification settings - Fork 0
/
redirect.js
63 lines (60 loc) · 1.55 KB
/
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
console.log("it's on")
// global options
var hasVPN = true
var cancelNonessential = true
// the RULE below is unstable, use it only if you do not have good VPN
if (!hasVPN) {
chrome.webRequest.onBeforeRequest.addListener(
function (details) {
var oldUrl = details.url
// parse the old one
var mark = 'libs/'
var target = oldUrl.substring(oldUrl.indexOf(mark) + mark.length)
console.log(target)
var parts = target.split('/')
var lib = parts[0]
var ver = parts[1]
var file = parts[2]
console.log(parts)
// bad cases for lib
var newLib = ''
switch (lib) {
case 'angularjs':
newLib = 'angular.js'
break
default:
newLib = lib
}
// bad cases for file
var newFile = ''
switch (file) {
case 'webfont.js':
newFile = 'webfontloader.js'
break
default:
newFile = file
}
var newUrl = 'https://cdnjs.cloudflare.com/ajax/libs/' + newLib + '/' + ver + '/' + newFile
console.log('Switch URL to ' + newUrl)
return { redirectUrl: newUrl }
},
{urls: [
'*://ajax.googleapis.com/*'
]},
['blocking']
)
}
// fonts & analytics are usually not essential for users
if (cancelNonessential) {
chrome.webRequest.onBeforeRequest.addListener(
function (details) {
console.log('Requesting', details.url)
return { cancel: true }
},
{urls: [
'*://*.google-analytics.com/*',
'*://fonts.googleapis.com/*'
]},
['blocking']
)
}