commit 89f7e0c88faf97328ea3acc99b36faf0f55f7fe5 parent 11e002e0a7a333752763d2bb439c3d59a559e9f3 Author: Tooru Fujisawa <arai_a@mac.com> Date: Thu, 16 Oct 2025 13:40:39 +0000 Bug 1993731 - Do not show the Ask an AI Chatbot menu item asynchronously. r=Mardak,firefox-ai-ml-reviewers,sclements Differential Revision: https://phabricator.services.mozilla.com/D268488 Diffstat:
5 files changed, 24 insertions(+), 15 deletions(-)
diff --git a/browser/base/content/nsContextMenu.sys.mjs b/browser/base/content/nsContextMenu.sys.mjs @@ -1381,7 +1381,7 @@ export class nsContextMenu { count = 0; } else if (!menuItem.hidden) { - if (menuItem.localName == "menu") { + if (menuItem.localName == "menu" && menuItem.menupopup) { this.showHideSeparators(menuItem.menupopup); } else if (menuItem.localName == "menugroup") { this.showHideSeparators(menuItem); diff --git a/browser/base/content/test/contextMenu/browser_contextmenu_contenteditable.js b/browser/base/content/test/contextMenu/browser_contextmenu_contenteditable.js @@ -23,6 +23,8 @@ const askChatMenu = [ // Need a blank entry here because the Ask Chat submenu is dynamically built with no ids. "", null, + "---", + null, ]; async function openMenuAndPaste(browser, useFormatting) { diff --git a/browser/base/content/test/contextMenu/contextmenu_common.js b/browser/base/content/test/contextMenu/contextmenu_common.js @@ -421,7 +421,7 @@ async function test_contextmenu(selector, menuItems, options = {}) { menuItems.includes("context-viewpartialsource-selection"); const askChatIndex = menuItems.indexOf("context-ask-chat"); - const isAskChatLastItem = menuItems.at(-4) === "context-ask-chat"; + const isAskChatLastItem = menuItems.at(-6) === "context-ask-chat"; if (askChatIndex >= 2) { hasSeparatorAboveAskChat = menuItems[askChatIndex - 2] === "---"; } diff --git a/browser/components/genai/GenAI.sys.mjs b/browser/components/genai/GenAI.sys.mjs @@ -670,11 +670,10 @@ export const GenAI = { contextTabs = null, } = contextMenu; - showItem(menu, false); - // DO NOT show menu when inside an extension panel const uri = browser.browsingContext.currentURI.spec; if (uri.startsWith("moz-extension:")) { + showItem(menu, false); return; } @@ -698,6 +697,7 @@ export const GenAI = { break; } if (!canShow) { + showItem(menu, false); return; } @@ -719,6 +719,9 @@ export const GenAI = { menu.menupopup?.remove(); } + // NOTE: Show the menu item synchronously, before any `await`. + showItem(menu, true); + // Determine if we have selection or should use page content const context = { contentType: "selection", @@ -806,8 +809,6 @@ export const GenAI = { Services.prefs.setBoolPref("browser.ml.chat.menu", false); } }); - - showItem(menu, true); }, /** diff --git a/browser/components/sidebar/tests/browser/browser_sidebar_context_menu.js b/browser/components/sidebar/tests/browser/browser_sidebar_context_menu.js @@ -330,17 +330,23 @@ add_task(async function test_tool_context_menu() { contextMenu.querySelector("menuseparator").hidden, "menuseparator is hidden" ); - - const toolMenuItems = [ - ...contextMenu.querySelectorAll("[customized-tool='true']"), - ]; - Assert.equal( - !!toolMenuItems.length && toolMenuItems.every(item => !item.hidden), - true, - "Tool menuitems are visible" - ); }); + // The submenu is populated asynchronously. + await BrowserTestUtils.waitForMutationCondition( + contextMenu, + { childList: true, subtree: true, attributes: true }, + () => { + const toolMenuItems = [ + ...contextMenu.querySelectorAll("[customized-tool='true']"), + ]; + return ( + !!toolMenuItems.length && toolMenuItems.every(item => !item.hidden) + ); + } + ); + Assert.ok(true, "Tool menuitems are visible"); + contextMenu.hidePopup(); await openAndWaitForContextMenu(contextMenu, sidebarMain, () => {