forked from L3au/prism-pretty
-
Notifications
You must be signed in to change notification settings - Fork 5
/
background.js
225 lines (188 loc) · 4.59 KB
/
background.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
function toDate (s) {
return Date.parse(s) ? new Date(s).toJSON().replace(/T|\.\d+Z$/g, ' ').trim() : s
}
function getHeader (name, headers) {
var ret = ''
headers.some(function (header) {
if (header.name.toLowerCase() === name.toLowerCase()) {
ret = header.value.toLowerCase()
return true
}
})
return ret
}
function parseUrl (href) {
var url = {}
try {
url = new (window.URL || window.webkitURL)(href)
} catch (e) {}
return url
}
function processRequest (url, headers) {
var mineTypes = {
'text/css': 'css',
'text/javascript': 'js',
'application/javascript': 'js',
'application/x-javascript': 'js',
'application/json': 'js'
}
var type
var contentType = getHeader('content-type', headers)
for (var mineType in mineTypes) {
if (mineTypes.hasOwnProperty(mineType)) {
if (contentType.indexOf(mineType) !== -1) {
type = mineTypes[mineType]
break
}
}
}
var parsedUrl = parseUrl(url)
if (/\.(md|markdown)$/i.test(parsedUrl.pathname)
&& contentType.match(/(plain|markdown)/)) {
type = 'markdown'
}
headers = headers.map(function (header) {
var name = header.name
var value = header.value
switch (name.toLowerCase()) {
case 'content-length':
value = (parseInt(value, 10) / 1024).toFixed(1) + 'K'
break
case 'date':
case 'expires':
case 'last-modified':
value = toDate(value)
break
default:
break
}
return {
name: name,
value: value
}
})
return {
url: url,
type: type,
contentType: contentType,
headers: headers
}
}
function setOptions (options, keep) {
chrome.storage.sync.set(options)
chrome.contextMenus.removeAll()
if (options.enabled) {
chrome.browserAction.setIcon({
path: {
'19': 'icon/icon_48.png',
'38': 'icon/icon_128.png'
}
})
// enable html pretty
if (~options.formatTypes.indexOf('html')) {
chrome.contextMenus.create({
id: 'PrettyPageSource',
type: 'normal',
title: 'Pretty Page Source',
contexts: ['page']
})
}
} else {
chrome.browserAction.setIcon({
path: {
'19': 'icon/icon_disabled.png',
'38': 'icon/icon_disabled.png'
}
})
}
if (!keep) {
chrome.tabs.reload({
bypassCache: false
})
}
}
// click right context menu
chrome.contextMenus.onClicked.addListener(function (info, tab) {
var tabId = tab.id
chrome.tabs.sendMessage(tabId, {
action: 'prettyDocument'
})
})
// init options
chrome.storage.sync.get(function (options) {
if (!options || Object.keys(options).length !== 9) {
options = {
enabled: true,
theme: 'dabblet',
fontSize: '14px',
indent: 4,
customCSS: '',
headers: true,
unicode: true,
bugFree: false,
formatTypes: ['js', 'css', 'markdown']
}
}
setOptions(options, true)
})
// cache requests
var cacheRequests = {}
chrome.webRequest.onHeadersReceived.addListener(function (details) {
var url = details.url
var tabId = details.tabId
var headers = details.responseHeaders
var request = processRequest(url, headers)
cacheRequests[tabId] = request
if (request.type && !request.contentType.includes('charset')) {
headers = headers.map(function (header) {
if (header.name.toLowerCase() === 'content-type') {
header.value = request.contentType + '; charset=utf-8'
}
return header
})
return {responseHeaders: headers}
}
return {}
}, {
urls: ['<all_urls>'],
types: ['main_frame']
}, ['blocking', 'responseHeaders'])
// message listener
chrome.runtime.onMessage.addListener(function (request, sender, sendResponse) {
var url = sender.url
var action = request.action
// clean cache headers
var tab = sender.tab || {}
if (action === 'request') {
sendResponse(cacheRequests[tab.id] || {url: url})
}
try {
delete cacheRequests[tab.id]
} catch (e) {}
if (action === 'prettify') {
var worker = new Worker('worker.js')
worker.onerror = function (e) {
console.error(e)
worker.terminate()
worker = null
sendResponse()
}
worker.onmessage = function (e) {
worker.terminate()
worker = null
sendResponse(e.data)
}
request.language = navigator.language
worker.postMessage(request)
return true
}
})
// popup readme
chrome.runtime.onInstalled.addListener(function (details) {
if (details.reason === 'install') {
chrome.tabs.create({
url: 'https://github.com/L3au/Prism-Pretty/',
active: true
})
}
})