-
Notifications
You must be signed in to change notification settings - Fork 25
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
9 changed files
with
1,313 additions
and
1,230 deletions.
There are no files selected for viewing
Large diffs are not rendered by default.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,28 @@ | ||
const defaultOptions = { | ||
partLimit: 1e3, | ||
requestLimit: 5, | ||
retry: 3 | ||
} | ||
|
||
export const options = Object.assign({}, defaultOptions) | ||
|
||
export const getLocalOptions = () => { | ||
return new Promise((resolve) => { | ||
chrome.storage.local.get('localOptions', ({ localOptions }) => { | ||
resolve(localOptions instanceof Object ? localOptions : {}) | ||
}) | ||
}) | ||
} | ||
export const mergeOptions = (newOptions) => { | ||
return Object.assign(options, defaultOptions, newOptions instanceof Object ? newOptions : {}) | ||
} | ||
export const mergeLocalOptions = async () => { | ||
return Object.assign(mergeOptions(await getLocalOptions())) | ||
} | ||
|
||
export default { | ||
options, | ||
getLocalOptions, | ||
mergeOptions, | ||
mergeLocalOptions | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,72 @@ | ||
import qs from 'qs' | ||
import { mergeLocalOptions } from './options' | ||
const noop = (func, defaultFunc) => { | ||
return typeof func === 'function' ? func : typeof defaultFunc === 'function' ? defaultFunc : () => {} | ||
} | ||
export const blob2array = (blob) => { | ||
return new Promise((resolve, reject) => { | ||
const reader = new FileReader() | ||
reader.onload = () => { | ||
resolve(Array.from(new Uint8Array(reader.result))) | ||
} | ||
reader.onerror = reject | ||
reader.readAsArrayBuffer(blob) | ||
}) | ||
} | ||
export const array2blob = (array, mimeType = '') => { | ||
return new Blob([new Uint8Array(array)], { type: mimeType }); | ||
} | ||
|
||
export const fetchBlobFile = (file) => { | ||
return new Promise((resolve) => { | ||
if (file.content) { | ||
resolve(new Blob([file.content], {type : file.type || 'text/plain'})) | ||
} else { | ||
chrome.runtime.sendMessage({ type: 'get', url: file.downloadUrl, dataType: 'blob' }, ([error, message]) => { | ||
if (chrome.runtime.lastError) { | ||
console.error(chrome.runtime.lastError.message); | ||
return; | ||
} | ||
if (error) { | ||
resolve(new Blob([file.downloadUrl], {type : 'text/plain'})) | ||
} else { | ||
resolve(array2blob(message.data, message.mimeType || 'text/plain')) | ||
} | ||
}) | ||
} | ||
}) | ||
} | ||
|
||
export const ajax = async (options) => { | ||
const config = await mergeLocalOptions() | ||
const core = (retry = 3) => { | ||
const errorCB = (err) => { | ||
if (retry-- > 0) { | ||
setTimeout(() => { | ||
core(retry - 1) | ||
}, 3e3) | ||
} else { | ||
noop(options.error)(err) | ||
} | ||
} | ||
const { method = 'get', data, dataType, headers, success } = options | ||
const fetchOptions = { | ||
method, | ||
headers: headers instanceof Object ? headers : {} | ||
} | ||
if (/get/i.test(method)) { | ||
options.url += (/\?/.test(options.url) ? '&' : '?') + qs.stringify(data) | ||
} else { | ||
fetchOptions.body = JSON.stringify(data) | ||
} | ||
fetch(options.url, Object.assign(fetchOptions)).then(res => { | ||
return res[dataType] ? res[dataType]() : res.text() | ||
}).then(success).catch(errorCB) | ||
} | ||
core(config.retry) | ||
} | ||
|
||
export default { | ||
ajax, | ||
fetchBlobFile | ||
} |
Oops, something went wrong.