WIP - update to new TST api

This commit is contained in:
Oliver Blanthorn 2019-06-01 18:59:17 +01:00 committed by Saul Reynolds-Haertle
parent a9ecb0287f
commit d4b6ba02ce
3 changed files with 60 additions and 2 deletions

View file

@ -24,6 +24,7 @@ import * as webext from "@src/lib/webext"
import { AutoContain } from "@src/lib/autocontainers"
import * as extension_info from "@src/lib/extension_info"
import * as omnibox from "@src/background/omnibox"
import * as treestyletab from "@src/interop/tst"
// Add various useful modules to the window for debugging
; (window as any).tri = Object.assign(Object.create(null), {
@ -217,3 +218,11 @@ window.tri = Object.assign(window.tri || Object.create(null), {
omnibox.init()
// }}}
// {{{ INTEROP
config.getAsync("treestyletabintegration").then(tst => {
if (tst) treestyletab.registerWithTST()
})
// }}}

View file

@ -159,6 +159,7 @@ import { firefoxVersionAtLeast } from "@src/lib/webext"
import * as rc from "@src/background/config_rc"
import * as css_util from "@src/lib/css_util"
import * as Updates from "@src/lib/updates"
import * as treestyletab from "@src/interop/tst"
ALL_EXCMDS = {
"": BGSELF,
@ -1995,7 +1996,7 @@ export async function tabprev(increment = 1) {
let hasTST = false
try {
// Not sure why this is an error
await browser.management.get("treestyletab@piro.sakura.ne.jp")
await browser.management.get(treestyletab.TST_ID)
hasTST = true
} catch (e) {
hasTST = false
@ -2004,9 +2005,12 @@ export async function tabprev(increment = 1) {
if (config.get("treestyletabintegration") && hasTST) {
// Ok so this entire piece here is really inefficient, it looks up all tabs, gets the active tab id, gets all tabs again (this time as a tree), iterates through all those tabs flattening them into an array, and then iterates over them once more to find the index of the active tab in that flattened array. This has a lot of room for improvement in the future.
// Find the current TAB id
await treestyletab.registerWithTST()
const activeTabId = (await activeTab()).id
// Get the whole tab tree
const tabTree = await browser.runtime.sendMessage("treestyletab@piro.sakura.ne.jp", {
// Need to register since recent TST versions to be able to send any messages
const tabTree = await browser.runtime.sendMessage(treestyletab.TST_ID, {
type: "get-tree",
tabs: "*",
})
@ -4446,4 +4450,17 @@ browser.runtime.onInstalled.addListener(details => {
// could add elif "update" and show a changelog. Hide it behind a setting to make it less annoying?
})
//#background
export function register(addon: string) {
switch (addon) {
case "tst":
case "treestyletab":
// For some reason, errors from this don't bubble up to cmdline
treestyletab.registerWithTST(true)
break
default:
throw new Error("Extension " + addon + " is not currently supported. Please file an :issue.")
}
}
// vim: tabstop=4 shiftwidth=4 expandtab

32
src/interop/tst.ts Normal file
View file

@ -0,0 +1,32 @@
export const TST_ID = "treestyletab@piro.sakura.ne.jp"
async function _registerWithTST(manual = false) {
try {
await browser.runtime.sendMessage(TST_ID, {
type: "register-self",
name: browser.i18n.getMessage("extensionName"),
icons: browser.runtime.getManifest().icons,
listeningTypes: [],
permissions: ["tabs"]
})
} catch (e) {
if (manual) throw new Error("TreeStyleTab hasn't finished loading: " + e)
}
}
// Register once TST says that it is ready
export async function registerWithTST(manual = false) {
try {
await browser.management.get(TST_ID)
} catch (e) {
// We get an error if TST isn't found
if (manual) throw new Error("TreeStyleTab couldn't be found: " + e)
}
// Initial registration must be done manually (i.e. make an excmd)/ on install
// but after that TST will tell us it is ready
browser.runtime.onMessageExternal.addListener((message, sender) => {
if (sender.id === TST_ID && (message.type === "ready" || message.type === "permissions-changed")) _registerWithTST()
})
const tst_prom = _registerWithTST(manual) // TST won't send a message if we've never registered before, so try it anyway
if (manual) await tst_prom
}