WIP: add a compat wrapper for browser

This commit is contained in:
Oliver Blanthorn 2023-07-01 20:01:42 +02:00
parent 5eaab1046f
commit 64bcac69dd
No known key found for this signature in database
GPG key ID: 2BB8C36BB504BFF3
2 changed files with 30 additions and 0 deletions

View file

@ -97,6 +97,7 @@ const itertools = await import("@src/lib/itertools")
const messaging = await import("@src/lib/messaging")
const State = await import("@src/state")
const webext = await import("@src/lib/webext")
const {default: compatProxy} = await import("@src/lib/compat_proxy")
const perf = await import("@src/perf")
const keyseq = await import("@src/lib/keyseq")
const native = await import("@src/lib/native")
@ -206,6 +207,7 @@ config.getAsync("preventautofocusjackhammer").then(allowautofocus => {
})
;(window as any).tri = Object.assign(Object.create(null), {
browserBg: webext.browserBg,
compatProxy,
commandline_content,
convert,
config,

28
src/lib/compat_proxy.ts Normal file
View file

@ -0,0 +1,28 @@
import { message } from "@src/lib/messaging"
const compatProxy = new Proxy(Object.create(null), {
get(target, api) {
return new Proxy(
{},
{
get(_, func) {
return (...args) => {
if (typeof browser[api][func] === 'function') {
return browser[api][func](...args)
}
// return Promise.reject(new Error(`browser.${String(api)}.${String(func)} isn't a function`))
// ideally what we want is to use TypeScript's knowledge of browser to create an "empty"
// return value of the same type as the function we're trying to call, but I don't know how to do that
// if function missing, return an empty object of the same type using TypeScript
// return ReturnType<typeof browser[api][func]> // can't use api because it's _any_, let's fix one thing at a time
// return new ReturnType<typeof browser.runtime.getURL>() // Error: ReturnType refers to a type
}
},
},
)
},
}) as typeof browser
export default compatProxy