This repository has been archived by the owner on Jun 3, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 32
/
content_script.coffee
executable file
·146 lines (118 loc) · 4.83 KB
/
content_script.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
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
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
settings = false
enableLog = false
chrome.extension.sendRequest {method: "getSettingsWithAction"}, (response) ->
settings = response.settings;
if syntaxtic.windowLoaded
enableLog && console.log 'Loaded'
syntaxtic.doHighlight()
else
enableLog && console.log 'Issuing onload'
window.onload = () ->
syntaxtic.windowLoaded = true
syntaxtic.doHighlight()
syntaxtic =
windowLoaded : false,
doHighlight : () ->
return if not settings
hasHtmlContentType = () ->
enableLog && console.log 'has html content?'
result = false
metaTags = document.getElementsByTagName('meta')
enableLog && console.log 'Meta Tags:', metaTags
for tag in metaTags
enableLog && console.log 'Tag:', tag, metaTags[tag]
httpEquiv = tag.httpEquiv
content = tag.content
continue if httpEquiv is 'undefined'
if httpEquiv.toLowerCase() == "content-type" && content.toLowerCase().match("html")
result = true
break
return result
highlight = () ->
if document.body.innerHTML.match("003ew0hdafa1119dadfa39aje") # this is gross
return
if document.body.firstChild != null && document.getElementsByTagName('pre')[0] == document.body.firstChild
classString = "brush: #{window.brushAlias}"
if settings.disableQuickCode
classString += "; quick-code: false"
document.body.innerHTML = """
<!-- 003ew0hdafa1119dadfa39aje -->
<script type="text/syntaxhighlighter" class="#{classString}">
<![CDATA[#{document.body.firstChild.innerHTML}]]>
</script>
"""
css2 = document.createElement("link")
css2.href = chrome.extension.getURL("styles/" + settings.theme)
css2.type = "text/css"
css2.rel = "stylesheet"
document.head.appendChild(css2)
css3 = document.createElement("style")
css3.appendChild(document.createTextNode(".syntaxhighlighter .toolbar {display:none}"))
document.head.appendChild(css3)
SyntaxHighlighter.highlight()
beautifyCsv = () ->
strData = document.body.firstChild.innerHTML
csvArray = CsvToArray(strData, ",")
maxColLengthsHash = [1..csvArray[0].length].map (i) -> -1
csvArrayDecoded = [1..csvArray.length].map (i) -> []
for firstDimension, i in csvArray
for orig, j in firstDimension
strDecode = html_entity_decode(orig)
csvArrayDecoded[i][j] = strDecode
if strDecode.length > maxColLengthsHash[j]
maxColLengthsHash[j] = strDecode.length
newCsvData = ""
for firstDimension, k in csvArray
for secondDimension, l in firstDimension
newCsvData += " " for m in [0..maxColLengthsHash[l] - csvArrayDecoded[k][l].length]
newCsvData += secondDimension
if(l + 1 == firstDimension.length)
newCsvData += '\n'
else
newCsvData += ", "
document.body.firstChild.innerHTML = newCsvData
window.brushAlias = "plain"
checkForObjectiveC = ->
strData = document.body.innerHTML;
if strData.match(/(@interface|@protocol|@INTERFACE|@PROTOCOL)/)?
return "objc"
else
return "cpp"
changeFontSize = () ->
styleElement = document.createElement('style')
styleElement.type = 'text/css'
styleElement.id = 'fontOverride'
document.getElementsByTagName('head')[0].appendChild(styleElement)
newNode = document.createTextNode(".syntaxhighlighter, .syntaxhighlighter code, .syntaxhighlighter div {\n
font-size: #{ settings.fontSize } !important;\n
font-family: '#{ settings.fontFamily }', monospace !important;\n
line-height: #{ settings.lineHeight }em !important;\n
}")
styleElement.appendChild(newNode)
applyPageSpecificSettings = () ->
if(settings.gutterBlacklist.indexOf(document.location.href) > -1)
loadScript(chrome.extension.getURL("toggle_gutter.js"));
if(settings.highlightBlacklist.indexOf(document.location.href) > -1)
loadScript(chrome.extension.getURL("toggle_highlight.js"));
loadScript = (url, callback) ->
head = document.getElementsByTagName('head')[0];
script = document.createElement('script');
script.type = 'text/javascript';
script.src = url;
script.onreadystatechange = callback;
script.onload = callback;
head.appendChild(script);
# #########################
# MAIN
# #########################
enableLog && console.log 'Main'
if window.brushAlias? && window.brushAlias != "" && !hasHtmlContentType()
if(window.brushAlias == 'csv')
beautifyCsv()
if(window.brushAlias == 'cHeader')
window.brushAlias = checkForObjectiveC()
highlight()
changeFontSize()
applyPageSpecificSettings()
window.onload = () ->
syntaxtic.windowLoaded = true