Skip to content

Commit

Permalink
feat(all): proxy fetch to fix lots of stuff at once
Browse files Browse the repository at this point in the history
  • Loading branch information
SpikeHD committed Oct 9, 2023
1 parent 5de58ab commit 6b43acd
Show file tree
Hide file tree
Showing 3 changed files with 38 additions and 2 deletions.
2 changes: 1 addition & 1 deletion src-tauri/injection/injection.js
Original file line number Diff line number Diff line change
Expand Up @@ -202,4 +202,4 @@ function applyExtraCSS() {
style.innerHTML = css
document.head.appendChild(style)
})
}
}
30 changes: 30 additions & 0 deletions src-tauri/injection/preinject.js
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,7 @@ function safemodeTimer(elm) {
*/
(async () => {
createLocalStorage()
proxyFetch()

await displayLoadingTop()

Expand Down Expand Up @@ -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
}
}
8 changes: 7 additions & 1 deletion src-tauri/tauri.conf.json
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,13 @@
"iconAsTemplate": true
},
"allowlist": {
"all": true
"all": true,
"http": {
"scope": [
"https://**/*",
"http://**/*"
]
}
},
"bundle": {
"active": true,
Expand Down

0 comments on commit 6b43acd

Please sign in to comment.