accept xhr options in download(url, options)
This commit is contained in:
parent
74e6ea5a56
commit
03048ea98e
|
@ -408,49 +408,42 @@ function deleteStyleSafe({id, notify = true} = {}) {
|
|||
}
|
||||
|
||||
|
||||
function download(url) {
|
||||
function download(url, {
|
||||
method = url.includes('?') ? 'POST' : 'GET',
|
||||
headers = {
|
||||
'Content-type': 'application/x-www-form-urlencoded',
|
||||
},
|
||||
body = url.includes('?') ? url.slice(url.indexOf('?')) : null,
|
||||
timeout = 10e3,
|
||||
requiredStatusCode = 200,
|
||||
} = {}) {
|
||||
return new Promise((resolve, reject) => {
|
||||
url = new URL(url);
|
||||
const TIMEOUT = 10000;
|
||||
const options = {
|
||||
method: url.search ? 'POST' : 'GET',
|
||||
body: url.search ? url.search.slice(1) : null,
|
||||
headers: {
|
||||
'Content-type': 'application/x-www-form-urlencoded'
|
||||
}
|
||||
};
|
||||
if (url.protocol === 'file:' && FIREFOX) {
|
||||
// https://stackoverflow.com/questions/42108782/firefox-webextensions-get-local-files-content-by-path
|
||||
options.mode = 'same-origin';
|
||||
// FIXME: add FetchController when it is available.
|
||||
// https://developer.mozilla.org/en-US/docs/Web/API/FetchController/abort
|
||||
let timer;
|
||||
const timer = setTimeout(() => {
|
||||
reject(new Error(`Fetch URL timeout: ${url.href}`));
|
||||
}, timeout);
|
||||
fetch(url.href, {mode: 'same-origin'})
|
||||
.then(r => {
|
||||
clearTimeout(timer);
|
||||
if (r.status !== 200) {
|
||||
throw r.status;
|
||||
}
|
||||
return r.text();
|
||||
return r.status === 200 ? r.text() : Promise.reject(r.status);
|
||||
})
|
||||
.then(resolve, reject);
|
||||
timer = setTimeout(
|
||||
() => reject(new Error(`Fetch URL timeout: ${url.href}`)),
|
||||
TIMEOUT
|
||||
);
|
||||
.catch(reject)
|
||||
.then(resolve);
|
||||
return;
|
||||
}
|
||||
const xhr = new XMLHttpRequest();
|
||||
xhr.timeout = TIMEOUT;
|
||||
xhr.onload = () => (xhr.status === 200 || url.protocol === 'file:'
|
||||
? resolve(xhr.responseText)
|
||||
: reject(xhr.status));
|
||||
xhr.timeout = timeout;
|
||||
xhr.onload = () => (
|
||||
!requiredStatusCode || xhr.status === requiredStatusCode || url.protocol === 'file:' ?
|
||||
resolve(xhr.responseText) :
|
||||
reject(xhr.status));
|
||||
xhr.onerror = reject;
|
||||
xhr.open(options.method, url.href, true);
|
||||
for (const key of Object.keys(options.headers)) {
|
||||
xhr.setRequestHeader(key, options.headers[key]);
|
||||
}
|
||||
xhr.send(options.body);
|
||||
xhr.open(method, url.href, true);
|
||||
Object.keys(headers || {}).forEach(name => xhr.setRequestHeader(name, headers[name]));
|
||||
xhr.send(body);
|
||||
});
|
||||
}
|
||||
|
||||
|
|
Loading…
Reference in New Issue
Block a user