BookmarksBarButton.sys.mjs (3497B)
1 /* This Source Code Form is subject to the terms of the Mozilla Public License, 2 * v. 2.0. If a copy of the MPL was not distributed with this file, You can 3 * obtain one at http://mozilla.org/MPL/2.0/. */ 4 5 const lazy = {}; 6 7 ChromeUtils.defineESModuleGetters(lazy, { 8 CustomizableUI: 9 "moz-src:///browser/components/customizableui/CustomizableUI.sys.mjs", 10 SpecialMessageActions: 11 "resource://messaging-system/lib/SpecialMessageActions.sys.mjs", 12 ASRouter: "resource:///modules/asrouter/ASRouter.sys.mjs", 13 BrowserUsageTelemetry: "resource:///modules/BrowserUsageTelemetry.sys.mjs", 14 NimbusFeatures: "resource://nimbus/ExperimentAPI.sys.mjs", 15 }); 16 17 export const BookmarksBarButton = { 18 async showBookmarksBarButton(browser, message) { 19 const { label, action, logo } = message.content; 20 let { gBrowser } = browser.ownerGlobal; 21 const featureId = "fxms_bmb_button"; 22 const widgetId = "fxms-bmb-button"; 23 const supportedActions = ["OPEN_URL", "SET_PREF", "MULTI_ACTION"]; 24 25 const fxmsBookmarksBarBtn = { 26 id: widgetId, 27 l10nId: label?.string_id, 28 label: label?.raw, 29 tooltiptext: label?.tooltiptext, 30 defaultArea: lazy.CustomizableUI.AREA_BOOKMARKS, 31 type: "button", 32 33 handleExperimentUpdate() { 34 const value = lazy.NimbusFeatures[featureId].getAllVariables() || {}; 35 36 if (!Object.keys(value).length) { 37 lazy.CustomizableUI.removeWidgetFromArea(widgetId); 38 } 39 }, 40 41 onCreated(aNode) { 42 // This surface is for first-run experiments only 43 // Once the button is removed by the user or experiment unenrollment, it cannot be added again 44 lazy.NimbusFeatures[featureId].onUpdate(this.handleExperimentUpdate); 45 aNode.className = `bookmark-item chromeclass-toolbar-additional`; 46 if (logo?.imageURL) { 47 aNode.style.listStyleImage = `url(${logo.imageURL})`; 48 } 49 50 lazy.BrowserUsageTelemetry.recordWidgetChange( 51 widgetId, 52 lazy.CustomizableUI.AREA_BOOKMARKS, 53 "create" 54 ); 55 lazy.ASRouter.addImpression(message); 56 }, 57 58 onCommand() { 59 // Click telemetry is handled in BrowserUsageTelemetry, see 60 // _recordCommand() 61 if (supportedActions.includes(action.type)) { 62 switch (action.type) { 63 case "OPEN_URL": 64 case "SET_PREF": 65 lazy.SpecialMessageActions.handleAction(action, gBrowser); 66 break; 67 case "MULTI_ACTION": 68 if ( 69 action.data.actions.every(iAction => 70 supportedActions.includes(iAction.type) 71 ) 72 ) { 73 lazy.SpecialMessageActions.handleAction(action, gBrowser); 74 break; 75 } 76 } 77 } 78 79 if (action.navigate || action.dismiss) { 80 lazy.CustomizableUI.destroyWidget(widgetId); 81 } 82 }, 83 84 onWidgetRemoved() { 85 lazy.CustomizableUI.destroyWidget(widgetId); 86 }, 87 88 onDestroyed() { 89 lazy.CustomizableUI.removeListener(this); 90 lazy.BrowserUsageTelemetry.recordWidgetChange( 91 widgetId, 92 null, 93 "destroy" 94 ); 95 }, 96 }; 97 98 try { 99 lazy.CustomizableUI.addListener(fxmsBookmarksBarBtn); 100 lazy.CustomizableUI.createWidget(fxmsBookmarksBarBtn); 101 } catch (err) { 102 console.error( 103 "Error creating bookmarks bar button. It most likely already exists." 104 ); 105 } 106 }, 107 };