mirror of
https://github.com/GetOpenScript/OpenScript.git
synced 2026-10-11 11:05:20 +00:00
Add OpenScript.fetch cross-origin network API and docs
This commit is contained in:
+13
-5
@@ -1,5 +1,6 @@
|
||||
import { syncUserScripts } from './utils/userScripts.js';
|
||||
import { runScriptStorageOperation } from './utils/storage.js';
|
||||
import { runScriptFetch } from './utils/fetch.js';
|
||||
|
||||
let syncQueue = Promise.resolve();
|
||||
const safelySync = async options => {
|
||||
@@ -30,9 +31,16 @@ chrome.runtime.onMessage.addListener((msg, sender, sendResponse) => {
|
||||
});
|
||||
|
||||
chrome.runtime.onUserScriptMessage.addListener((msg, sender, sendResponse) => {
|
||||
if (msg?.type !== 'OPEN_SCRIPT_STORAGE') return;
|
||||
runScriptStorageOperation(msg.token, msg.operation, msg.key, msg.value)
|
||||
.then(result => sendResponse({ ok: true, ...result }))
|
||||
.catch(error => sendResponse({ ok: false, error: error.message }));
|
||||
return true;
|
||||
if (msg?.type === 'OPEN_SCRIPT_STORAGE') {
|
||||
runScriptStorageOperation(msg.token, msg.operation, msg.key, msg.value)
|
||||
.then(result => sendResponse({ ok: true, ...result }))
|
||||
.catch(error => sendResponse({ ok: false, error: error.message }));
|
||||
return true;
|
||||
}
|
||||
if (msg?.type === 'OPEN_SCRIPT_FETCH') {
|
||||
runScriptFetch(msg.url, msg.options)
|
||||
.then(result => sendResponse({ ok: true, ...result }))
|
||||
.catch(error => sendResponse({ ok: false, error: error.message }));
|
||||
return true;
|
||||
}
|
||||
});
|
||||
|
||||
@@ -0,0 +1,9 @@
|
||||
const NULL_BODY_STATUSES = new Set([101, 204, 205, 304]);
|
||||
|
||||
export const runScriptFetch = async (url, options = {}) => {
|
||||
const res = await fetch(url, options);
|
||||
const { status, statusText } = res;
|
||||
const headers = [...res.headers.entries()];
|
||||
const body = NULL_BODY_STATUSES.has(status) ? null : await res.arrayBuffer();
|
||||
return { status, statusText, headers, url: res.url, body };
|
||||
};
|
||||
@@ -3,6 +3,7 @@ import { normalizeMatch, parseMeta, getMetaRunAt } from './parser.js';
|
||||
import { VERSION } from '../version.js';
|
||||
|
||||
const STORAGE_MESSAGE = 'OPEN_SCRIPT_STORAGE';
|
||||
const FETCH_MESSAGE = 'OPEN_SCRIPT_FETCH';
|
||||
|
||||
export const isUserScriptsAvailable = async () => {
|
||||
if (!chrome.userScripts) return false;
|
||||
@@ -34,7 +35,21 @@ ${code}
|
||||
delete: key => call('delete', key).then(() => undefined),
|
||||
list: () => call('list').then(result => result.keys),
|
||||
});
|
||||
const OpenScript = Object.freeze({ version: '${VERSION}', env, storage });
|
||||
const fetch = async (url, options = {}) => {
|
||||
let { headers, body, ...rest } = options;
|
||||
if (headers instanceof Headers) headers = Object.fromEntries(headers.entries());
|
||||
const response = await chrome.runtime.sendMessage({
|
||||
type: '${FETCH_MESSAGE}', url: url.toString(), options: { ...rest, headers, body },
|
||||
});
|
||||
if (!response?.ok) throw new TypeError(response?.error || 'OpenScript fetch failed');
|
||||
const resBody = [101, 204, 205, 304].includes(response.status) ? null : response.body;
|
||||
const res = new Response(resBody, {
|
||||
status: response.status, statusText: response.statusText, headers: response.headers,
|
||||
});
|
||||
Object.defineProperty(res, 'url', { value: response.url || url.toString() });
|
||||
return res;
|
||||
};
|
||||
const OpenScript = Object.freeze({ version: '${VERSION}', env, storage, fetch });
|
||||
globalThis.OpenScript = OpenScript;
|
||||
globalThis.env = env;
|
||||
return [OpenScript, env];
|
||||
|
||||
+1
-1
@@ -1 +1 @@
|
||||
export const VERSION = '1.0.3';
|
||||
export const VERSION = '1.0.4';
|
||||
|
||||
Reference in New Issue
Block a user