use event page to make HTTP requests from HTTPS pages in Chromium

This commit is contained in:
ccd0 2014-08-30 17:36:17 -07:00
parent 5eb6ba3ec5
commit 40b68a389d
4 changed files with 75 additions and 12 deletions

View File

@ -80,6 +80,7 @@ module.exports = (grunt) ->
'src/General/meta/usestrict.js' 'src/General/meta/usestrict.js'
'tmp-<%= pkg.type %>/script.js' 'tmp-<%= pkg.type %>/script.js'
] ]
'testbuilds/crx<%= pkg.meta.suffix[pkg.channel] %>/eventPage.js': 'tmp-<%= pkg.type %>/eventPage.js'
userscript: userscript:
files: files:
'testbuilds/<%= pkg.name %><%= pkg.meta.suffix[pkg.channel] %>.meta.js': 'src/General/meta/metadata.js' 'testbuilds/<%= pkg.name %><%= pkg.meta.suffix[pkg.channel] %>.meta.js': 'src/General/meta/metadata.js'
@ -108,6 +109,9 @@ module.exports = (grunt) ->
script: script:
src: 'tmp-<%= pkg.type %>/script.coffee' src: 'tmp-<%= pkg.type %>/script.coffee'
dest: 'tmp-<%= pkg.type %>/script.js' dest: 'tmp-<%= pkg.type %>/script.js'
eventPage:
src: 'src/General/eventPage/eventPage.coffee'
dest: 'tmp-<%= pkg.type %>/eventPage.js'
concurrent: concurrent:
build: [ build: [
@ -219,6 +223,7 @@ module.exports = (grunt) ->
'set-build:crx' 'set-build:crx'
'concat:coffee' 'concat:coffee'
'coffee:script' 'coffee:script'
'coffee:eventPage'
'set-channel:stable' 'set-channel:stable'
'build-crx-channel' 'build-crx-channel'
'set-channel:beta' 'set-channel:beta'

View File

@ -1,4 +1,15 @@
CrossOrigin = CrossOrigin = do ->
<% if (type === 'crx') { %>
eventPageRequest = do ->
callbacks = []
chrome.runtime.onMessage.addListener (data) ->
callbacks[data.id] data
delete callbacks[data.id]
(url, responseType, cb) ->
chrome.runtime.sendMessage {url, responseType}, (id) ->
callbacks[id] = cb
<% } %>
file: do -> file: do ->
makeBlob = (urlBlob, contentType, contentDisposition, url) -> makeBlob = (urlBlob, contentType, contentDisposition, url) ->
name = url.match(/([^\/]+)\/*$/)?[1] name = url.match(/([^\/]+)\/*$/)?[1]
@ -14,15 +25,20 @@ CrossOrigin =
(url, cb) -> (url, cb) ->
<% if (type === 'crx') { %> <% if (type === 'crx') { %>
$.ajax url, if /^https:\/\//.test(url) or location.protocol is 'http:'
responseType: 'blob' $.ajax url,
onload: -> responseType: 'blob'
return cb null unless @readyState is @DONE and @status is 200 onload: ->
contentType = @getResponseHeader 'Content-Type' return cb null unless @readyState is @DONE and @status is 200
contentDisposition = @getResponseHeader 'Content-Disposition' contentType = @getResponseHeader 'Content-Type'
cb (makeBlob @response, contentType, contentDisposition, url) contentDisposition = @getResponseHeader 'Content-Disposition'
onerror: -> cb (makeBlob @response, contentType, contentDisposition, url)
cb null onerror: ->
cb null
else
eventPageRequest url, 'arraybuffer', ({response, contentType, contentDisposition, error}) ->
return cb null if error
cb (makeBlob new Uint8Array(response), contentType, contentDisposition, url)
<% } %> <% } %>
<% if (type === 'userscript') { %> <% if (type === 'userscript') { %>
GM_xmlhttpRequest GM_xmlhttpRequest
@ -48,9 +64,9 @@ CrossOrigin =
responses = {} responses = {}
(url, cb) -> (url, cb) ->
<% if (type === 'crx') { %> <% if (type === 'crx') { %>
$.cache url, (-> cb @response), responseType: 'json' if /^https:\/\//.test(url) or location.protocol is 'http:'
return $.cache url, (-> cb @response), responseType: 'json'
<% } %> <% } %>
<% if (type === 'userscript') { %>
if responses[url] if responses[url]
cb responses[url] cb responses[url]
return return
@ -58,6 +74,7 @@ CrossOrigin =
callbacks[url].push cb callbacks[url].push cb
return return
callbacks[url] = [cb] callbacks[url] = [cb]
<% if (type === 'userscript') { %>
GM_xmlhttpRequest GM_xmlhttpRequest
method: "GET" method: "GET"
url: url url: url
@ -71,3 +88,12 @@ CrossOrigin =
onabort: -> onabort: ->
delete callbacks[url] delete callbacks[url]
<% } %> <% } %>
<% if (type === 'crx') { %>
eventPageRequest url, 'json', ({response, error}) ->
if error
delete callbacks[url]
else
cb response for cb in callbacks[url]
delete callbacks[url]
responses[url] = response
<% } %>

View File

@ -0,0 +1,28 @@
requestID = 0
chrome.runtime.onMessage.addListener (request, sender, sendResponse) ->
id = requestID
requestID++
sendResponse id
xhr = new XMLHttpRequest()
xhr.open 'GET', request.url, true
xhr.responseType = request.responseType
xhr.addEventListener 'load', ->
if @readyState is @DONE && xhr.status is 200
contentType = @getResponseHeader 'Content-Type'
contentDisposition = @getResponseHeader 'Content-Disposition'
{response} = @
if request.responseType is 'arraybuffer'
response = [new Uint8Array(response)...]
chrome.tabs.sendMessage sender.tab.id, {id, response, contentType, contentDisposition}
else
chrome.tabs.sendMessage sender.tab.id, {id, error: true}
, false
xhr.addEventListener 'error', ->
chrome.tabs.sendMessage sender.tab.id, {id, error: true}
, false
xhr.addEventListener 'abort', ->
chrome.tabs.sendMessage sender.tab.id, {id, error: true}
, false
xhr.send()

View File

@ -14,6 +14,10 @@
"all_frames": true, "all_frames": true,
"run_at": "document_start" "run_at": "document_start"
}], }],
"background": {
"scripts": ["eventPage.js"],
"persistent": false
},
"homepage_url": "<%= meta.page %>", "homepage_url": "<%= meta.page %>",
<% if (channel !== 'noupdate') { %> "update_url": "<%= meta.downloads %>updates<%= meta.suffix[channel] %>.xml", <% if (channel !== 'noupdate') { %> "update_url": "<%= meta.downloads %>updates<%= meta.suffix[channel] %>.xml",
<% } %> "minimum_chrome_version": "<%= meta.min.chrome %>", <% } %> "minimum_chrome_version": "<%= meta.min.chrome %>",