Skip to content

Commit

Permalink
Move if-then templates out of functions in CrossOrigin. #829
Browse files Browse the repository at this point in the history
  • Loading branch information
ccd0 committed Jul 27, 2019
1 parent fb24946 commit c8f7dea
Showing 1 changed file with 26 additions and 19 deletions.
45 changes: 26 additions & 19 deletions src/platform/CrossOrigin.coffee
Original file line number Diff line number Diff line change
Expand Up @@ -11,15 +11,17 @@ eventPageRequest = do ->
callbacks[id] = cb
<% } %>

<% if (type === 'crx') { %>
CrossOrigin.binary = (url, cb, headers={}) ->
# XXX https://forums.lanik.us/viewtopic.php?f=64&t=24173&p=78310
url = url.replace /^((?:https?:)?\/\/(?:\w+\.)?(?:4chan|4channel|4cdn)\.org)\/adv\//, '$1//adv/'
<% if (type === 'crx') { %>
eventPageRequest {type: 'ajax', url, headers, responseType: 'arraybuffer'}, ({response, responseHeaderString}) ->
response = new Uint8Array(response) if response
cb response, responseHeaderString
<% } %>
<% if (type === 'userscript') { %>
<% } else if (type === 'userscript') { %>
CrossOrigin.binary = (url, cb, headers={}) ->
# XXX https://forums.lanik.us/viewtopic.php?f=64&t=24173&p=78310
url = url.replace /^((?:https?:)?\/\/(?:\w+\.)?(?:4chan|4channel|4cdn)\.org)\/adv\//, '$1//adv/'
fallback = ->
$.ajax url, {
headers
Expand Down Expand Up @@ -58,7 +60,7 @@ CrossOrigin.binary = (url, cb, headers={}) ->
(GM?.xmlHttpRequest or GM_xmlhttpRequest) gmOptions
catch
fallback()
<% } %>
<% } %>

CrossOrigin.file = (url, cb) ->
CrossOrigin.binary url, (data, headers) ->
Expand Down Expand Up @@ -109,19 +111,31 @@ CrossOrigin.Request = class Request
# `response` - decoded response body
# `abort` - function for aborting the request (silently fails on some platforms)
# `getResponseHeader` - function for reading response headers
<% if (type === 'crx') { %>
CrossOrigin.ajax = (url, options={}) ->
{onloadend, timeout, responseType, headers} = options
responseType ?= 'json'

req = new CrossOrigin.Request()
req.onloadend = onloadend

eventPageRequest {type: 'ajax', url, responseType, headers, timeout}, (result) ->
if result.status
$.extend req, result
req.onloadend()

req
<% } else if (type === 'userscript') { %>
CrossOrigin.ajax = (url, options={}) ->
{onloadend, timeout, responseType, headers} = options
responseType ?= 'json'

<% if (type === 'userscript') { %>
unless GM?.xmlHttpRequest? or GM_xmlhttpRequest?
return $.ajax url, options
<% } %>

req = new CrossOrigin.Request()
req.onloadend = onloadend

<% if (type === 'userscript') { %>
gmOptions = {
method: 'GET'
url
Expand Down Expand Up @@ -154,25 +168,18 @@ CrossOrigin.ajax = (url, options={}) ->
req.abort = ->
try
gmReq.abort()
<% } %>

<% if (type === 'crx') { %>
eventPageRequest {type: 'ajax', url, responseType, headers, timeout}, (result) ->
if result.status
$.extend req, result
req.onloadend()
<% } %>

req
<% } %>

CrossOrigin.cache = (url, cb) ->
$.cache url, cb,
ajax: CrossOrigin.ajax

<% if (type === 'crx') { %>
CrossOrigin.permission = (cb) ->
<% if (type === 'crx') { %>
eventPageRequest {type: 'permission'}, -> cb()
<% } %>
<% if (type === 'userscript') { %>
<% } else if (type === 'userscript') { %>
CrossOrigin.permission = (cb) ->
cb()
<% } %>
<% } %>

0 comments on commit c8f7dea

Please sign in to comment.