2015-10-16 01:15:04 +03:00
|
|
|
/* -*- Mode: indent-tabs-mode: nil; js-indent-level: 2 -*- */
|
|
|
|
/* vim: set sts=2 sw=2 et tw=80: */
|
|
|
|
"use strict";
|
|
|
|
|
2015-06-04 01:34:44 +03:00
|
|
|
XPCOMUtils.defineLazyModuleGetter(this, "CustomizableUI",
|
|
|
|
"resource:///modules/CustomizableUI.jsm");
|
2016-08-19 22:29:11 +03:00
|
|
|
XPCOMUtils.defineLazyModuleGetter(this, "clearTimeout",
|
|
|
|
"resource://gre/modules/Timer.jsm");
|
|
|
|
XPCOMUtils.defineLazyModuleGetter(this, "setTimeout",
|
|
|
|
"resource://gre/modules/Timer.jsm");
|
2017-01-29 02:00:15 +03:00
|
|
|
XPCOMUtils.defineLazyModuleGetter(this, "ViewPopup",
|
|
|
|
"resource:///modules/ExtensionPopups.jsm");
|
2015-06-04 01:34:44 +03:00
|
|
|
|
2016-10-21 17:03:58 +03:00
|
|
|
XPCOMUtils.defineLazyServiceGetter(this, "DOMUtils",
|
|
|
|
"@mozilla.org/inspector/dom-utils;1",
|
|
|
|
"inIDOMUtils");
|
2016-08-05 00:50:52 +03:00
|
|
|
|
2015-10-14 02:18:43 +03:00
|
|
|
Cu.import("resource://devtools/shared/event-emitter.js");
|
2015-06-04 01:34:44 +03:00
|
|
|
Cu.import("resource://gre/modules/ExtensionUtils.jsm");
|
2016-09-07 13:05:11 +03:00
|
|
|
Cu.import("resource://gre/modules/Task.jsm");
|
|
|
|
|
2015-09-15 21:19:45 +03:00
|
|
|
var {
|
2016-05-24 01:59:33 +03:00
|
|
|
IconDetails,
|
2017-01-31 09:44:16 +03:00
|
|
|
SingletonEventManager,
|
2015-06-04 01:34:44 +03:00
|
|
|
} = ExtensionUtils;
|
|
|
|
|
2016-08-19 22:29:11 +03:00
|
|
|
const POPUP_PRELOAD_TIMEOUT_MS = 200;
|
|
|
|
|
2016-01-16 02:14:25 +03:00
|
|
|
const XUL_NS = "http://www.mozilla.org/keymaster/gatekeeper/there.is.only.xul";
|
|
|
|
|
2016-10-21 01:27:51 +03:00
|
|
|
function isAncestorOrSelf(target, node) {
|
|
|
|
for (; node; node = node.parentNode) {
|
|
|
|
if (node === target) {
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2015-06-04 01:34:44 +03:00
|
|
|
// WeakMap[Extension -> BrowserAction]
|
2017-02-21 21:00:06 +03:00
|
|
|
const browserActionMap = new WeakMap();
|
|
|
|
|
|
|
|
const browserAreas = {
|
|
|
|
"navbar": CustomizableUI.AREA_NAVBAR,
|
|
|
|
"menupanel": CustomizableUI.AREA_PANEL,
|
|
|
|
"tabstrip": CustomizableUI.AREA_TABSTRIP,
|
|
|
|
"personaltoolbar": CustomizableUI.AREA_BOOKMARKS,
|
|
|
|
};
|
2015-06-04 01:34:44 +03:00
|
|
|
|
|
|
|
// Responsible for the browser_action section of the manifest as well
|
|
|
|
// as the associated popup.
|
2015-12-03 03:58:53 +03:00
|
|
|
function BrowserAction(options, extension) {
|
2015-06-04 01:34:44 +03:00
|
|
|
this.extension = extension;
|
2016-01-16 02:14:25 +03:00
|
|
|
|
|
|
|
let widgetId = makeWidgetId(extension.id);
|
|
|
|
this.id = `${widgetId}-browser-action`;
|
|
|
|
this.viewId = `PanelUI-webext-${widgetId}-browser-action-view`;
|
2015-06-04 01:34:44 +03:00
|
|
|
this.widget = null;
|
|
|
|
|
2016-08-19 22:29:11 +03:00
|
|
|
this.pendingPopup = null;
|
|
|
|
this.pendingPopupTimeout = null;
|
|
|
|
|
2017-01-31 09:44:16 +03:00
|
|
|
this.tabManager = extension.tabManager;
|
2015-12-02 07:37:41 +03:00
|
|
|
|
2015-10-16 01:14:49 +03:00
|
|
|
this.defaults = {
|
2015-10-28 12:27:29 +03:00
|
|
|
enabled: true,
|
2016-03-01 06:34:49 +03:00
|
|
|
title: options.default_title || extension.name,
|
2015-10-16 01:14:49 +03:00
|
|
|
badgeText: "",
|
|
|
|
badgeBackgroundColor: null,
|
2016-03-01 06:34:49 +03:00
|
|
|
icon: IconDetails.normalize({path: options.default_icon}, extension),
|
|
|
|
popup: options.default_popup || "",
|
2017-02-21 21:00:06 +03:00
|
|
|
area: browserAreas[options.default_area || "navbar"],
|
2015-10-16 01:14:49 +03:00
|
|
|
};
|
|
|
|
|
2016-05-02 17:59:36 +03:00
|
|
|
this.browserStyle = options.browser_style || false;
|
|
|
|
if (options.browser_style === null) {
|
|
|
|
this.extension.logger.warn("Please specify whether you want browser_style " +
|
|
|
|
"or not in your browser_action options.");
|
|
|
|
}
|
|
|
|
|
2015-10-16 01:14:49 +03:00
|
|
|
this.tabContext = new TabContext(tab => Object.create(this.defaults),
|
|
|
|
extension);
|
|
|
|
|
|
|
|
EventEmitter.decorate(this);
|
2015-06-04 01:34:44 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
BrowserAction.prototype = {
|
|
|
|
build() {
|
|
|
|
let widget = CustomizableUI.createWidget({
|
|
|
|
id: this.id,
|
2016-01-16 02:14:25 +03:00
|
|
|
viewId: this.viewId,
|
|
|
|
type: "view",
|
2015-06-04 01:34:44 +03:00
|
|
|
removable: true,
|
2016-01-16 02:14:25 +03:00
|
|
|
label: this.defaults.title || this.extension.name,
|
|
|
|
tooltiptext: this.defaults.title || "",
|
2017-02-21 21:00:06 +03:00
|
|
|
defaultArea: this.defaults.area,
|
2016-01-16 02:14:25 +03:00
|
|
|
|
|
|
|
onBeforeCreated: document => {
|
|
|
|
let view = document.createElementNS(XUL_NS, "panelview");
|
|
|
|
view.id = this.viewId;
|
|
|
|
view.setAttribute("flex", "1");
|
|
|
|
|
|
|
|
document.getElementById("PanelUI-multiView").appendChild(view);
|
2016-11-09 23:03:33 +03:00
|
|
|
document.addEventListener("popupshowing", this);
|
2016-01-16 02:14:25 +03:00
|
|
|
},
|
|
|
|
|
|
|
|
onDestroyed: document => {
|
|
|
|
let view = document.getElementById(this.viewId);
|
|
|
|
if (view) {
|
2016-10-21 01:27:51 +03:00
|
|
|
this.clearPopup();
|
|
|
|
CustomizableUI.hidePanelForNode(view);
|
2016-01-16 02:14:25 +03:00
|
|
|
view.remove();
|
|
|
|
}
|
2016-11-09 23:03:33 +03:00
|
|
|
document.removeEventListener("popupshowing", this);
|
2016-01-16 02:14:25 +03:00
|
|
|
},
|
|
|
|
|
|
|
|
onCreated: node => {
|
|
|
|
node.classList.add("badged-button");
|
2016-07-14 01:16:00 +03:00
|
|
|
node.classList.add("webextension-browser-action");
|
2015-06-04 01:34:44 +03:00
|
|
|
node.setAttribute("constrain-size", "true");
|
|
|
|
|
2016-08-19 22:29:11 +03:00
|
|
|
node.onmousedown = event => this.handleEvent(event);
|
|
|
|
|
2015-10-16 01:14:49 +03:00
|
|
|
this.updateButton(node, this.defaults);
|
2016-01-16 02:14:25 +03:00
|
|
|
},
|
2015-06-04 01:34:44 +03:00
|
|
|
|
2016-01-16 02:14:25 +03:00
|
|
|
onViewShowing: event => {
|
|
|
|
let document = event.target.ownerDocument;
|
2015-06-04 01:34:44 +03:00
|
|
|
let tabbrowser = document.defaultView.gBrowser;
|
|
|
|
|
2016-01-16 02:14:25 +03:00
|
|
|
let tab = tabbrowser.selectedTab;
|
|
|
|
let popupURL = this.getProperty(tab, "popup");
|
|
|
|
this.tabManager.addActiveTabPermission(tab);
|
|
|
|
|
2016-09-07 13:05:11 +03:00
|
|
|
// Popups are shown only if a popup URL is defined; otherwise
|
|
|
|
// a "click" event is dispatched. This is done for compatibility with the
|
|
|
|
// Google Chrome onClicked extension API.
|
2016-01-16 02:14:25 +03:00
|
|
|
if (popupURL) {
|
|
|
|
try {
|
2016-08-19 22:29:11 +03:00
|
|
|
let popup = this.getPopup(document.defaultView, popupURL);
|
|
|
|
event.detail.addBlocker(popup.attach(event.target));
|
2016-01-16 02:14:25 +03:00
|
|
|
} catch (e) {
|
|
|
|
Cu.reportError(e);
|
|
|
|
event.preventDefault();
|
2015-06-04 01:34:44 +03:00
|
|
|
}
|
2016-01-16 02:14:25 +03:00
|
|
|
} else {
|
|
|
|
// This isn't not a hack, but it seems to provide the correct behavior
|
|
|
|
// with the fewest complications.
|
|
|
|
event.preventDefault();
|
|
|
|
this.emit("click");
|
|
|
|
}
|
2015-06-04 01:34:44 +03:00
|
|
|
},
|
|
|
|
});
|
|
|
|
|
2015-12-03 03:58:53 +03:00
|
|
|
this.tabContext.on("tab-select", // eslint-disable-line mozilla/balanced-listeners
|
2016-07-16 11:20:04 +03:00
|
|
|
(evt, tab) => { this.updateWindow(tab.ownerGlobal); });
|
2015-10-16 01:14:49 +03:00
|
|
|
|
|
|
|
this.widget = widget;
|
2015-08-30 03:15:22 +03:00
|
|
|
},
|
|
|
|
|
2016-09-07 13:05:11 +03:00
|
|
|
/**
|
|
|
|
* Triggers this browser action for the given window, with the same effects as
|
|
|
|
* if it were clicked by a user.
|
|
|
|
*
|
|
|
|
* This has no effect if the browser action is disabled for, or not
|
|
|
|
* present in, the given window.
|
|
|
|
*/
|
|
|
|
triggerAction: Task.async(function* (window) {
|
|
|
|
let popup = ViewPopup.for(this.extension, window);
|
|
|
|
if (popup) {
|
|
|
|
popup.closePopup();
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
let widget = this.widget.forWindow(window);
|
|
|
|
let tab = window.gBrowser.selectedTab;
|
|
|
|
|
|
|
|
if (!widget || !this.getProperty(tab, "enabled")) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Popups are shown only if a popup URL is defined; otherwise
|
|
|
|
// a "click" event is dispatched. This is done for compatibility with the
|
|
|
|
// Google Chrome onClicked extension API.
|
|
|
|
if (this.getProperty(tab, "popup")) {
|
|
|
|
if (this.widget.areaType == CustomizableUI.TYPE_MENU_PANEL) {
|
|
|
|
yield window.PanelUI.show();
|
|
|
|
}
|
|
|
|
|
|
|
|
let event = new window.CustomEvent("command", {bubbles: true, cancelable: true});
|
|
|
|
widget.node.dispatchEvent(event);
|
|
|
|
} else {
|
|
|
|
this.emit("click");
|
|
|
|
}
|
|
|
|
}),
|
|
|
|
|
2016-08-19 22:29:11 +03:00
|
|
|
handleEvent(event) {
|
|
|
|
let button = event.target;
|
2017-01-27 12:51:03 +03:00
|
|
|
let window = button.ownerGlobal;
|
2016-08-19 22:29:11 +03:00
|
|
|
|
|
|
|
switch (event.type) {
|
|
|
|
case "mousedown":
|
|
|
|
if (event.button == 0) {
|
|
|
|
// Begin pre-loading the browser for the popup, so it's more likely to
|
|
|
|
// be ready by the time we get a complete click.
|
|
|
|
let tab = window.gBrowser.selectedTab;
|
|
|
|
let popupURL = this.getProperty(tab, "popup");
|
2016-11-01 19:41:30 +03:00
|
|
|
let enabled = this.getProperty(tab, "enabled");
|
2016-08-19 22:29:11 +03:00
|
|
|
|
2016-11-01 19:41:30 +03:00
|
|
|
if (popupURL && enabled) {
|
2016-11-01 00:30:16 +03:00
|
|
|
// Add permission for the active tab so it will exist for the popup.
|
|
|
|
// Store the tab to revoke the permission during clearPopup.
|
|
|
|
if (!this.pendingPopup && !this.tabManager.hasActiveTabPermission(tab)) {
|
|
|
|
this.tabManager.addActiveTabPermission(tab);
|
|
|
|
this.tabToRevokeDuringClearPopup = tab;
|
|
|
|
}
|
|
|
|
|
2016-08-19 22:29:11 +03:00
|
|
|
this.pendingPopup = this.getPopup(window, popupURL);
|
|
|
|
window.addEventListener("mouseup", this, true);
|
|
|
|
} else {
|
|
|
|
this.clearPopup();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
|
|
|
|
case "mouseup":
|
|
|
|
if (event.button == 0) {
|
|
|
|
this.clearPopupTimeout();
|
|
|
|
// If we have a pending pre-loaded popup, cancel it after we've waited
|
|
|
|
// long enough that we can be relatively certain it won't be opening.
|
|
|
|
if (this.pendingPopup) {
|
2017-02-06 22:55:53 +03:00
|
|
|
let node = window.gBrowser && this.widget.forWindow(window).node;
|
2016-10-21 01:27:51 +03:00
|
|
|
if (isAncestorOrSelf(node, event.originalTarget)) {
|
2016-08-19 22:29:11 +03:00
|
|
|
this.pendingPopupTimeout = setTimeout(() => this.clearPopup(),
|
|
|
|
POPUP_PRELOAD_TIMEOUT_MS);
|
|
|
|
} else {
|
|
|
|
this.clearPopup();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
break;
|
2016-11-09 23:03:33 +03:00
|
|
|
|
|
|
|
case "popupshowing":
|
|
|
|
const menu = event.target;
|
|
|
|
const trigger = menu.triggerNode;
|
|
|
|
const node = window.document.getElementById(this.id);
|
2016-11-28 05:15:02 +03:00
|
|
|
const contexts = ["toolbar-context-menu", "customizationPanelItemContextMenu"];
|
2016-11-09 23:03:33 +03:00
|
|
|
|
2016-11-28 05:15:02 +03:00
|
|
|
if (contexts.includes(menu.id) && node && isAncestorOrSelf(node, trigger)) {
|
2016-11-09 23:03:33 +03:00
|
|
|
global.actionContextMenu({
|
|
|
|
extension: this.extension,
|
|
|
|
onBrowserAction: true,
|
|
|
|
menu: menu,
|
|
|
|
});
|
|
|
|
}
|
|
|
|
break;
|
2016-08-19 22:29:11 +03:00
|
|
|
}
|
|
|
|
},
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Returns a potentially pre-loaded popup for the given URL in the given
|
|
|
|
* window. If a matching pre-load popup already exists, returns that.
|
|
|
|
* Otherwise, initializes a new one.
|
|
|
|
*
|
|
|
|
* If a pre-load popup exists which does not match, it is destroyed before a
|
|
|
|
* new one is created.
|
|
|
|
*
|
|
|
|
* @param {Window} window
|
|
|
|
* The browser window in which to create the popup.
|
|
|
|
* @param {string} popupURL
|
|
|
|
* The URL to load into the popup.
|
|
|
|
* @returns {ViewPopup}
|
|
|
|
*/
|
|
|
|
getPopup(window, popupURL) {
|
|
|
|
this.clearPopupTimeout();
|
|
|
|
let {pendingPopup} = this;
|
|
|
|
this.pendingPopup = null;
|
|
|
|
|
|
|
|
if (pendingPopup) {
|
|
|
|
if (pendingPopup.window === window && pendingPopup.popupURL === popupURL) {
|
|
|
|
return pendingPopup;
|
|
|
|
}
|
|
|
|
pendingPopup.destroy();
|
|
|
|
}
|
|
|
|
|
|
|
|
let fixedWidth = this.widget.areaType == CustomizableUI.TYPE_MENU_PANEL;
|
|
|
|
return new ViewPopup(this.extension, window, popupURL, this.browserStyle, fixedWidth);
|
|
|
|
},
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Clears any pending pre-loaded popup and related timeouts.
|
|
|
|
*/
|
|
|
|
clearPopup() {
|
|
|
|
this.clearPopupTimeout();
|
|
|
|
if (this.pendingPopup) {
|
2016-11-01 00:30:16 +03:00
|
|
|
if (this.tabToRevokeDuringClearPopup) {
|
|
|
|
this.tabManager.revokeActiveTabPermission(this.tabToRevokeDuringClearPopup);
|
|
|
|
this.tabToRevokeDuringClearPopup = null;
|
|
|
|
}
|
2016-08-19 22:29:11 +03:00
|
|
|
this.pendingPopup.destroy();
|
|
|
|
this.pendingPopup = null;
|
|
|
|
}
|
|
|
|
},
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Clears any pending timeouts to clear stale, pre-loaded popups.
|
|
|
|
*/
|
|
|
|
clearPopupTimeout() {
|
|
|
|
if (this.pendingPopup) {
|
|
|
|
this.pendingPopup.window.removeEventListener("mouseup", this, true);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (this.pendingPopupTimeout) {
|
|
|
|
clearTimeout(this.pendingPopupTimeout);
|
|
|
|
this.pendingPopupTimeout = null;
|
|
|
|
}
|
|
|
|
},
|
|
|
|
|
2015-10-16 01:14:49 +03:00
|
|
|
// Update the toolbar button |node| with the tab context data
|
|
|
|
// in |tabData|.
|
|
|
|
updateButton(node, tabData) {
|
2016-01-16 08:43:26 +03:00
|
|
|
let title = tabData.title || this.extension.name;
|
|
|
|
node.setAttribute("tooltiptext", title);
|
|
|
|
node.setAttribute("label", title);
|
2015-06-04 01:34:44 +03:00
|
|
|
|
2015-10-16 01:14:49 +03:00
|
|
|
if (tabData.badgeText) {
|
|
|
|
node.setAttribute("badge", tabData.badgeText);
|
2015-06-04 01:34:44 +03:00
|
|
|
} else {
|
|
|
|
node.removeAttribute("badge");
|
|
|
|
}
|
|
|
|
|
2015-10-28 12:27:29 +03:00
|
|
|
if (tabData.enabled) {
|
|
|
|
node.removeAttribute("disabled");
|
|
|
|
} else {
|
|
|
|
node.setAttribute("disabled", "true");
|
|
|
|
}
|
|
|
|
|
2015-06-04 01:34:44 +03:00
|
|
|
let badgeNode = node.ownerDocument.getAnonymousElementByAttribute(node,
|
2015-12-03 03:58:53 +03:00
|
|
|
"class", "toolbarbutton-badge");
|
2015-06-04 01:34:44 +03:00
|
|
|
if (badgeNode) {
|
2015-10-16 01:14:49 +03:00
|
|
|
let color = tabData.badgeBackgroundColor;
|
2016-08-05 00:50:52 +03:00
|
|
|
if (color) {
|
|
|
|
color = `rgba(${color[0]}, ${color[1]}, ${color[2]}, ${color[3] / 255})`;
|
2015-06-04 01:34:44 +03:00
|
|
|
}
|
2015-10-16 01:15:04 +03:00
|
|
|
badgeNode.style.backgroundColor = color || "";
|
2015-06-04 01:34:44 +03:00
|
|
|
}
|
|
|
|
|
2016-06-06 07:05:36 +03:00
|
|
|
const LEGACY_CLASS = "toolbarbutton-legacy-addon";
|
|
|
|
node.classList.remove(LEGACY_CLASS);
|
|
|
|
|
2016-07-14 01:16:00 +03:00
|
|
|
let baseSize = 16;
|
|
|
|
let {icon, size} = IconDetails.getPreferredIcon(tabData.icon, this.extension, baseSize);
|
2016-06-06 07:05:36 +03:00
|
|
|
|
|
|
|
// If the best available icon size is not divisible by 16, check if we have
|
|
|
|
// an 18px icon to fall back to, and trim off the padding instead.
|
|
|
|
if (size % 16 && !icon.endsWith(".svg")) {
|
2016-07-14 01:16:00 +03:00
|
|
|
let result = IconDetails.getPreferredIcon(tabData.icon, this.extension, 18);
|
2016-06-06 07:05:36 +03:00
|
|
|
|
|
|
|
if (result.size % 18 == 0) {
|
2016-07-14 01:16:00 +03:00
|
|
|
baseSize = 18;
|
2016-06-06 07:05:36 +03:00
|
|
|
icon = result.icon;
|
|
|
|
node.classList.add(LEGACY_CLASS);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-02-10 02:32:50 +03:00
|
|
|
let getIcon = size => IconDetails.escapeUrl(
|
|
|
|
IconDetails.getPreferredIcon(tabData.icon, this.extension, size).icon);
|
2016-07-14 01:16:00 +03:00
|
|
|
|
|
|
|
node.setAttribute("style", `
|
|
|
|
--webextension-menupanel-image: url("${getIcon(32)}");
|
|
|
|
--webextension-menupanel-image-2x: url("${getIcon(64)}");
|
2017-02-10 02:32:50 +03:00
|
|
|
--webextension-toolbar-image: url("${IconDetails.escapeUrl(icon)}");
|
2016-07-14 01:16:00 +03:00
|
|
|
--webextension-toolbar-image-2x: url("${getIcon(baseSize * 2)}");
|
|
|
|
`);
|
2015-06-04 01:34:44 +03:00
|
|
|
},
|
|
|
|
|
|
|
|
// Update the toolbar button for a given window.
|
|
|
|
updateWindow(window) {
|
2015-10-16 01:14:49 +03:00
|
|
|
let widget = this.widget.forWindow(window);
|
|
|
|
if (widget) {
|
|
|
|
let tab = window.gBrowser.selectedTab;
|
|
|
|
this.updateButton(widget.node, this.tabContext.get(tab));
|
|
|
|
}
|
2015-06-04 01:34:44 +03:00
|
|
|
},
|
|
|
|
|
|
|
|
// Update the toolbar button when the extension changes the icon,
|
|
|
|
// title, badge, etc. If it only changes a parameter for a single
|
|
|
|
// tab, |tab| will be that tab. Otherwise it will be null.
|
|
|
|
updateOnChange(tab) {
|
|
|
|
if (tab) {
|
|
|
|
if (tab.selected) {
|
2016-07-16 11:20:04 +03:00
|
|
|
this.updateWindow(tab.ownerGlobal);
|
2015-06-04 01:34:44 +03:00
|
|
|
}
|
|
|
|
} else {
|
2017-01-31 09:44:16 +03:00
|
|
|
for (let window of windowTracker.browserWindows()) {
|
2015-10-16 01:14:49 +03:00
|
|
|
this.updateWindow(window);
|
2015-06-04 01:34:44 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
},
|
|
|
|
|
|
|
|
// tab is allowed to be null.
|
|
|
|
// prop should be one of "icon", "title", "badgeText", "popup", or "badgeBackgroundColor".
|
|
|
|
setProperty(tab, prop, value) {
|
2015-10-16 01:14:49 +03:00
|
|
|
if (tab == null) {
|
|
|
|
this.defaults[prop] = value;
|
2016-01-16 08:43:26 +03:00
|
|
|
} else if (value != null) {
|
2016-01-18 11:08:35 +03:00
|
|
|
this.tabContext.get(tab)[prop] = value;
|
2016-01-16 08:43:26 +03:00
|
|
|
} else {
|
|
|
|
delete this.tabContext.get(tab)[prop];
|
2015-10-16 01:14:49 +03:00
|
|
|
}
|
|
|
|
|
2015-06-04 01:34:44 +03:00
|
|
|
this.updateOnChange(tab);
|
|
|
|
},
|
|
|
|
|
|
|
|
// tab is allowed to be null.
|
|
|
|
// prop should be one of "title", "badgeText", "popup", or "badgeBackgroundColor".
|
|
|
|
getProperty(tab, prop) {
|
2015-10-16 01:14:49 +03:00
|
|
|
if (tab == null) {
|
|
|
|
return this.defaults[prop];
|
|
|
|
}
|
2016-08-04 01:54:59 +03:00
|
|
|
return this.tabContext.get(tab)[prop];
|
2015-06-04 01:34:44 +03:00
|
|
|
},
|
|
|
|
|
|
|
|
shutdown() {
|
2015-10-16 01:14:49 +03:00
|
|
|
this.tabContext.shutdown();
|
2015-06-04 01:34:44 +03:00
|
|
|
CustomizableUI.destroyWidget(this.id);
|
|
|
|
},
|
|
|
|
};
|
|
|
|
|
2016-03-14 16:54:57 +03:00
|
|
|
BrowserAction.for = (extension) => {
|
|
|
|
return browserActionMap.get(extension);
|
|
|
|
};
|
|
|
|
|
|
|
|
global.browserActionFor = BrowserAction.for;
|
|
|
|
|
2015-12-03 03:58:53 +03:00
|
|
|
/* eslint-disable mozilla/balanced-listeners */
|
2015-06-04 01:34:44 +03:00
|
|
|
extensions.on("manifest_browser_action", (type, directive, extension, manifest) => {
|
|
|
|
let browserAction = new BrowserAction(manifest.browser_action, extension);
|
|
|
|
browserAction.build();
|
|
|
|
browserActionMap.set(extension, browserAction);
|
|
|
|
});
|
|
|
|
|
|
|
|
extensions.on("shutdown", (type, extension) => {
|
|
|
|
if (browserActionMap.has(extension)) {
|
|
|
|
browserActionMap.get(extension).shutdown();
|
|
|
|
browserActionMap.delete(extension);
|
|
|
|
}
|
|
|
|
});
|
2015-12-03 03:58:53 +03:00
|
|
|
/* eslint-enable mozilla/balanced-listeners */
|
2015-06-04 01:34:44 +03:00
|
|
|
|
2016-08-17 01:51:50 +03:00
|
|
|
extensions.registerSchemaAPI("browserAction", "addon_parent", context => {
|
2016-08-15 11:04:58 +03:00
|
|
|
let {extension} = context;
|
2017-01-31 09:44:16 +03:00
|
|
|
|
|
|
|
let {tabManager} = extension;
|
|
|
|
|
|
|
|
function getTab(tabId) {
|
|
|
|
if (tabId !== null) {
|
|
|
|
return tabTracker.getTab(tabId);
|
|
|
|
}
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
|
2015-06-04 01:34:44 +03:00
|
|
|
return {
|
|
|
|
browserAction: {
|
2017-01-26 22:27:03 +03:00
|
|
|
onClicked: new SingletonEventManager(context, "browserAction.onClicked", fire => {
|
2015-06-04 01:34:44 +03:00
|
|
|
let listener = () => {
|
2017-01-31 09:44:16 +03:00
|
|
|
fire.async(tabManager.convert(tabTracker.activeTab));
|
2015-06-04 01:34:44 +03:00
|
|
|
};
|
2016-03-14 16:54:57 +03:00
|
|
|
BrowserAction.for(extension).on("click", listener);
|
2015-06-04 01:34:44 +03:00
|
|
|
return () => {
|
2016-03-14 16:54:57 +03:00
|
|
|
BrowserAction.for(extension).off("click", listener);
|
2015-06-04 01:34:44 +03:00
|
|
|
};
|
|
|
|
}).api(),
|
|
|
|
|
2015-10-28 12:27:29 +03:00
|
|
|
enable: function(tabId) {
|
2017-01-31 09:44:16 +03:00
|
|
|
let tab = getTab(tabId);
|
2016-03-14 16:54:57 +03:00
|
|
|
BrowserAction.for(extension).setProperty(tab, "enabled", true);
|
2015-10-28 12:27:29 +03:00
|
|
|
},
|
|
|
|
|
|
|
|
disable: function(tabId) {
|
2017-01-31 09:44:16 +03:00
|
|
|
let tab = getTab(tabId);
|
2016-03-14 16:54:57 +03:00
|
|
|
BrowserAction.for(extension).setProperty(tab, "enabled", false);
|
2015-10-28 12:27:29 +03:00
|
|
|
},
|
|
|
|
|
2015-06-04 01:34:44 +03:00
|
|
|
setTitle: function(details) {
|
2017-01-31 09:44:16 +03:00
|
|
|
let tab = getTab(details.tabId);
|
2016-01-16 08:43:26 +03:00
|
|
|
|
|
|
|
let title = details.title;
|
|
|
|
// Clear the tab-specific title when given a null string.
|
|
|
|
if (tab && title == "") {
|
|
|
|
title = null;
|
|
|
|
}
|
2016-03-14 16:54:57 +03:00
|
|
|
BrowserAction.for(extension).setProperty(tab, "title", title);
|
2015-06-04 01:34:44 +03:00
|
|
|
},
|
|
|
|
|
2016-02-02 05:14:05 +03:00
|
|
|
getTitle: function(details) {
|
2017-01-31 09:44:16 +03:00
|
|
|
let tab = getTab(details.tabId);
|
2016-02-27 04:56:30 +03:00
|
|
|
|
2016-03-14 16:54:57 +03:00
|
|
|
let title = BrowserAction.for(extension).getProperty(tab, "title");
|
2016-02-02 05:14:05 +03:00
|
|
|
return Promise.resolve(title);
|
2015-06-04 01:34:44 +03:00
|
|
|
},
|
|
|
|
|
2016-02-02 05:14:05 +03:00
|
|
|
setIcon: function(details) {
|
2017-01-31 09:44:16 +03:00
|
|
|
let tab = getTab(details.tabId);
|
2016-02-27 04:56:30 +03:00
|
|
|
|
2015-10-16 01:15:04 +03:00
|
|
|
let icon = IconDetails.normalize(details, extension, context);
|
2016-03-14 16:54:57 +03:00
|
|
|
BrowserAction.for(extension).setProperty(tab, "icon", icon);
|
2015-06-04 01:34:44 +03:00
|
|
|
},
|
|
|
|
|
|
|
|
setBadgeText: function(details) {
|
2017-01-31 09:44:16 +03:00
|
|
|
let tab = getTab(details.tabId);
|
2016-02-27 04:56:30 +03:00
|
|
|
|
2016-03-14 16:54:57 +03:00
|
|
|
BrowserAction.for(extension).setProperty(tab, "badgeText", details.text);
|
2015-06-04 01:34:44 +03:00
|
|
|
},
|
|
|
|
|
2016-02-02 05:14:05 +03:00
|
|
|
getBadgeText: function(details) {
|
2017-01-31 09:44:16 +03:00
|
|
|
let tab = getTab(details.tabId);
|
2016-02-27 04:56:30 +03:00
|
|
|
|
2016-03-14 16:54:57 +03:00
|
|
|
let text = BrowserAction.for(extension).getProperty(tab, "badgeText");
|
2016-02-02 05:14:05 +03:00
|
|
|
return Promise.resolve(text);
|
2015-06-04 01:34:44 +03:00
|
|
|
},
|
|
|
|
|
|
|
|
setPopup: function(details) {
|
2017-01-31 09:44:16 +03:00
|
|
|
let tab = getTab(details.tabId);
|
2016-02-27 04:56:30 +03:00
|
|
|
|
2015-10-16 01:14:49 +03:00
|
|
|
// Note: Chrome resolves arguments to setIcon relative to the calling
|
|
|
|
// context, but resolves arguments to setPopup relative to the extension
|
|
|
|
// root.
|
|
|
|
// For internal consistency, we currently resolve both relative to the
|
|
|
|
// calling context.
|
|
|
|
let url = details.popup && context.uri.resolve(details.popup);
|
2016-03-14 16:54:57 +03:00
|
|
|
BrowserAction.for(extension).setProperty(tab, "popup", url);
|
2015-06-04 01:34:44 +03:00
|
|
|
},
|
|
|
|
|
2016-02-02 05:14:05 +03:00
|
|
|
getPopup: function(details) {
|
2017-01-31 09:44:16 +03:00
|
|
|
let tab = getTab(details.tabId);
|
2016-02-27 04:56:30 +03:00
|
|
|
|
2016-03-14 16:54:57 +03:00
|
|
|
let popup = BrowserAction.for(extension).getProperty(tab, "popup");
|
2016-02-02 05:14:05 +03:00
|
|
|
return Promise.resolve(popup);
|
2015-06-04 01:34:44 +03:00
|
|
|
},
|
|
|
|
|
|
|
|
setBadgeBackgroundColor: function(details) {
|
2017-01-31 09:44:16 +03:00
|
|
|
let tab = getTab(details.tabId);
|
2016-08-05 00:50:52 +03:00
|
|
|
let color = details.color;
|
|
|
|
if (!Array.isArray(color)) {
|
2016-10-21 17:03:58 +03:00
|
|
|
let col = DOMUtils.colorToRGBA(color);
|
2016-08-05 00:50:52 +03:00
|
|
|
color = col && [col.r, col.g, col.b, Math.round(col.a * 255)];
|
|
|
|
}
|
|
|
|
BrowserAction.for(extension).setProperty(tab, "badgeBackgroundColor", color);
|
2015-06-04 01:34:44 +03:00
|
|
|
},
|
|
|
|
|
|
|
|
getBadgeBackgroundColor: function(details, callback) {
|
2017-01-31 09:44:16 +03:00
|
|
|
let tab = getTab(details.tabId);
|
2016-02-27 04:56:30 +03:00
|
|
|
|
2016-03-14 16:54:57 +03:00
|
|
|
let color = BrowserAction.for(extension).getProperty(tab, "badgeBackgroundColor");
|
2016-08-05 00:50:52 +03:00
|
|
|
return Promise.resolve(color || [0xd9, 0, 0, 255]);
|
2015-06-04 01:34:44 +03:00
|
|
|
},
|
2015-12-03 03:58:53 +03:00
|
|
|
},
|
2015-06-04 01:34:44 +03:00
|
|
|
};
|
|
|
|
});
|