-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathcontent_script.js
88 lines (71 loc) · 2.21 KB
/
content_script.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
//todo - add hotkey support
var FILTER, SEARCH_REG_EXPR, IMPORT_REG_EXPR, URL_MATCH;
FILTER = [
'yui.yahooapis.com',
'ajax.googleapis.com',
'fonts.googleapis.com',
'ajax.aspnetcdn.com',
'ajax.microsoft.com',
'code.jquery.com',
'use.typekit.net'
];
SEARCH_REG_EXPR = /jcr=\w+/;
IMPORT_REG_EXPR = /(@import[^)]+)/;
URL_MATCH = /url[\s\('"]+([^'"]+)/;
chrome.extension.onMessage.addListener(function(request, sender, sendResponse){
switch (request) {
case 'refresh':
refresh();
break;
}
sendResponse(true);
});
function refresh(){
var i, item, a, search, newUrl, content, parts, update;
search = 'jcr=' + (new Date().getTime()).toString(36);
for (i = 0; i < document.styleSheets.length; i++) {
update = false;
item = document.styleSheets[i];
if (item.ownerNode) {
if (item.href && item.ownerNode.nodeName === 'LINK') {
newUrl = generateNewUrl(item.href, search);
if (newUrl) {
item.ownerNode.href = newUrl;
}
}
else if (item.ownerNode.nodeName === 'STYLE') {
content = item.ownerNode.innerHTML;
parts = content.split(IMPORT_REG_EXPR);
parts.forEach(function(part, key){
var href, matches;
if (IMPORT_REG_EXPR.test(part)) {
matches = part.match(URL_MATCH);
if (matches.length === 2) {
update = true;
parts[key] = part.replace(matches[1], generateNewUrl(matches[1], search));
}
}
});
if (update) {
item.ownerNode.innerHTML = parts.join('');
}
}
}
}
}
/**
* @param url
* @param search
* @return {*}
*/
function generateNewUrl(url, search){
var a = document.createElement('a');
a.href = url;
if (FILTER.indexOf(a.host) === -1) {
a.search = SEARCH_REG_EXPR.test(a.search)
? a.search.replace(/jcr=\w+/, search)
: search;
return a.href;
}
return false;
}