-
Notifications
You must be signed in to change notification settings - Fork 10
/
background.coffee
147 lines (129 loc) · 4.27 KB
/
background.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
147
# background.js
enabled = !1
onBeforeSendHeaders = do (types=['application/rdf+xml','text/n3','text/turtle']) ->
(d) ->
return if not enabled
setup = false
for elt in d.requestHeaders when elt.name.toLowerCase() is 'accept'
setup = true
values = elt.value.split ','
for j, i in values
if j.indexOf(';') < 0
values[i] = values[i] + ';q=0.9'
for j, i in types
values.unshift j
elt.value = values.join ','
break
if not setup and d.requestHeaders.push
values = []
for j in types
values.unshift j
d.requestHeaders.push({name: 'Accept', value: values.join ','})
return {requestHeaders: d.requestHeaders}
requests = {}
init = (d) ->
requests[d.requestId] = true
return { cancel: true }
skinnable = (d) ->
return requests[d.requestId] == true
skin = (d) ->
delete requests[d.requestId]
chrome.tabs.update(d.tabId, {
url: chrome.extension.getURL 'skin.html?uri='+encodeURIComponent(d.url),
})
onHeadersReceived = (d) ->
return if not enabled
for header in d.responseHeaders
if header.name.match(/content-type/i) and header.value.match(/\/(n3|rdf|turtle)/)
return init d
onErrorOccurred = (d) ->
if skinnable d
skin d
events = {
'onBeforeSendHeaders': {
callback: onBeforeSendHeaders,
filter: {types: ["main_frame"], urls: ["<all_urls>"]},
extras: ['requestHeaders', 'blocking'],
},
'onHeadersReceived': {
callback: onHeadersReceived,
filter: {types: ["main_frame"], urls: ["<all_urls>"]},
extras: ['responseHeaders', 'blocking'],
},
'onErrorOccurred': {
callback: onErrorOccurred,
filter: {types: ["main_frame"], urls: ["<all_urls>"]},
},
}
install = (api=chrome.webRequest) ->
for event, meta of events
console.log 'setup/addListener: ' + event
if meta.extras
api[event].addListener meta.callback, meta.filter, meta.extras
else
api[event].addListener meta.callback, meta.filter
console.log 'setup: success!'
class Client
constructor: (@port) ->
@hooks = ({api, handler: handler.bind(@), extras} for {api, handler, extras} in Client.hooks)
@id = port.sender.tab.id
for {api, handler, extras} in @hooks
if extras
api.addListener handler, {urls: ['<all_urls>'], tabId: @id}, extras
else
api.addListener handler, {urls: ['<all_urls>'], tabId: @id}
disconnect: ->
for {api, handler} in @hooks
api.removeListener handler
onCompleted: (d) ->
d.eventName = 'onCompleted'
@port.postMessage d
onHeadersReceived: (d) ->
d.eventName = 'onHeadersReceived'
d.statusCode = parseInt d.statusLine.split(' ')[1]
if d.statusCode in [301, 302, 303, 307]
@port.postMessage d
onErrorOccurred: (d) ->
d.eventName = 'onErrorOccurred'
@port.postMessage d
@hooks: [
api: chrome.webRequest.onCompleted
handler: @::onCompleted
extras: ['responseHeaders']
,
api: chrome.webRequest.onHeadersReceived
handler: @::onHeadersReceived
extras: ['responseHeaders']
,
api: chrome.webRequest.onErrorOccurred
handler: @::onErrorOccurred
]
class Server
constructor: (@clientClass=Client) ->
@clients = {}
chrome.extension.onConnect.addListener @onConnect.bind @
onConnect: (port) ->
id = port.portId_
port.onDisconnect.addListener @onDisconnect.bind @
@clients[id] = new @clientClass port
onDisconnect: (port) ->
id = port.portId_
if @clients[id]
@clients[id].disconnect()
delete @clients[id]
server = new Server
setEnabled = ->
enabled = !enabled
if enabled
chrome.browserAction.setBadgeText
text: ''
else
chrome.browserAction.setBadgeText
text: 'OFF'
chrome.extension.onMessage.addListener (message, sender, respond) ->
if message.method in ['disable', 'enable']
do setEnabled
respond
enabled: enabled
do setEnabled
install()