Add Userscript proxy
This commit is contained in:
@@ -815,10 +815,13 @@ export async function globalFetch(url: string, arg: GlobalFetchArgs = {}): Promi
|
|||||||
return { ok: false, headers: {}, data: 'You are trying local request on web version. This is not allowed due to browser security policy. Use the desktop version instead, or use a tunneling service like ngrok and set the CORS to allow all.' };
|
return { ok: false, headers: {}, data: 'You are trying local request on web version. This is not allowed due to browser security policy. Use the desktop version instead, or use a tunneling service like ngrok and set the CORS to allow all.' };
|
||||||
}
|
}
|
||||||
|
|
||||||
// Simplify the globalFetch function: Detach built-in functions
|
|
||||||
if (forcePlainFetch) {
|
if (forcePlainFetch) {
|
||||||
return await fetchWithPlainFetch(url, arg);
|
return await fetchWithPlainFetch(url, arg);
|
||||||
}
|
}
|
||||||
|
//userScriptFetch is provided by userscript
|
||||||
|
if(window.userScriptFetch){
|
||||||
|
return await fetchWithUSFetch(url, arg);
|
||||||
|
}
|
||||||
if (isTauri) {
|
if (isTauri) {
|
||||||
return await fetchWithTauri(url, arg);
|
return await fetchWithTauri(url, arg);
|
||||||
}
|
}
|
||||||
@@ -890,6 +893,26 @@ async function fetchWithPlainFetch(url: string, arg: GlobalFetchArgs): Promise<G
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Performs a fetch request using userscript provided fetch.
|
||||||
|
*
|
||||||
|
* @param {string} url - The URL to fetch.
|
||||||
|
* @param {GlobalFetchArgs} arg - The arguments for the fetch request.
|
||||||
|
* @returns {Promise<GlobalFetchResult>} - The result of the fetch request.
|
||||||
|
*/
|
||||||
|
async function fetchWithUSFetch(url: string, arg: GlobalFetchArgs): Promise<GlobalFetchResult> {
|
||||||
|
try {
|
||||||
|
const headers = { 'Content-Type': 'application/json', ...arg.headers };
|
||||||
|
const response = await userScriptFetch(url, { body: JSON.stringify(arg.body), headers, method: arg.method ?? "POST", signal: arg.abortSignal });
|
||||||
|
const data = arg.rawResponse ? new Uint8Array(await response.arrayBuffer()) : await response.json();
|
||||||
|
const ok = response.ok && response.status >= 200 && response.status < 300;
|
||||||
|
addFetchLogInGlobalFetch(data, ok, url, arg);
|
||||||
|
return { ok, data, headers: Object.fromEntries(response.headers) };
|
||||||
|
} catch (error) {
|
||||||
|
return { ok: false, data: `${error}`, headers: {} };
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Performs a fetch request using Tauri.
|
* Performs a fetch request using Tauri.
|
||||||
*
|
*
|
||||||
@@ -1866,40 +1889,6 @@ export async function fetchNative(url:string, arg:{
|
|||||||
throw new Error('Body is required for POST and PUT requests')
|
throw new Error('Body is required for POST and PUT requests')
|
||||||
}
|
}
|
||||||
|
|
||||||
const jsonizer = (body:ReadableStream<Uint8Array>) => {
|
|
||||||
return async () => {
|
|
||||||
const text = await textifyReadableStream(body)
|
|
||||||
return JSON.parse(text)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
const textizer = (body:ReadableStream<Uint8Array>) => {
|
|
||||||
return async () => {
|
|
||||||
const text = await textifyReadableStream(body)
|
|
||||||
return text
|
|
||||||
}
|
|
||||||
}
|
|
||||||
const arrayBufferizer = (body:ReadableStream<Uint8Array>) => {
|
|
||||||
return async () => {
|
|
||||||
const chunks:Uint8Array[] = []
|
|
||||||
const reader = body.getReader()
|
|
||||||
while(true){
|
|
||||||
const {done, value} = await reader.read()
|
|
||||||
if(done){
|
|
||||||
break
|
|
||||||
}
|
|
||||||
chunks.push(value)
|
|
||||||
}
|
|
||||||
const totalLength = chunks.reduce((acc, chunk) => acc + chunk.length, 0)
|
|
||||||
const arrayBuffer = new Uint8Array(totalLength)
|
|
||||||
let offset = 0
|
|
||||||
for(const chunk of chunks){
|
|
||||||
arrayBuffer.set(chunk, offset)
|
|
||||||
offset += chunk.length
|
|
||||||
}
|
|
||||||
return arrayBuffer.buffer
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
arg.method = arg.method ?? 'POST'
|
arg.method = arg.method ?? 'POST'
|
||||||
|
|
||||||
let headers = arg.headers ?? {}
|
let headers = arg.headers ?? {}
|
||||||
@@ -1932,7 +1921,15 @@ export async function fetchNative(url:string, arg:{
|
|||||||
resType: 'stream',
|
resType: 'stream',
|
||||||
chatId: arg.chatId,
|
chatId: arg.chatId,
|
||||||
})
|
})
|
||||||
if(isTauri){
|
if(window.userScriptFetch){
|
||||||
|
return await window.userScriptFetch(url,{
|
||||||
|
body: realBody,
|
||||||
|
headers: headers,
|
||||||
|
method: arg.method,
|
||||||
|
signal: arg.signal
|
||||||
|
})
|
||||||
|
}
|
||||||
|
else if(isTauri){
|
||||||
fetchIndex++
|
fetchIndex++
|
||||||
if(arg.signal && arg.signal.aborted){
|
if(arg.signal && arg.signal.aborted){
|
||||||
throw new Error('aborted')
|
throw new Error('aborted')
|
||||||
|
|||||||
1
src/vite-env.d.ts
vendored
1
src/vite-env.d.ts
vendored
@@ -4,3 +4,4 @@
|
|||||||
|
|
||||||
declare var Buffer: BufferConstructor
|
declare var Buffer: BufferConstructor
|
||||||
declare var safeStructuredClone: <T>(data: T) => T
|
declare var safeStructuredClone: <T>(data: T) => T
|
||||||
|
declare var userScriptFetch: (url: string,arg:RequestInit) => Promise<Response>
|
||||||
11
util/risuUserscript.user.js
Normal file
11
util/risuUserscript.user.js
Normal file
@@ -0,0 +1,11 @@
|
|||||||
|
// ==UserScript==
|
||||||
|
// @name RisuAI Requester
|
||||||
|
// @version 1.0.0
|
||||||
|
// @match https://risuai.xyz/*
|
||||||
|
// @grant GM.xmlHttpRequest
|
||||||
|
// @require https://cdn.jsdelivr.net/npm/@trim21/gm-fetch@0.2.3
|
||||||
|
// @run-at document-end
|
||||||
|
// @connect *
|
||||||
|
// ==/UserScript==
|
||||||
|
|
||||||
|
window.fetchWithUSFetch = GM_fetch
|
||||||
Reference in New Issue
Block a user