-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Support both TST and Waterfox Sidebar
- Loading branch information
Showing
2 changed files
with
65 additions
and
9 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -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 { | ||
|
@@ -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, | ||
|
@@ -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); | ||
|
@@ -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: '*', | ||
|
@@ -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: '*', | ||
|
@@ -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); | ||
} | ||
} | ||
}); | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -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; | ||
} |