2016-06-28 09:19:01 +12:00
|
|
|
/* global chrome, safari */
|
|
|
|
|
|
|
|
import fetch from './fetch'
|
|
|
|
import isNode from 'detect-node'
|
|
|
|
|
|
|
|
const safariQueue = {}
|
|
|
|
|
|
|
|
// messaging with the safari extension global page
|
|
|
|
function safariHandler (ev) {
|
2019-10-08 22:31:42 +13:00
|
|
|
const type = ev.message.type
|
|
|
|
const url = ev.message.input
|
|
|
|
const data = ev.message.output // arraybuffer
|
2016-06-28 09:19:01 +12:00
|
|
|
if (!safariQueue[url]) {
|
|
|
|
// console.error("Unable to get callback for " + url, JSON.stringify(safariQueue))
|
|
|
|
return
|
|
|
|
}
|
2019-10-08 22:31:42 +13:00
|
|
|
const cb = safariQueue[url].cb
|
|
|
|
const responseType = safariQueue[url].responseType
|
2016-06-28 09:19:01 +12:00
|
|
|
console.log(url, cb, responseType, data)
|
|
|
|
delete safariQueue[url]
|
|
|
|
|
|
|
|
if (responseType === 'blob') {
|
2019-10-08 22:31:42 +13:00
|
|
|
const blob = new Blob([data], { type: type })
|
2016-06-28 09:19:01 +12:00
|
|
|
cb(blob, type)
|
|
|
|
} else {
|
|
|
|
if (!responseType) {
|
2019-10-08 22:31:42 +13:00
|
|
|
const blob = new Blob([data], { type: type })
|
|
|
|
const fr = new FileReader()
|
2016-06-28 09:19:01 +12:00
|
|
|
fr.onloadend = function () {
|
|
|
|
cb(fr.result, type)
|
|
|
|
}
|
|
|
|
fr.readAsText(blob)
|
|
|
|
} else {
|
|
|
|
cb(data, type)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if (typeof safari !== 'undefined') {
|
|
|
|
safari.self.addEventListener('message', safariHandler, false)
|
|
|
|
}
|
|
|
|
|
2016-08-23 19:19:01 +12:00
|
|
|
function fetchBackground (url, responseType) {
|
|
|
|
return new Promise((resolve, reject) => {
|
|
|
|
if (typeof chrome !== 'undefined' && chrome.runtime.sendMessage) {
|
|
|
|
chrome.runtime.sendMessage(url, function (objurl) {
|
2018-03-16 22:55:04 +13:00
|
|
|
resolve(fetch(objurl, responseType).then((data) => {
|
|
|
|
URL.revokeObjectURL(objurl)
|
|
|
|
return data
|
|
|
|
}))
|
2016-08-23 19:19:01 +12:00
|
|
|
})
|
|
|
|
} else if (typeof safari !== 'undefined') {
|
2019-10-08 19:37:27 +13:00
|
|
|
safariQueue[url] = { cb: resolve, responseType: responseType }
|
2016-08-23 19:19:01 +12:00
|
|
|
safari.self.tab.dispatchMessage('remote', url)
|
|
|
|
} else {
|
|
|
|
resolve(null)
|
|
|
|
}
|
|
|
|
})
|
2016-06-28 09:19:01 +12:00
|
|
|
}
|
|
|
|
|
2016-08-23 19:19:01 +12:00
|
|
|
export default function fetchRemote (url, responseType) {
|
2018-03-13 10:08:52 +13:00
|
|
|
if (url.startsWith('//')) {
|
2018-03-16 23:09:30 +13:00
|
|
|
url = 'https:' + url
|
2016-06-28 09:19:01 +12:00
|
|
|
}
|
2020-09-08 03:08:49 +12:00
|
|
|
if (!isNode && document.location.protocol === 'https:') {
|
|
|
|
if (url.startsWith('/')) {
|
|
|
|
url = window.location.origin + url
|
|
|
|
}
|
2016-08-23 19:19:01 +12:00
|
|
|
return fetchBackground(url, responseType)
|
2016-06-28 09:19:01 +12:00
|
|
|
}
|
2018-03-16 23:09:30 +13:00
|
|
|
return fetch(url, responseType).then((data) => {
|
|
|
|
if (!data) return fetchBackground(url, responseType)
|
|
|
|
else return Promise.resolve(data)
|
|
|
|
})
|
2016-06-28 09:19:01 +12:00
|
|
|
}
|