Enable accessing background window from content script with tri.bg

This commit is contained in:
glacambre 2024-01-26 19:41:46 +01:00
parent 1522b0e2dc
commit ee540db193
4 changed files with 10 additions and 0 deletions

View file

@ -4,6 +4,7 @@
import * as proxy_background from "@src/lib/browser_proxy_background" import * as proxy_background from "@src/lib/browser_proxy_background"
import * as controller from "@src/lib/controller" import * as controller from "@src/lib/controller"
import { omniscient_controller } from "@src/lib/omniscient_controller"
import * as perf from "@src/perf" import * as perf from "@src/perf"
import { listenForCounters } from "@src/perf" import { listenForCounters } from "@src/perf"
import * as messaging from "@src/lib/messaging" import * as messaging from "@src/lib/messaging"
@ -259,6 +260,7 @@ const messages = {
downloadUrlAs: download_background.downloadUrlAs, downloadUrlAs: download_background.downloadUrlAs,
}, },
browser_proxy_background: { shim: proxy_background.shim }, browser_proxy_background: { shim: proxy_background.shim },
omniscient_background: omniscient_controller,
} }
export type Messages = typeof messages export type Messages = typeof messages

View file

@ -96,6 +96,7 @@ const excmds = await import("@src/.excmds_content.generated")
const finding_content = await import("@src/content/finding") const finding_content = await import("@src/content/finding")
const itertools = await import("@src/lib/itertools") const itertools = await import("@src/lib/itertools")
const messaging = await import("@src/lib/messaging") const messaging = await import("@src/lib/messaging")
const backgroundProxy = await import("@src/lib/tabs")
const State = await import("@src/state") const State = await import("@src/state")
const webext = await import("@src/lib/webext") const webext = await import("@src/lib/webext")
const perf = await import("@src/perf") const perf = await import("@src/perf")
@ -208,6 +209,7 @@ config.getAsync("preventautofocusjackhammer").then(allowautofocus => {
}) })
;(window as any).tri = Object.assign(Object.create(null), { ;(window as any).tri = Object.assign(Object.create(null), {
browserBg: webext.browserBg, browserBg: webext.browserBg,
bg: backgroundProxy.backgroundProxy,
commandline_content, commandline_content,
convert, convert,
config, config,

View file

@ -5853,6 +5853,7 @@ async function js_helper(str: string[]) {
* - -r load the js source from a Javascript file relative to your RC file. (NB: will throw an error if no RC file exists) * - -r load the js source from a Javascript file relative to your RC file. (NB: will throw an error if no RC file exists)
* *
* Some of Tridactyl's functions are accessible here via the `tri` object. Just do `console.log(tri)` in the web console on the new tab page to see what's available. * Some of Tridactyl's functions are accessible here via the `tri` object. Just do `console.log(tri)` in the web console on the new tab page to see what's available.
* `tri.bg` is an object enabling access to the background script's context. It works similarly to the `tri.tabs` objects documented in the [[jsb]] documentation.
* *
* If you want to pipe an argument to `js`, you need to use the "-p" flag or "-d" flag with an argument and then use the JS_ARG global variable, e.g: * If you want to pipe an argument to `js`, you need to use the "-p" flag or "-d" flag with an argument and then use the JS_ARG global variable, e.g:
* *

View file

@ -1,10 +1,13 @@
import * as Messaging from "@src/lib/messaging" import * as Messaging from "@src/lib/messaging"
const allTabs = -1 const allTabs = -1
const background = -2
const msg = (tabId, ...args) => { const msg = (tabId, ...args) => {
if (tabId === allTabs) { if (tabId === allTabs) {
return Messaging.messageAllTabs("omniscient_content", ...args) return Messaging.messageAllTabs("omniscient_content", ...args)
} else if (tabId === background) {
return (Messaging.message as any)("omniscient_background", args[0], args[1][0])
} else { } else {
return Messaging.messageTab(tabId, "omniscient_content", ...args) return Messaging.messageTab(tabId, "omniscient_content", ...args)
} }
@ -85,3 +88,5 @@ export const tabsProxy = new Proxy(Object.create(null), {
throw new Error(`'tabs' object can only be accessed by a number (e.g. tabs[3]) or a string (e.g. tabs.document or tabs['document']). Type of accessor: "${typeof p}"`) throw new Error(`'tabs' object can only be accessed by a number (e.g. tabs[3]) or a string (e.g. tabs.document or tabs['document']). Type of accessor: "${typeof p}"`)
}, },
}) })
export const backgroundProxy = tabProxy(background, [])