-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathbackground.coffee
94 lines (87 loc) · 1.77 KB
/
background.coffee
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
localVersion = [
"1.2.1"
"1.2.2"
"1.2.3"
"1.2.4"
"1.2.5"
"1.2.6"
"1.3.1"
"1.3.2"
"1.4.1"
"1.4.2"
"1.4.3"
"1.4.4"
"1.5.1"
"1.5.2"
"1.6.1"
"1.6.2"
"1.6.3"
"1.6.4"
"1.7.0"
"1.7.1"
"1.7.2"
"1.8.0"
"1.8.1"
"1.8.2"
"1.8.3"
"1.9.0"
"1.9.1"
"1.10.0"
"1.10.1"
"1.10.2"
"1.11.0"
"1.11.1"
"1.12.4"
"2.0.0",
"2.0.1",
"2.0.2",
"2.0.3",
"2.1.0",
"2.1.1",
"2.2.1",
"2.2.2",
"2.2.3",
"2.2.4",
"3.0.0",
"3.1.0"
]
filter =
urls: [
"http://*/*.js",
"https://*/*.js"
]
whiteLists = [
'g.alicdn.com'
]
notInWhiteList = (url)->
for item in whiteLists
if url.indexOf(item) isnt -1
return false
return true
handler = (details)->
jQueryVersion = execJQueryVersion(details.url)
if jQueryVersion
localCache = getLocalJsFile(jQueryVersion)
if localCache and notInWhiteList(details.url)
return {
redirectUrl: localCache
}
###
get jquery version if url is a valid jquery
@return {String} jquery version, empty if is not a valid jquery
###
execJQueryVersion = (originalURL)->
if originalURL.toLowerCase().indexOf('jquery') > -1
jQueryVersionRegex = /\d\.\d*\.\d/g
matches = originalURL.match jQueryVersionRegex
return matches[0] if matches?.length is 1
return ""
###
get local jquery file by version
@return {String} local js file, empty if not have one
###
getLocalJsFile = (version)->
if version in localVersion
return chrome.extension.getURL("files/jQuery/jquery-#{version}.min.js")
return ""
chrome.webRequest.onBeforeRequest.addListener handler, filter, ["blocking"]