forked from josePhoenix/Reddit-Rage-Faces
-
Notifications
You must be signed in to change notification settings - Fork 0
/
contentscript.js
72 lines (64 loc) · 2.3 KB
/
contentscript.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
function RAGEbuildSelector(kw) {
return 'a[href="http://ra.ge/' + kw + '"]';
}
function RAGEinjectCSS(styleScript) {
var rageStyles = '';
var rageIndex = styleScript.indexOf('a[href=');
var keyWords = [];
while (rageIndex < styleScript.length) {
// Copy all a[href styles, until the ending bracket.
if (styleScript.substring(rageIndex, rageIndex + 7) === 'a[href=') {
var theseKeywords = [];
while (rageIndex < styleScript.length && styleScript.charAt(rageIndex) !== '{') {
if (styleScript.substring(rageIndex, rageIndex + 7) !== 'a[href=') {
rageStyles = rageStyles + styleScript.charAt(rageIndex);
rageIndex += 1;
}
else {
var keyword = '';
rageIndex += 9;
while (rageIndex < styleScript.length && styleScript.charAt(rageIndex) !== '"') {
keyword = keyword + styleScript.charAt(rageIndex);
rageIndex += 1;
}
//console.log("RAGES: found keyword: " + keyword);
theseKeywords.push(keyword);
rageIndex += 2; /* '"]'*/
rageStyles = rageStyles + RAGEbuildSelector(keyword);
}
}
if (theseKeywords.length > 1) {
theseKeywords.forEach(function (s) {
keyWords.push(s);
});
}
// Rageface, copy to <style> while not }
while (rageIndex < styleScript.length && styleScript.charAt(rageIndex) !== '}') {
rageStyles = rageStyles + styleScript.charAt(rageIndex);
rageIndex += 1;
}
rageStyles = rageStyles + '} ';
rageIndex += 1;
}
else {
// Not a rage face, find one!
rageIndex += 1;
}
}
var allRageSelector = '';
keyWords.forEach(function (kw) {
allRageSelector = allRageSelector + RAGEbuildSelector(kw) + ', ';
});
rageStyles = rageStyles + '\n' + allRageSelector.slice(0, allRageSelector.length - 2) + '{\nfont-size: 0;\ncolor: white;}';
// End this with </style>
// rageStyles = rageStyles + ' </style>';
// document.head.innerHTML = document.head.innerHTML + rageStyles;
var styles = document.createElement('style');
styles.setAttribute('type', 'text/css');
styles.setAttribute('title', 'ragefaces');
styles.innerHTML = rageStyles;
var headNode = document.getElementsByTagName('head')[0];
headNode.appendChild(styles);
// Whew, done!
}
chrome.extension.sendMessage({'action' : 'fetchRageCSS'}, RAGEinjectCSS);