-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathannotate.js
121 lines (113 loc) · 3.53 KB
/
annotate.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
113
114
115
116
117
118
119
120
121
if (String.trim === undefined){
String.trim = function(){
return this.match(/^\s*(.*?)\s*$/)[1]; // tested a bit, this is noticeably the fastest method
};
}
var comments = $("table table:eq(2) > tbody > tr");
comments.addClass("comment");
comments.each(function(i, node){
$=jQuery;
if ($("table tr a[id^=up]", node).length > 0){
$("table tr a[id^=up]", node).click((function(comment){
return function(){
$(comment).removeClass("notvoted").addClass('voted');
}
})(node));
$("table tr a[id^=down]", node).click((function(comment){
return function(){
$(comment).removeClass("notvoted").addClass('voted');
}
})(node));
$(node).addClass("notvoted");
} else {
$(node).addClass("voted");
}
});
var headers = comments.find("span.comhead");
var commentData = [];
headers.each(function(i, node){
node = $(node);
var score = $("span[id^=score_]", node);
var commenter = $("a[href^=user]", node);
var replacement = $("<span>").addClass("comhead");
score.addClass("score");
replacement.append(score);
replacement.append($("<span>").addClass("between").text(node.contents()[0].data));
commenter.addClass("user");
replacement.append(commenter);
replacement.append($("<span>").addClass("time").text(node.contents()[1].data));
replacement.append(node.contents()[2]);
while (node.contents().length > 2){
replacement.append(node.contents()[2]);
}
node.replaceWith(replacement);
});
/** CSS manipulation based on rules - from http://www.hunlock.com/blogs/Totally_Pwn_CSS_with_Javascript
* Modified to:
* Correctly handle cases where there are no rules (the long `for` line)
* Prioritize rule insertion to try W3C-compliant first
* Clean up global scope a bit
* Add & populate a rule in one step
* Note that, in some browsers, rules are not accessible if running through file:// !
**/
var CSSRule = {
get: function getCSSRule(ruleName, deleteFlag) {
ruleName = ruleName.toLowerCase();
if (document.styleSheets) {
for (var i = 0; i < document.styleSheets.length; i++) {
var styleSheet = document.styleSheets[i];
var cssRule = false;
for (var ii = 0; ii < (styleSheet.cssRules ? styleSheet.cssRules.length : (styleSheet.rules ? styleSheet.rules.length : 0)); ii++) {
if (styleSheet.cssRules) {
cssRule = styleSheet.cssRules[ii];
} else {
cssRule = styleSheet.rules[ii];
}
if (cssRule) {
if (cssRule.selectorText.toLowerCase() === ruleName) {
if (deleteFlag === 'delete') {
if (styleSheet.cssRules) {
styleSheet.deleteRule(ii);
} else {
styleSheet.removeRule(ii);
}
return true;
} else {
return cssRule;
}
}
}
}
}
}
return false;
}
, add: function addCSSRule(ruleName, rule) {
if (document.styleSheets) {
if (!this.get(ruleName)) {
var len = document.styleSheets.length - 1;
if (document.styleSheets[len].insertRule) {
document.styleSheets[len].insertRule(ruleName + (rule ? "{" + rule + "}" : "{}"), document.styleSheets[len].cssRules.length);
} else {
document.styleSheets[len].addRule(ruleName, (rule ? "{" + rule + "}" : "{}"));
}
}
}
return this.get(ruleName);
}
, remove: function killCSSRule(ruleName) {
return this.get(ruleName, 'delete');
}
};
chrome.extension.onRequest.addListener( function(request, sender, sendResponse) {
if (request.users){
CSSRule.remove(".notvoted .user");
}
if (request.scores){
CSSRule.remove(".notvoted .score");
}
if (request.users && request.scores){
CSSRule.remove(".notvoted .between");
}
sendResponse({});
});