Add OpenScript.fetch cross-origin network API and docs

This commit is contained in:
2026-09-13 13:39:46 -07:00
parent 1cd7b693ab
commit da135d506a
11 changed files with 332 additions and 11 deletions
+13 -5
View File
@@ -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;
}
});
+9
View File
@@ -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 };
};
+16 -1
View File
@@ -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
View File
@@ -1 +1 @@
export const VERSION = '1.0.3';
export const VERSION = '1.0.4';