-
Notifications
You must be signed in to change notification settings - Fork 19
/
content.js
301 lines (232 loc) · 6.2 KB
/
content.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
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
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
var rootEl
function $ (v, c) {
return (c || document).querySelector(v)
}
function $$ (v, c) {
return [].slice.call((c || document).querySelectorAll(v))
}
function execScript (content) {
var script = document.createElement('script')
script.textContent = 'try{' + content + '}catch(e){}'
document.head.appendChild(script)
script.remove()
}
function detectCSS (content) {
var type
var style = document.createElement('style')
style.textContent = content
document.head.appendChild(style)
if (style.sheet.rules.length) {
type = 'css'
}
style.remove()
return type
}
function App () {
this.init()
}
App.prototype = {
constructor: App,
init: function () {
var self = this
var contentPromise = new Promise(function (resolve, reject) {
function onLoad () {
rootEl = document.documentElement
var content
var body = document.body || {children: []}
var children = body.children
var pre = children[0]
if (children.length === 0) {
content = body.textContent
}
if (pre && pre.nodeName === 'PRE'
&& pre.getAttribute('style')) {
content = pre.textContent
}
if (!content || !content.trim()) {
reject()
return chrome.runtime.sendMessage({
action: 'clean'
})
}
resolve(content)
}
if (document.readyState === 'complete') {
onLoad()
} else {
document.addEventListener('DOMContentLoaded', onLoad)
}
})
Promise.all([
contentPromise,
new Promise(function (resolve, reject) {
chrome.storage.sync.get(function (options) {
self.options = options
if (!options.enabled) {
return reject()
}
resolve(options)
})
}),
new Promise(function (resolve) {
chrome.runtime.sendMessage({
action: 'request'
}, function (request) {
self.request = request
resolve(request)
})
})
]).then(function (result) {
var content = result[0]
var options = result[1]
var request = result[2]
self.content = content
if (!self.parseContent()) {
return
}
if (request.type == 'markdown') {
options.theme = 'markdown'
}
self.sendPrettyMsg()
}).catch(function (e) {
// ignore promise exception
})
self.addEvents()
},
loading: function () {
var options = this.options
var classList = [
'prism-pretty',
'prism-pretty-spinner',
'prism-pretty-' + options.theme
]
classList.forEach(function (cls) {
rootEl.classList.add(cls)
})
},
unloading: function () {
var options = this.options
var classList = [
'prism-pretty',
'prism-pretty-spinner',
'prism-pretty-' + options.theme
]
classList.forEach(function (cls) {
rootEl.classList.remove(cls)
})
},
addEvents: function () {
var self = this
chrome.storage.onChanged.addListener(function () {
if (rootEl.classList.contains('prism-pretty')) {
location.reload(true)
}
})
chrome.runtime.onMessage.addListener(function (request) {
var action = request.action
if (action === 'prettyDocument') {
if (rootEl.classList.contains('prism-pretty')) {
location.reload(true)
return
}
self.request.type = 'html'
// pretty html
self.sendPrettyMsg()
}
})
},
parseContent: function () {
var content = this.content
var options = this.options
var request = this.request
var type = request.type
var types = options.formatTypes
var url = new URL(request.url)
if (/\.(md|markdown)$/i.test(url.pathname) && (url.protocol === 'file:')) {
type = 'markdown'
}
if (/\.(js|jsx)$/i.test(url.pathname)) {
type = 'js'
}
if (!type) {
try {
JSON.parse(content)
type = 'js'
} catch (e) {
type = detectCSS(content)
}
}
request.type = type
if (!type || !~types.indexOf(type)) {
return
}
return true
},
sendPrettyMsg: function () {
var self = this
var content = self.content
var options = self.options
var request = self.request
self.loading()
chrome.runtime.sendMessage(Object.assign({
action: 'prettify',
content: content,
options: options,
language: navigator.language
}, request), function (response) {
if (!response) {
return self.unloading()
}
var title = document.title
var className = 'prism-pretty'
className += ' pretty-theme-' + options.theme
className += ' pretty-size-' + options.fontSize
var meta = '<meta name="viewport" content="width=device-width,initial-scale=1">'
var fontSrc = chrome.runtime.getURL('css/droid-sans-mono.woff2')
var fontStyle = '@font-face{font-family:"Droid Sans Mono";src:url("fontSrc") format("woff2");}'
fontStyle = '<style>' + fontStyle.replace('fontSrc', fontSrc) + '</style>'
rootEl.innerHTML = '<head>' + meta + fontStyle + '</head><body>' + response + '</body>'
rootEl.className = className
if (title) {
document.title = 'Prism Pretty: ' + title
}
// headers fade
var headerEl = $('.request-headers')
if (headerEl) {
setTimeout(function () {
headerEl.style.opacity = 0
}, 3000)
}
// preview css
var wrap
if (wrap = $('.preview-wrap')) {
var script = $('script', wrap)
if (script) {
execScript(script.textContent)
script.remove()
}
}
// markdown hash restore
if (request.type === 'markdown') {
var hash = location.hash.slice(1)
if (!hash) {
return
}
var anchors = $$('.anchor')
var anchor
anchors.some(function (a) {
if (a.id === hash) {
anchor = a
return true
}
})
if (anchor) {
setTimeout(function () {
window.scrollTo(0, anchor.getBoundingClientRect().top - 10)
})
}
}
})
}
}
new App()