Generalize event page request mechanism.

This commit is contained in:
ccd0 2019-03-08 15:03:29 -08:00
parent 52128775e1
commit c86fadf0f3
2 changed files with 34 additions and 31 deletions

View File

@ -1,28 +1,32 @@
requestID = 0
chrome.runtime.onMessage.addListener (request, sender, sendResponse) ->
if request.responseType is 'arraybuffer'
# Cross-origin image fetching. Need permission.
chrome.permissions.contains
origins: ['*://*/']
, (result) ->
if result
ajax request, sender, sendResponse
else
chrome.permissions.request
origins: ['*://*/']
, ->
ajax request, sender, sendResponse
return true
else
# JSON fetching from non-HTTPS archive.
ajax request, sender, sendResponse
ajax = (request, sender, sendResponse) ->
id = requestID
requestID++
sendResponse id
handlers[request.type] request, (response) ->
chrome.tabs.sendMessage sender.tab.id, {id, data: response}
handlers =
ajax: (request, cb) ->
if request.responseType is 'arraybuffer'
# Cross-origin image fetching. Need permission.
chrome.permissions.contains
origins: ['*://*/']
, (result) ->
if result
ajax request, cb
else
chrome.permissions.request
origins: ['*://*/']
, ->
ajax request, cb
return true
else
# JSON fetching from non-HTTPS archive.
ajax request, cb
ajax = (request, cb) ->
xhr = new XMLHttpRequest()
xhr.open 'GET', request.url, true
xhr.responseType = request.responseType
@ -34,17 +38,17 @@ ajax = (request, sender, sendResponse) ->
response = [new Uint8Array(response)...]
contentType = @getResponseHeader 'Content-Type'
contentDisposition = @getResponseHeader 'Content-Disposition'
chrome.tabs.sendMessage sender.tab.id, {id, status, statusText, response, contentType, contentDisposition}
cb {status, statusText, response, contentType, contentDisposition}
else
chrome.tabs.sendMessage sender.tab.id, {id, status, statusText, response, error: true}
cb {status, statusText, response, error: true}
, false
xhr.addEventListener 'error', ->
chrome.tabs.sendMessage sender.tab.id, {id, error: true}
cb {error: true}
, false
xhr.addEventListener 'abort', ->
chrome.tabs.sendMessage sender.tab.id, {id, error: true}
cb {error: true}
, false
xhr.addEventListener 'timeout', ->
chrome.tabs.sendMessage sender.tab.id, {id, error: true}
cb {error: true}
, false
xhr.send()

View File

@ -1,11 +1,11 @@
<% if (type === 'crx') { %>
eventPageRequest = do ->
callbacks = []
chrome.runtime.onMessage.addListener (data) ->
callbacks[data.id] data
delete callbacks[data.id]
(url, responseType, cb, timeout) ->
chrome.runtime.sendMessage {url, responseType, timeout}, (id) ->
chrome.runtime.onMessage.addListener (response) ->
callbacks[response.id] response.data
delete callbacks[response.id]
(params, cb) ->
chrome.runtime.sendMessage params, (id) ->
callbacks[id] = cb
<% } %>
@ -14,7 +14,7 @@ CrossOrigin =
# XXX https://forums.lanik.us/viewtopic.php?f=64&t=24173&p=78310
url = url.replace /^((?:https?:)?\/\/(?:\w+\.)?4c(?:ha|d)n\.org)\/adv\//, '$1//adv/'
<% if (type === 'crx') { %>
eventPageRequest url, 'arraybuffer', ({response, contentType, contentDisposition, error}) ->
eventPageRequest {type: 'ajax', url, responseType: 'arraybuffer'}, ({response, contentType, contentDisposition, error}) ->
return cb null if error
cb new Uint8Array(response), contentType, contentDisposition
<% } %>
@ -125,10 +125,9 @@ CrossOrigin =
ontimeout: -> failure(url)
<% } %>
<% if (type === 'crx') { %>
eventPageRequest url, 'json', (result) ->
eventPageRequest {type: 'ajax', url, responseType: 'json', timeout}, (result) ->
if result.status
success url, result
else
failure url
, timeout
<% } %>