I have a bookmark context menu command that is enabled only when right-clicking on a bookmark or folder that is inside (a descendant of), say, Folder X.
My original implementation goes: My Command starts out disabled. When a node (bookmark or folder) is right-clicked, get its parentId
and check if it matches FOLDER_X_ID
, otherwise get that parent node’s parentId
and check, and so on recursively. If a match is found, enable My Command.
Snippet to illustrate:
browser.menus.create({
contexts: ['bookmarks'],
id: 'myCommand',
title: 'My Command',
enabled: false,
});
browser.menus.onShown.addListener (onMenuShow);
browser.menus.onHidden.addListener (onMenuHide);
async function onMenuShow({ bookmarkId }) {
if (bookmarkId && await isInsideFolderX(bookmarkId)) {
browser.menus.update('myCommand', { enabled: true });
browser.menus.refresh();
}
}
async function isInsideFolderX(bookmarkId) {
if (bookmarkId === 'FOLDER_X_ID' || isRootId(bookmarkId)) return false;
const parentId = (await getNode(bookmarkId)).parentId;
return parentId === 'FOLDER_X_ID' ? true : await isInsideFolderX(parentId);
}
const ROOT_IDS = ['toolbar_____', 'menu________', 'unfiled_____'];
const isRootId = bookmarkId => ROOT_IDS.includes(bookmarkId);
const getNode = async bookmarkId => (await browser.bookmarks.get(bookmarkId))[0];
function onMenuHide() {
browser.menus.update('myCommand', { enabled: false }); // Restore disabled status
}
When there are none or a few bookmarks in the tree (like in a fresh browser instance), upon right-clicking a node inside Folder X, My Command appears instantly enabled.
Meanwhile when there are about 7k+ bookmarks (like in my browser), upon doing the same as above, My Command takes about a second to visibly change from disabled to enabled.
This is even when right-clicking a direct child of Folder X, which rules out multiple serial calls to browser.bookmarks.get()
as the culprit.
What this tells me is when the bookmarks tree is large enough, simply accessing a single node once can take a significant amount of time.
This noticeable lag is poor UX; you might just assume the command is disabled if you didn’t wait long enough. Any suggestions to reduce the lag on browsers with lots and lots of bookmarks?