From 6b43acd57951433914e8c6ef0c33240c801ccbb7 Mon Sep 17 00:00:00 2001 From: SpikeHD Date: Mon, 9 Oct 2023 12:01:38 -0700 Subject: [PATCH] feat(all): proxy fetch to fix lots of stuff at once --- src-tauri/injection/injection.js | 2 +- src-tauri/injection/preinject.js | 30 ++++++++++++++++++++++++++++++ src-tauri/tauri.conf.json | 8 +++++++- 3 files changed, 38 insertions(+), 2 deletions(-) diff --git a/src-tauri/injection/injection.js b/src-tauri/injection/injection.js index 4bfa1714..d95d82e5 100644 --- a/src-tauri/injection/injection.js +++ b/src-tauri/injection/injection.js @@ -202,4 +202,4 @@ function applyExtraCSS() { style.innerHTML = css document.head.appendChild(style) }) -} \ No newline at end of file +} diff --git a/src-tauri/injection/preinject.js b/src-tauri/injection/preinject.js index 613e1d1e..69378a64 100644 --- a/src-tauri/injection/preinject.js +++ b/src-tauri/injection/preinject.js @@ -40,6 +40,7 @@ function safemodeTimer(elm) { */ (async () => { createLocalStorage() + proxyFetch() await displayLoadingTop() @@ -308,4 +309,33 @@ function createLocalStorage() { clearInterval(interval) }, 50) +} + +/** + * Overwrite the global fetch function with a new one that will redirect to the tauri API + */ +function proxyFetch() { + window.nativeFetch = window.fetch + + // eslint-disable-next-line no-global-assign + fetch = async (url, options) => { + const discordReg = /https?:\/\/(?:[a-z]+\.)?(?:discord\.com|discordapp\.net)(?:\/.*)?/g + + // If it matches, just let it go through native + if (url.toString().match(discordReg)) { + return window.nativeFetch(url, options) + } + + const { http } = window.__TAURI__ + const response = await http.fetch(url, { + responseType: 2, + ...options + }) + + // Adherence to what most scripts will expect to have available when they are using fetch() + response.json = () => JSON.parse(response.data) + response.text = () => response.data + + return response + } } \ No newline at end of file diff --git a/src-tauri/tauri.conf.json b/src-tauri/tauri.conf.json index fe488bb8..6592764b 100644 --- a/src-tauri/tauri.conf.json +++ b/src-tauri/tauri.conf.json @@ -16,7 +16,13 @@ "iconAsTemplate": true }, "allowlist": { - "all": true + "all": true, + "http": { + "scope": [ + "https://**/*", + "http://**/*" + ] + } }, "bundle": { "active": true,