-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathinject.js
More file actions
106 lines (97 loc) · 3.49 KB
/
inject.js
File metadata and controls
106 lines (97 loc) · 3.49 KB
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
var ignore_scripts = [
/(.*\/)highlight(\.pack)?.js/, // highlight.js
/.*(syntaxhighlighter|sh(Core|Brush)).*\.js/]; //SyntaxHighlighter
var highlight_css = null;
var main = function(){
var scripts = document.getElementsByTagName("script");
for (var idx in scripts) {
var s = scripts[idx];
if (s && s.src){
for (var jdx in ignore_scripts) {
if (ignore_scripts[jdx].test(s.src)){
return;
}
}
}
}
var inject_style = function(favorite_style){
if (highlight_css) return;
highlight_css = document.createElement("link");
highlight_css.setAttribute("rel", "stylesheet");
highlight_css.setAttribute("type", "text/css");
highlight_css.setAttribute("href", chrome.extension.getURL("styles/"+favorite_style+".css"));
document.head.appendChild(highlight_css);
}
var is_already_highlighted = function(block){
for (var i = 0; i < block.childNodes.length; i++){
var child = block.childNodes[i];
if (child.nodeType == 3)
continue;
if (child.nodeName == 'BR' || child.nodeName == 'WBR')
continue;
return true;
}
return false;
}
var findCodeStrict = function(pre) {
var code = null;
for (var i=0; i < pre.childNodes.length; ++i){
var node = pre.childNodes[i];
if (node.nodeName == 'CODE'){
if (code) {
return null;
}
code = node;
} else if (!(node.nodeType == 3 && node.nodeValue.search(/^\s+$/) == 0)){
return null;
}
}
return code;
}
add_style_prefix_to_spans = function(element, to_elements, prefix){
if (element.nodeType != 1) return;
var spans = element.getElementsByTagName(to_elements);
for (var i=0; i < spans.length; ++i){
var s = spans[i];
var newClass = "";
for (var j=0; j<s.classList.length; ++j){
if (newClass) newClass += " ";
newClass += prefix + s.classList[j];
}
s.className = newClass;
}
}
init_highlight = function(favorite_style, live_page){
hljs.tabReplace = ' ';
var do_highlighting = function(root){
if (!root.getElementsByTagName) return;
var pres = root.getElementsByTagName('pre');
for (var i = 0; i < pres.length; i++) {
var pre = pres[i];
var code = findCodeStrict(pre);
if (code && !is_already_highlighted(code)){
var result = hljs.highlightBlock(code, hljs.tabReplace);
if (pre.className)
pre.className + " ch-inject";
else
pre.className = "ch-inject";
add_style_prefix_to_spans(pre, 'span', "ch-inject-");
inject_style(favorite_style);
}
}
}
do_highlighting(document);
if (live_page){
document.body.addEventListener("DOMNodeInserted", function(event){
do_highlighting(event.target);
});
}
}
chrome.extension.sendRequest({ask: "page_settings"}, function(response) {
if (response.no_highlight){
return;
}
init_highlight(response.favorite_style, response.defered_highlight);
});
};
main();