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";
|
|
|
|
|
2017-06-08 12:59:12 +03:00
|
|
|
/* exported browserActionFor, sidebarActionFor, pageActionFor */
|
|
|
|
/* global browserActionFor:false, sidebarActionFor:false, pageActionFor:false */
|
|
|
|
|
|
|
|
// The ext-* files are imported into the same scopes.
|
2017-08-07 04:20:25 +03:00
|
|
|
/* import-globals-from ext-browser.js */
|
2017-06-08 12:59:12 +03:00
|
|
|
|
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-05-03 18:12:03 +03:00
|
|
|
XPCOMUtils.defineLazyModuleGetter(this, "TelemetryStopwatch",
|
|
|
|
"resource://gre/modules/TelemetryStopwatch.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-09-15 21:19:45 +03:00
|
|
|
var {
|
2017-05-06 00:21:07 +03:00
|
|
|
DefaultWeakMap,
|
2015-06-04 01:34:44 +03:00
|
|
|
} = ExtensionUtils;
|
|
|
|
|
2017-05-27 01:44:41 +03:00
|
|
|
Cu.import("resource://gre/modules/ExtensionParent.jsm");
|
|
|
|
|
|
|
|
var {
|
|
|
|
IconDetails,
|
2017-08-07 02:12:14 +03:00
|
|
|
StartupCache,
|
2017-05-27 01:44:41 +03:00
|
|
|
} = ExtensionParent;
|
|
|
|
|
2016-08-19 22:29:11 +03:00
|
|
|
const POPUP_PRELOAD_TIMEOUT_MS = 200;
|
2017-05-03 18:12:03 +03:00
|
|
|
const POPUP_OPEN_MS_HISTOGRAM = "WEBEXT_BROWSERACTION_POPUP_OPEN_MS";
|
|
|
|
const POPUP_RESULT_HISTOGRAM = "WEBEXT_BROWSERACTION_POPUP_PRELOAD_RESULT_COUNT";
|
2016-08-19 22:29:11 +03:00
|
|
|
|
2017-04-02 21:59:55 +03:00
|
|
|
var XUL_NS = "http://www.mozilla.org/keymaster/gatekeeper/there.is.only.xul";
|
2016-01-16 02:14:25 +03:00
|
|
|
|
2017-06-21 23:13:00 +03:00
|
|
|
const isAncestorOrSelf = (target, node) => {
|
2016-10-21 01:27:51 +03:00
|
|
|
for (; node; node = node.parentNode) {
|
|
|
|
if (node === target) {
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return false;
|
2017-06-21 23:13:00 +03:00
|
|
|
};
|
2016-10-21 01:27:51 +03:00
|
|
|
|
2015-06-04 01:34:44 +03:00
|
|
|
// WeakMap[Extension -> BrowserAction]
|
2017-02-23 05:47:16 +03:00
|
|
|
const browserActionMap = new WeakMap();
|
|
|
|
|
2017-03-04 03:15:06 +03:00
|
|
|
XPCOMUtils.defineLazyGetter(this, "browserAreas", () => {
|
|
|
|
return {
|
|
|
|
"navbar": CustomizableUI.AREA_NAVBAR,
|
2017-08-01 20:37:41 +03:00
|
|
|
"menupanel": CustomizableUI.AREA_FIXED_OVERFLOW_PANEL,
|
2017-03-04 03:15:06 +03:00
|
|
|
"tabstrip": CustomizableUI.AREA_TABSTRIP,
|
|
|
|
"personaltoolbar": CustomizableUI.AREA_BOOKMARKS,
|
|
|
|
};
|
|
|
|
});
|
2015-06-04 01:34:44 +03:00
|
|
|
|
2017-03-27 04:42:59 +03:00
|
|
|
this.browserAction = class extends ExtensionAPI {
|
|
|
|
static for(extension) {
|
|
|
|
return browserActionMap.get(extension);
|
|
|
|
}
|
|
|
|
|
2017-08-07 02:12:14 +03:00
|
|
|
async onManifestEntry(entryName) {
|
2017-03-27 04:42:59 +03:00
|
|
|
let {extension} = this;
|
|
|
|
|
|
|
|
let options = extension.manifest.browser_action;
|
|
|
|
|
2017-05-06 00:21:07 +03:00
|
|
|
this.iconData = new DefaultWeakMap(icons => this.getIconData(icons));
|
|
|
|
|
2017-03-27 04:42:59 +03:00
|
|
|
let widgetId = makeWidgetId(extension.id);
|
|
|
|
this.id = `${widgetId}-browser-action`;
|
|
|
|
this.viewId = `PanelUI-webext-${widgetId}-browser-action-view`;
|
|
|
|
this.widget = null;
|
|
|
|
|
|
|
|
this.pendingPopup = null;
|
|
|
|
this.pendingPopupTimeout = null;
|
2017-05-03 18:12:03 +03:00
|
|
|
this.eventQueue = [];
|
2017-03-27 04:42:59 +03:00
|
|
|
|
|
|
|
this.tabManager = extension.tabManager;
|
|
|
|
|
|
|
|
this.defaults = {
|
|
|
|
enabled: true,
|
|
|
|
title: options.default_title || extension.name,
|
|
|
|
badgeText: "",
|
|
|
|
badgeBackgroundColor: null,
|
|
|
|
popup: options.default_popup || "",
|
|
|
|
area: browserAreas[options.default_area || "navbar"],
|
|
|
|
};
|
|
|
|
|
|
|
|
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.");
|
|
|
|
}
|
|
|
|
|
2017-08-07 02:12:14 +03:00
|
|
|
browserActionMap.set(extension, this);
|
|
|
|
|
|
|
|
this.defaults.icon = await StartupCache.get(
|
|
|
|
extension, ["browserAction", "default_icon"],
|
|
|
|
() => IconDetails.normalize({
|
|
|
|
path: options.default_icon,
|
2017-09-06 13:40:45 +03:00
|
|
|
iconType: "browserAction",
|
2017-08-07 02:12:14 +03:00
|
|
|
themeIcons: options.theme_icons,
|
|
|
|
}, extension));
|
|
|
|
|
|
|
|
this.iconData.set(
|
|
|
|
this.defaults.icon,
|
|
|
|
await StartupCache.get(
|
|
|
|
extension, ["browserAction", "default_icon_data"],
|
|
|
|
() => this.getIconData(this.defaults.icon)));
|
|
|
|
|
2017-03-27 04:42:59 +03:00
|
|
|
this.tabContext = new TabContext(tab => Object.create(this.defaults),
|
|
|
|
extension);
|
2015-10-16 01:14:49 +03:00
|
|
|
|
2017-03-27 04:42:59 +03:00
|
|
|
this.build();
|
2016-05-02 17:59:36 +03:00
|
|
|
}
|
|
|
|
|
2017-03-27 04:42:59 +03:00
|
|
|
onShutdown(reason) {
|
|
|
|
browserActionMap.delete(this.extension);
|
2015-10-16 01:14:49 +03:00
|
|
|
|
2017-03-27 04:42:59 +03:00
|
|
|
this.tabContext.shutdown();
|
|
|
|
CustomizableUI.destroyWidget(this.id);
|
|
|
|
|
|
|
|
this.clearPopup();
|
|
|
|
}
|
2015-06-04 01:34:44 +03:00
|
|
|
|
|
|
|
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-23 05:47:16 +03:00
|
|
|
defaultArea: this.defaults.area,
|
2016-01-16 02:14:25 +03:00
|
|
|
|
2017-08-07 02:34:31 +03:00
|
|
|
// Don't attempt to load properties from the built-in widget string
|
|
|
|
// bundle.
|
|
|
|
localized: false,
|
|
|
|
|
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");
|
2017-06-16 16:41:57 +03:00
|
|
|
view.setAttribute("extension", true);
|
2016-01-16 02:14:25 +03:00
|
|
|
|
|
|
|
document.getElementById("PanelUI-multiView").appendChild(view);
|
2017-08-07 00:27:10 +03:00
|
|
|
|
|
|
|
if (this.extension.hasPermission("menus") ||
|
|
|
|
this.extension.hasPermission("contextMenus")) {
|
|
|
|
document.addEventListener("popupshowing", this);
|
|
|
|
}
|
2016-01-16 02:14:25 +03:00
|
|
|
},
|
|
|
|
|
|
|
|
onDestroyed: document => {
|
2017-04-02 21:59:55 +03:00
|
|
|
document.removeEventListener("popupshowing", this);
|
|
|
|
|
2016-01-16 02:14:25 +03:00
|
|
|
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();
|
|
|
|
}
|
|
|
|
},
|
|
|
|
|
|
|
|
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);
|
2017-03-12 06:05:09 +03:00
|
|
|
node.onmouseover = event => this.handleEvent(event);
|
|
|
|
node.onmouseout = event => this.handleEvent(event);
|
2016-08-19 22:29:11 +03:00
|
|
|
|
2017-08-18 22:53:30 +03:00
|
|
|
this.updateButton(node, this.defaults, true);
|
2016-01-16 02:14:25 +03:00
|
|
|
},
|
2015-06-04 01:34:44 +03:00
|
|
|
|
2017-05-03 18:12:03 +03:00
|
|
|
onViewShowing: async event => {
|
|
|
|
TelemetryStopwatch.start(POPUP_OPEN_MS_HISTOGRAM, this);
|
2016-01-16 02:14:25 +03:00
|
|
|
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 {
|
2017-07-24 16:56:01 +03:00
|
|
|
if (event.target.closest("panelmultiview")) {
|
|
|
|
// FIXME: The line below needs to change eventually, but for now:
|
|
|
|
// ensure the view is _always_ visible _before_ `popup.attach()` is
|
|
|
|
// called. PanelMultiView.jsm dictates different behavior.
|
|
|
|
event.target.setAttribute("current", true);
|
|
|
|
}
|
2016-08-19 22:29:11 +03:00
|
|
|
let popup = this.getPopup(document.defaultView, popupURL);
|
2017-05-03 18:12:03 +03:00
|
|
|
let attachPromise = popup.attach(event.target);
|
|
|
|
event.detail.addBlocker(attachPromise);
|
|
|
|
await attachPromise;
|
|
|
|
TelemetryStopwatch.finish(POPUP_OPEN_MS_HISTOGRAM, this);
|
|
|
|
if (this.eventQueue.length) {
|
|
|
|
let histogram = Services.telemetry.getHistogramById(POPUP_RESULT_HISTOGRAM);
|
|
|
|
histogram.add("popupShown");
|
|
|
|
this.eventQueue = [];
|
|
|
|
}
|
2016-01-16 02:14:25 +03:00
|
|
|
} catch (e) {
|
2017-05-03 18:12:03 +03:00
|
|
|
TelemetryStopwatch.cancel(POPUP_OPEN_MS_HISTOGRAM, this);
|
2016-01-16 02:14:25 +03:00
|
|
|
Cu.reportError(e);
|
|
|
|
event.preventDefault();
|
2015-06-04 01:34:44 +03:00
|
|
|
}
|
2016-01-16 02:14:25 +03:00
|
|
|
} else {
|
2017-05-03 18:12:03 +03:00
|
|
|
TelemetryStopwatch.cancel(POPUP_OPEN_MS_HISTOGRAM, this);
|
2016-01-16 02:14:25 +03:00
|
|
|
// This isn't not a hack, but it seems to provide the correct behavior
|
|
|
|
// with the fewest complications.
|
|
|
|
event.preventDefault();
|
2017-06-15 22:48:40 +03:00
|
|
|
this.emit("click", tabbrowser.selectedBrowser);
|
2017-07-03 19:14:58 +03:00
|
|
|
// Ensure we close any popups this node was in:
|
|
|
|
CustomizableUI.hidePanelForNode(event.target);
|
2016-01-16 02:14:25 +03:00
|
|
|
}
|
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;
|
2017-03-27 04:42:59 +03:00
|
|
|
}
|
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.
|
2017-03-27 04:42:59 +03:00
|
|
|
*
|
|
|
|
* @param {Window} window
|
2016-09-07 13:05:11 +03:00
|
|
|
*/
|
2017-03-27 04:42:59 +03:00
|
|
|
async triggerAction(window) {
|
2016-09-07 13:05:11 +03:00
|
|
|
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) {
|
2017-08-01 20:37:41 +03:00
|
|
|
await window.document.getElementById("nav-bar").overflowable.show();
|
2016-09-07 13:05:11 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
let event = new window.CustomEvent("command", {bubbles: true, cancelable: true});
|
|
|
|
widget.node.dispatchEvent(event);
|
|
|
|
} else {
|
2017-08-04 21:18:39 +03:00
|
|
|
this.tabManager.addActiveTabPermission(tab);
|
2016-09-07 13:05:11 +03:00
|
|
|
this.emit("click");
|
|
|
|
}
|
2017-03-27 04:42:59 +03:00
|
|
|
}
|
2016-09-07 13:05:11 +03:00
|
|
|
|
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
|
|
|
|
2017-03-12 06:05:09 +03:00
|
|
|
if (popupURL && enabled && (this.pendingPopup || !ViewPopup.for(this.extension, window))) {
|
2017-05-03 18:12:03 +03:00
|
|
|
this.eventQueue.push("Mousedown");
|
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.
|
2017-03-12 06:05:09 +03:00
|
|
|
if (!this.tabManager.hasActiveTabPermission(tab)) {
|
2016-11-01 00:30:16 +03:00
|
|
|
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
|
|
|
|
2017-03-12 06:05:09 +03:00
|
|
|
case "mouseover": {
|
|
|
|
// 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");
|
|
|
|
let enabled = this.getProperty(tab, "enabled");
|
|
|
|
|
|
|
|
if (popupURL && enabled && (this.pendingPopup || !ViewPopup.for(this.extension, window))) {
|
2017-05-03 18:12:03 +03:00
|
|
|
this.eventQueue.push("Hover");
|
2017-03-12 06:05:09 +03:00
|
|
|
this.pendingPopup = this.getPopup(window, popupURL, true);
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
case "mouseout":
|
|
|
|
if (this.pendingPopup) {
|
2017-05-03 18:12:03 +03:00
|
|
|
if (this.eventQueue.length) {
|
|
|
|
let histogram = Services.telemetry.getHistogramById(POPUP_RESULT_HISTOGRAM);
|
|
|
|
histogram.add(`clearAfter${this.eventQueue.pop()}`);
|
|
|
|
this.eventQueue = [];
|
|
|
|
}
|
2017-03-12 06:05:09 +03:00
|
|
|
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
|
|
|
}
|
2017-03-27 04:42:59 +03:00
|
|
|
}
|
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.
|
2017-03-12 06:05:09 +03:00
|
|
|
* @param {boolean} [blockParser = false]
|
|
|
|
* True if the HTML parser should initially be blocked.
|
2016-08-19 22:29:11 +03:00
|
|
|
* @returns {ViewPopup}
|
|
|
|
*/
|
2017-03-12 06:05:09 +03:00
|
|
|
getPopup(window, popupURL, blockParser = false) {
|
2016-08-19 22:29:11 +03:00
|
|
|
this.clearPopupTimeout();
|
|
|
|
let {pendingPopup} = this;
|
|
|
|
this.pendingPopup = null;
|
|
|
|
|
|
|
|
if (pendingPopup) {
|
|
|
|
if (pendingPopup.window === window && pendingPopup.popupURL === popupURL) {
|
2017-05-02 21:55:03 +03:00
|
|
|
if (!blockParser) {
|
2017-03-12 06:05:09 +03:00
|
|
|
pendingPopup.unblockParser();
|
|
|
|
}
|
|
|
|
|
2016-08-19 22:29:11 +03:00
|
|
|
return pendingPopup;
|
|
|
|
}
|
|
|
|
pendingPopup.destroy();
|
|
|
|
}
|
|
|
|
|
|
|
|
let fixedWidth = this.widget.areaType == CustomizableUI.TYPE_MENU_PANEL;
|
2017-03-12 06:05:09 +03:00
|
|
|
return new ViewPopup(this.extension, window, popupURL, this.browserStyle, fixedWidth, blockParser);
|
2017-03-27 04:42:59 +03:00
|
|
|
}
|
2016-08-19 22:29:11 +03:00
|
|
|
|
|
|
|
/**
|
|
|
|
* 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);
|
|
|
|
}
|
2016-08-19 22:29:11 +03:00
|
|
|
this.pendingPopup.destroy();
|
|
|
|
this.pendingPopup = null;
|
|
|
|
}
|
2017-04-02 21:59:55 +03:00
|
|
|
this.tabToRevokeDuringClearPopup = null;
|
2017-03-27 04:42:59 +03:00
|
|
|
}
|
2016-08-19 22:29:11 +03:00
|
|
|
|
|
|
|
/**
|
|
|
|
* 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;
|
|
|
|
}
|
2017-03-27 04:42:59 +03:00
|
|
|
}
|
2016-08-19 22:29:11 +03:00
|
|
|
|
2015-10-16 01:14:49 +03:00
|
|
|
// Update the toolbar button |node| with the tab context data
|
|
|
|
// in |tabData|.
|
2017-08-18 22:53:30 +03:00
|
|
|
updateButton(node, tabData, sync = false) {
|
2016-01-16 08:43:26 +03:00
|
|
|
let title = tabData.title || this.extension.name;
|
2017-08-18 22:53:30 +03:00
|
|
|
let callback = () => {
|
2017-05-06 00:56:19 +03:00
|
|
|
node.setAttribute("tooltiptext", title);
|
|
|
|
node.setAttribute("label", title);
|
2015-06-04 01:34:44 +03:00
|
|
|
|
2017-05-06 00:56:19 +03:00
|
|
|
if (tabData.badgeText) {
|
|
|
|
node.setAttribute("badge", tabData.badgeText);
|
|
|
|
} else {
|
|
|
|
node.removeAttribute("badge");
|
|
|
|
}
|
2015-10-28 12:27:29 +03:00
|
|
|
|
2017-05-06 00:56:19 +03:00
|
|
|
if (tabData.enabled) {
|
|
|
|
node.removeAttribute("disabled");
|
|
|
|
} else {
|
|
|
|
node.setAttribute("disabled", "true");
|
2015-06-04 01:34:44 +03:00
|
|
|
}
|
|
|
|
|
2017-05-06 00:56:19 +03:00
|
|
|
let badgeNode = node.ownerDocument.getAnonymousElementByAttribute(node,
|
|
|
|
"class", "toolbarbutton-badge");
|
|
|
|
if (badgeNode) {
|
|
|
|
let color = tabData.badgeBackgroundColor;
|
|
|
|
if (color) {
|
|
|
|
color = `rgba(${color[0]}, ${color[1]}, ${color[2]}, ${color[3] / 255})`;
|
|
|
|
}
|
|
|
|
badgeNode.style.backgroundColor = color || "";
|
|
|
|
}
|
2016-06-06 07:05:36 +03:00
|
|
|
|
2017-05-06 00:56:19 +03:00
|
|
|
let {style, legacy} = this.iconData.get(tabData.icon);
|
|
|
|
const LEGACY_CLASS = "toolbarbutton-legacy-addon";
|
|
|
|
if (legacy) {
|
|
|
|
node.classList.add(LEGACY_CLASS);
|
|
|
|
} else {
|
|
|
|
node.classList.remove(LEGACY_CLASS);
|
|
|
|
}
|
|
|
|
|
|
|
|
node.setAttribute("style", style);
|
2017-08-18 22:53:30 +03:00
|
|
|
};
|
|
|
|
if (sync) {
|
|
|
|
callback();
|
|
|
|
} else {
|
|
|
|
node.ownerGlobal.requestAnimationFrame(callback);
|
|
|
|
}
|
2017-05-06 00:21:07 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
getIconData(icons) {
|
2016-07-14 01:16:00 +03:00
|
|
|
let baseSize = 16;
|
2017-05-06 00:21:07 +03:00
|
|
|
let {icon, size} = IconDetails.getPreferredIcon(icons, this.extension, baseSize);
|
|
|
|
|
|
|
|
let legacy = false;
|
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.
|
2017-06-28 01:23:35 +03:00
|
|
|
if (size % 16 && typeof icon === "string" && !icon.endsWith(".svg")) {
|
2017-05-06 00:21:07 +03:00
|
|
|
let result = IconDetails.getPreferredIcon(icons, 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;
|
2017-05-06 00:21:07 +03:00
|
|
|
legacy = true;
|
2016-06-06 07:05:36 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-06-28 01:23:35 +03:00
|
|
|
let getIcon = (size, theme) => {
|
|
|
|
let {icon} = IconDetails.getPreferredIcon(icons, this.extension, size);
|
|
|
|
if (typeof icon === "object") {
|
|
|
|
return IconDetails.escapeUrl(icon[theme]);
|
|
|
|
}
|
|
|
|
return IconDetails.escapeUrl(icon);
|
|
|
|
};
|
|
|
|
|
|
|
|
let getStyle = (name, size) => {
|
|
|
|
return `
|
|
|
|
--webextension-${name}: url("${getIcon(size, "default")}");
|
|
|
|
--webextension-${name}-light: url("${getIcon(size, "light")}");
|
|
|
|
--webextension-${name}-dark: url("${getIcon(size, "dark")}");
|
|
|
|
`;
|
|
|
|
};
|
2016-07-14 01:16:00 +03:00
|
|
|
|
2017-05-06 00:21:07 +03:00
|
|
|
let style = `
|
2017-06-28 01:23:35 +03:00
|
|
|
${getStyle("menupanel-image", 32)}
|
|
|
|
${getStyle("menupanel-image-2x", 64)}
|
|
|
|
${getStyle("toolbar-image", baseSize)}
|
|
|
|
${getStyle("toolbar-image-2x", baseSize * 2)}
|
2017-05-06 00:21:07 +03:00
|
|
|
`;
|
|
|
|
|
|
|
|
return {style, legacy};
|
2017-03-27 04:42:59 +03:00
|
|
|
}
|
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));
|
|
|
|
}
|
2017-03-27 04:42:59 +03:00
|
|
|
}
|
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
|
|
|
}
|
|
|
|
}
|
2017-03-27 04:42:59 +03:00
|
|
|
}
|
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);
|
2017-03-27 04:42:59 +03:00
|
|
|
}
|
2015-06-04 01:34:44 +03:00
|
|
|
|
|
|
|
// 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];
|
2017-01-31 09:44:16 +03:00
|
|
|
}
|
|
|
|
|
2017-04-02 21:59:55 +03:00
|
|
|
getAPI(context) {
|
|
|
|
let {extension} = context;
|
|
|
|
let {tabManager} = extension;
|
2015-10-28 12:27:29 +03:00
|
|
|
|
2017-03-27 04:42:59 +03:00
|
|
|
let browserAction = this;
|
2017-03-25 21:36:56 +03:00
|
|
|
|
2017-04-02 21:59:55 +03:00
|
|
|
function getTab(tabId) {
|
|
|
|
if (tabId !== null) {
|
|
|
|
return tabTracker.getTab(tabId);
|
|
|
|
}
|
|
|
|
return null;
|
|
|
|
}
|
2016-01-16 08:43:26 +03:00
|
|
|
|
2017-04-02 21:59:55 +03:00
|
|
|
return {
|
|
|
|
browserAction: {
|
2017-06-15 22:48:40 +03:00
|
|
|
onClicked: new InputEventManager(context, "browserAction.onClicked", fire => {
|
|
|
|
let listener = (event, browser) => {
|
|
|
|
context.withPendingBrowser(browser, () =>
|
|
|
|
fire.sync(tabManager.convert(tabTracker.activeTab)));
|
2017-04-02 21:59:55 +03:00
|
|
|
};
|
2017-03-25 21:36:56 +03:00
|
|
|
browserAction.on("click", listener);
|
2017-04-02 21:59:55 +03:00
|
|
|
return () => {
|
2017-03-25 21:36:56 +03:00
|
|
|
browserAction.off("click", listener);
|
2017-04-02 21:59:55 +03:00
|
|
|
};
|
|
|
|
}).api(),
|
|
|
|
|
|
|
|
enable: function(tabId) {
|
|
|
|
let tab = getTab(tabId);
|
2017-03-25 21:36:56 +03:00
|
|
|
browserAction.setProperty(tab, "enabled", true);
|
2017-04-02 21:59:55 +03:00
|
|
|
},
|
|
|
|
|
|
|
|
disable: function(tabId) {
|
|
|
|
let tab = getTab(tabId);
|
2017-03-25 21:36:56 +03:00
|
|
|
browserAction.setProperty(tab, "enabled", false);
|
2017-04-02 21:59:55 +03:00
|
|
|
},
|
|
|
|
|
|
|
|
setTitle: function(details) {
|
|
|
|
let tab = getTab(details.tabId);
|
|
|
|
|
|
|
|
let title = details.title;
|
|
|
|
// Clear the tab-specific title when given a null string.
|
|
|
|
if (tab && title == "") {
|
|
|
|
title = null;
|
|
|
|
}
|
2017-03-25 21:36:56 +03:00
|
|
|
browserAction.setProperty(tab, "title", title);
|
2017-04-02 21:59:55 +03:00
|
|
|
},
|
2015-06-04 01:34:44 +03:00
|
|
|
|
2017-04-02 21:59:55 +03:00
|
|
|
getTitle: function(details) {
|
|
|
|
let tab = getTab(details.tabId);
|
2016-02-27 04:56:30 +03:00
|
|
|
|
2017-03-25 21:36:56 +03:00
|
|
|
let title = browserAction.getProperty(tab, "title");
|
2017-04-02 21:59:55 +03:00
|
|
|
return Promise.resolve(title);
|
|
|
|
},
|
2015-06-04 01:34:44 +03:00
|
|
|
|
2017-04-02 21:59:55 +03:00
|
|
|
setIcon: function(details) {
|
|
|
|
let tab = getTab(details.tabId);
|
2016-02-27 04:56:30 +03:00
|
|
|
|
2017-09-06 13:40:45 +03:00
|
|
|
details.iconType = "browserAction";
|
|
|
|
|
2017-04-02 21:59:55 +03:00
|
|
|
let icon = IconDetails.normalize(details, extension, context);
|
2017-03-25 21:36:56 +03:00
|
|
|
browserAction.setProperty(tab, "icon", icon);
|
2017-04-02 21:59:55 +03:00
|
|
|
},
|
2015-06-04 01:34:44 +03:00
|
|
|
|
2017-04-02 21:59:55 +03:00
|
|
|
setBadgeText: function(details) {
|
|
|
|
let tab = getTab(details.tabId);
|
2016-02-27 04:56:30 +03:00
|
|
|
|
2017-03-25 21:36:56 +03:00
|
|
|
browserAction.setProperty(tab, "badgeText", details.text);
|
2017-04-02 21:59:55 +03:00
|
|
|
},
|
2015-06-04 01:34:44 +03:00
|
|
|
|
2017-04-02 21:59:55 +03:00
|
|
|
getBadgeText: function(details) {
|
|
|
|
let tab = getTab(details.tabId);
|
2016-02-27 04:56:30 +03:00
|
|
|
|
2017-03-25 21:36:56 +03:00
|
|
|
let text = browserAction.getProperty(tab, "badgeText");
|
2017-04-02 21:59:55 +03:00
|
|
|
return Promise.resolve(text);
|
|
|
|
},
|
2015-06-04 01:34:44 +03:00
|
|
|
|
2017-04-02 21:59:55 +03:00
|
|
|
setPopup: function(details) {
|
|
|
|
let tab = getTab(details.tabId);
|
2016-02-27 04:56:30 +03:00
|
|
|
|
2017-04-02 21:59:55 +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);
|
2017-09-08 00:09:40 +03:00
|
|
|
if (url && !context.checkLoadURL(url)) {
|
|
|
|
return Promise.reject({message: `Access denied for URL ${url}`});
|
|
|
|
}
|
2017-03-25 21:36:56 +03:00
|
|
|
browserAction.setProperty(tab, "popup", url);
|
2017-04-02 21:59:55 +03:00
|
|
|
},
|
2015-06-04 01:34:44 +03:00
|
|
|
|
2017-04-02 21:59:55 +03:00
|
|
|
getPopup: function(details) {
|
|
|
|
let tab = getTab(details.tabId);
|
2016-02-27 04:56:30 +03:00
|
|
|
|
2017-03-25 21:36:56 +03:00
|
|
|
let popup = browserAction.getProperty(tab, "popup");
|
2017-04-02 21:59:55 +03:00
|
|
|
return Promise.resolve(popup);
|
|
|
|
},
|
2015-06-04 01:34:44 +03:00
|
|
|
|
2017-04-02 21:59:55 +03:00
|
|
|
setBadgeBackgroundColor: function(details) {
|
|
|
|
let tab = getTab(details.tabId);
|
|
|
|
let color = details.color;
|
|
|
|
if (!Array.isArray(color)) {
|
|
|
|
let col = DOMUtils.colorToRGBA(color);
|
|
|
|
color = col && [col.r, col.g, col.b, Math.round(col.a * 255)];
|
|
|
|
}
|
2017-03-25 21:36:56 +03:00
|
|
|
browserAction.setProperty(tab, "badgeBackgroundColor", color);
|
2017-04-02 21:59:55 +03:00
|
|
|
},
|
2015-06-04 01:34:44 +03:00
|
|
|
|
2017-04-02 21:59:55 +03:00
|
|
|
getBadgeBackgroundColor: function(details, callback) {
|
|
|
|
let tab = getTab(details.tabId);
|
2016-02-27 04:56:30 +03:00
|
|
|
|
2017-03-25 21:36:56 +03:00
|
|
|
let color = browserAction.getProperty(tab, "badgeBackgroundColor");
|
2017-04-02 21:59:55 +03:00
|
|
|
return Promise.resolve(color || [0xd9, 0, 0, 255]);
|
|
|
|
},
|
2017-03-09 10:36:25 +03:00
|
|
|
|
|
|
|
openPopup: function() {
|
|
|
|
let window = windowTracker.topWindow;
|
|
|
|
browserAction.triggerAction(window);
|
|
|
|
},
|
2015-06-04 01:34:44 +03:00
|
|
|
},
|
2017-04-02 21:59:55 +03:00
|
|
|
};
|
|
|
|
}
|
|
|
|
};
|
2017-03-27 04:42:59 +03:00
|
|
|
|
|
|
|
global.browserActionFor = this.browserAction.for;
|