Skip to content

Commit

Permalink
Support both TST and Waterfox Sidebar
Browse files Browse the repository at this point in the history
  • Loading branch information
piroor committed Mar 19, 2024
1 parent 14fcf2b commit 9a51bf6
Show file tree
Hide file tree
Showing 2 changed files with 65 additions and 9 deletions.
19 changes: 11 additions & 8 deletions background/background.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,10 +7,12 @@

import {
configs,
TST_ID,
WS_ID,
callTSTAPI,
getTSTVersion,
} from '/common/common.js';

const TST_ID = '[email protected]';

// pointer-events is not transitionable, so we use animation.
const ANIMATION = `
@keyframes delay-pointer-events {
Expand Down Expand Up @@ -129,8 +131,8 @@ let mRenderedOnDemand = false;
async function registerToTST() {
try {
const [TSTVersion] = await Promise.all([
browser.runtime.sendMessage(TST_ID, { type: 'get-version' }),
browser.runtime.sendMessage(TST_ID, {
getTSTVersion(),
callTSTAPI({
type: 'register-self' ,
name: browser.i18n.getMessage('extensionName'),
//icons: browser.runtime.getManifest().icons,
Expand Down Expand Up @@ -176,6 +178,7 @@ configs.$addObserver(key => {
function onMessageExternal(message, sender) {
switch (sender.id) {
case TST_ID:
case WS_ID:
if (message && message.messages) {
for (const oneMessage of message.messages) {
onMessageExternal(oneMessage, sender);
Expand All @@ -189,7 +192,7 @@ function onMessageExternal(message, sender) {

case 'sidebar-show':
(mRenderedOnDemand ?
browser.runtime.sendMessage(TST_ID, {
callTSTAPI({
type: 'get-light-tree',
windowId: message.windowId,
tabs: '*',
Expand Down Expand Up @@ -227,7 +230,7 @@ function tryReset() {
tryReset.reserved = null;
const tabs = await (mRenderedOnDemand ?
browser.windows.getAll().then(async windows => {
const tabs = await Promise.all(windows.map(win => browser.runtime.sendMessage(TST_ID, {
const tabs = await Promise.all(windows.map(win => callTSTAPI({
type: 'get-light-tree',
windowId: win.id,
tabs: '*',
Expand Down Expand Up @@ -333,11 +336,11 @@ function insertHandle(tabId) {
const messages = [...mPendingInsertContentsMessages.values()];
mPendingInsertContentsMessages.clear();
if (mCanSendBulkMessages) {
browser.runtime.sendMessage(TST_ID, { messages });
callTSTAPI({ messages });
}
else {
for (const message of messages) {
browser.runtime.sendMessage(TST_ID, message);
callTSTAPI(message);
}
}
});
Expand Down
55 changes: 54 additions & 1 deletion common/common.js
Original file line number Diff line number Diff line change
Expand Up @@ -15,8 +15,61 @@ export const configs = new Configs({

size: 24,
showDelay: 750,
hideDelay: 250
hideDelay: 250,

TSTID: null,
}, {
localKeys: [
]
});


export const TST_ID = '[email protected]';
export const WS_ID = '[email protected]';

export async function ensureTSTDetected() {
try {
if (await browser.runtime.sendMessage(TST_ID, { type: 'ping' })) {
configs.TSTID = TST_ID;
return;
}
}
catch(_error) {
}
try {
if (await browser.runtime.sendMessage(WS_ID, { type: 'ping' })) {
configs.TSTID = WS_ID;
return;
}
}
catch(_error) {
}
throw new Error('Missing dependency: you need to install Tree Style Tab addon also');
}

export async function callTSTAPI(message) {
if (!configs.TSTID)
await ensureTSTDetected();

try {
return browser.runtime.sendMessage(configs.TSTID, message);
}
catch(error) {
configs.TSTID = null;
throw error;
}
}

export async function getTSTVersion() {
const version = await callTSTAPI({ type: 'get-version' });
switch (configs.TSTID) {
case TST_ID:
return version;

case WS_ID:
// WS 0.1-1.0 are corresponding to TST 4.x
const majorAndMinor = version.match(/^(\d+)\.(\d+)/);
return Math.ceil(parseFloat(majorAndMinor)) + 3;
}
return 0;
}

0 comments on commit 9a51bf6

Please sign in to comment.