mirror of
https://github.com/vale981/tridactyl
synced 2025-03-04 09:01:39 -05:00
WIP: add a compat wrapper for browser
This commit is contained in:
parent
5eaab1046f
commit
64bcac69dd
2 changed files with 30 additions and 0 deletions
|
@ -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
28
src/lib/compat_proxy.ts
Normal 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
|
Loading…
Add table
Reference in a new issue