1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
|
/* -*- Mode: indent-tabs-mode: nil; js-indent-level: 2 -*- */
/* vim: set sts=2 sw=2 et tw=80: */
/* This Source Code Form is subject to the terms of the Mozilla Public
* License, v. 2.0. If a copy of the MPL was not distributed with this file,
* You can obtain one at http://mozilla.org/MPL/2.0/. */
"use strict";
ChromeUtils.defineESModuleGetters(this, {
BrowserUsageTelemetry: "resource:///modules/BrowserUsageTelemetry.sys.mjs",
ExtensionTelemetry: "resource://gre/modules/ExtensionTelemetry.sys.mjs",
PageActions: "resource:///modules/PageActions.sys.mjs",
PanelPopup: "resource:///modules/ExtensionPopups.sys.mjs",
});
var { DefaultWeakMap } = ExtensionUtils;
var { ExtensionParent } = ChromeUtils.importESModule(
"resource://gre/modules/ExtensionParent.sys.mjs"
);
var { PageActionBase } = ChromeUtils.importESModule(
"resource://gre/modules/ExtensionActions.sys.mjs"
);
// WeakMap[Extension -> PageAction]
let pageActionMap = new WeakMap();
class PageAction extends PageActionBase {
constructor(extension, buttonDelegate) {
let tabContext = new TabContext(tab => this.getContextData(null));
super(tabContext, extension);
this.buttonDelegate = buttonDelegate;
}
updateOnChange(target) {
this.buttonDelegate.updateButton(target.ownerGlobal);
}
dispatchClick(tab, clickInfo) {
this.buttonDelegate.emit("click", tab, clickInfo);
}
getTab(tabId) {
if (tabId !== null) {
return tabTracker.getTab(tabId);
}
return null;
}
}
this.pageAction = class extends ExtensionAPIPersistent {
static for(extension) {
return pageActionMap.get(extension);
}
static onUpdate(id, manifest) {
if (!("page_action" in manifest)) {
// If the new version has no page action then mark this widget as hidden
// in the telemetry. If it is already marked hidden then this will do
// nothing.
BrowserUsageTelemetry.recordWidgetChange(makeWidgetId(id), null, "addon");
}
}
static onDisable(id) {
BrowserUsageTelemetry.recordWidgetChange(makeWidgetId(id), null, "addon");
}
static onUninstall(id) {
// If the telemetry already has this widget as hidden then this will not
// record anything.
BrowserUsageTelemetry.recordWidgetChange(makeWidgetId(id), null, "addon");
}
async onManifestEntry(entryName) {
let { extension } = this;
let options = extension.manifest.page_action;
this.action = new PageAction(extension, this);
await this.action.loadIconData();
let widgetId = makeWidgetId(extension.id);
this.id = widgetId + "-page-action";
this.tabManager = extension.tabManager;
this.browserStyle = options.browser_style;
pageActionMap.set(extension, this);
this.lastValues = new DefaultWeakMap(() => ({}));
if (!this.browserPageAction) {
let onPlacedHandler = (buttonNode, isPanel) => {
// eslint-disable-next-line mozilla/balanced-listeners
buttonNode.addEventListener("auxclick", event => {
if (event.button !== 1 || event.target.disabled) {
return;
}
// The panel is not automatically closed when middle-clicked.
if (isPanel) {
buttonNode.closest("#pageActionPanel").hidePopup();
}
let window = event.target.ownerGlobal;
let tab = window.gBrowser.selectedTab;
this.tabManager.addActiveTabPermission(tab);
this.action.dispatchClick(tab, {
button: event.button,
modifiers: clickModifiersFromEvent(event),
});
});
};
this.browserPageAction = PageActions.addAction(
new PageActions.Action({
id: widgetId,
extensionID: extension.id,
title: this.action.getProperty(null, "title"),
iconURL: this.action.getProperty(null, "icon"),
pinnedToUrlbar: this.action.getPinned(),
disabled: !this.action.getProperty(null, "enabled"),
onCommand: (event, buttonNode) => {
this.handleClick(event.target.ownerGlobal, {
button: event.button || 0,
modifiers: clickModifiersFromEvent(event),
});
},
onBeforePlacedInWindow: browserWindow => {
if (
this.extension.hasPermission("menus") ||
this.extension.hasPermission("contextMenus")
) {
browserWindow.document.addEventListener("popupshowing", this);
}
},
onPlacedInPanel: buttonNode => onPlacedHandler(buttonNode, true),
onPlacedInUrlbar: buttonNode => onPlacedHandler(buttonNode, false),
onRemovedFromWindow: browserWindow => {
browserWindow.document.removeEventListener("popupshowing", this);
},
})
);
if (this.extension.startupReason != "APP_STARTUP") {
// Make sure the browser telemetry has the correct state for this widget.
// Defer loading BrowserUsageTelemetry until after startup is complete.
ExtensionParent.browserStartupPromise.then(() => {
BrowserUsageTelemetry.recordWidgetChange(
widgetId,
this.browserPageAction.pinnedToUrlbar
? "page-action-buttons"
: null,
"addon"
);
});
}
// If the page action is only enabled in some URLs, do pattern matching in
// the active tabs and update the button if necessary.
if (this.action.getProperty(null, "enabled") === undefined) {
for (let window of windowTracker.browserWindows()) {
let tab = window.gBrowser.selectedTab;
if (this.action.isShownForTab(tab)) {
this.updateButton(window);
}
}
}
}
}
onShutdown(isAppShutdown) {
pageActionMap.delete(this.extension);
this.action.onShutdown();
// Removing the browser page action causes PageActions to forget about it
// across app restarts, so don't remove it on app shutdown, but do remove
// it on all other shutdowns since there's no guarantee the action will be
// coming back.
if (!isAppShutdown && this.browserPageAction) {
this.browserPageAction.remove();
this.browserPageAction = null;
}
}
// Updates the page action button in the given window to reflect the
// properties of the currently selected tab:
//
// Updates "tooltiptext" and "aria-label" to match "title" property.
// Updates "image" to match the "icon" property.
// Enables or disables the icon, based on the "enabled" and "patternMatching" properties.
updateButton(window) {
let tab = window.gBrowser.selectedTab;
let tabData = this.action.getContextData(tab);
let last = this.lastValues.get(window);
window.requestAnimationFrame(() => {
// If we get called just before shutdown, we might have been destroyed by
// this point.
if (!this.browserPageAction) {
return;
}
let title = tabData.title || this.extension.name;
if (last.title !== title) {
this.browserPageAction.setTitle(title, window);
last.title = title;
}
let enabled =
tabData.enabled != null ? tabData.enabled : tabData.patternMatching;
if (last.enabled !== enabled) {
this.browserPageAction.setDisabled(!enabled, window);
last.enabled = enabled;
}
let icon = tabData.icon;
if (last.icon !== icon) {
this.browserPageAction.setIconURL(icon, window);
last.icon = icon;
}
});
}
/**
* Triggers this page action for the given window, with the same effects as
* if it were clicked by a user.
*
* This has no effect if the page action is hidden for the selected tab.
*
* @param {Window} window
*/
triggerAction(window) {
this.handleClick(window, { button: 0, modifiers: [] });
}
handleEvent(event) {
switch (event.type) {
case "popupshowing":
const menu = event.target;
const trigger = menu.triggerNode;
const getActionId = () => {
let actionId = trigger.getAttribute("actionid");
if (actionId) {
return actionId;
}
// When a page action is clicked, triggerNode will be an ancestor of
// a node corresponding to an action. triggerNode will be the page
// action node itself when a page action is selected with the
// keyboard. That's because the semantic meaning of page action is on
// an hbox that contains an <image>.
for (let n = trigger; n && !actionId; n = n.parentElement) {
if (n.id == "page-action-buttons" || n.localName == "panelview") {
// We reached the page-action-buttons or panelview container.
// Stop looking; no action was found.
break;
}
actionId = n.getAttribute("actionid");
}
return actionId;
};
if (
menu.id === "pageActionContextMenu" &&
trigger &&
getActionId() === this.browserPageAction.id &&
!this.browserPageAction.getDisabled(trigger.ownerGlobal)
) {
global.actionContextMenu({
extension: this.extension,
onPageAction: true,
menu: menu,
});
}
break;
}
}
// Handles a click event on the page action button for the given
// window.
// If the page action has a |popup| property, a panel is opened to
// that URL. Otherwise, a "click" event is emitted, and dispatched to
// the any click listeners in the add-on.
async handleClick(window, clickInfo) {
const { extension } = this;
ExtensionTelemetry.pageActionPopupOpen.stopwatchStart(extension, this);
let tab = window.gBrowser.selectedTab;
let popupURL = this.action.triggerClickOrPopup(tab, clickInfo);
// If the widget has a popup URL defined, we open a popup, but do not
// dispatch a click event to the extension.
// If it has no popup URL defined, we dispatch a click event, but do not
// open a popup.
if (popupURL) {
if (this.popupNode && this.popupNode.panel.state !== "closed") {
// The panel is being toggled closed.
ExtensionTelemetry.pageActionPopupOpen.stopwatchCancel(extension, this);
window.BrowserPageActions.togglePanelForAction(
this.browserPageAction,
this.popupNode.panel
);
return;
}
this.popupNode = new PanelPopup(
extension,
window.document,
popupURL,
this.browserStyle
);
// Remove popupNode when it is closed.
this.popupNode.panel.addEventListener(
"popuphiding",
() => {
this.popupNode = undefined;
},
{ once: true }
);
await this.popupNode.contentReady;
window.BrowserPageActions.togglePanelForAction(
this.browserPageAction,
this.popupNode.panel
);
ExtensionTelemetry.pageActionPopupOpen.stopwatchFinish(extension, this);
} else {
ExtensionTelemetry.pageActionPopupOpen.stopwatchCancel(extension, this);
}
}
PERSISTENT_EVENTS = {
onClicked({ context, fire }) {
const { extension } = this;
const { tabManager } = extension;
let listener = async (_event, tab, clickInfo) => {
if (fire.wakeup) {
await fire.wakeup();
}
// TODO: we should double-check if the tab is already being closed by the time
// the background script got started and we converted the primed listener.
context?.withPendingBrowser(tab.linkedBrowser, () =>
fire.sync(tabManager.convert(tab), clickInfo)
);
};
this.on("click", listener);
return {
unregister: () => {
this.off("click", listener);
},
convert(newFire, extContext) {
fire = newFire;
context = extContext;
},
};
},
};
getAPI(context) {
const { action } = this;
return {
pageAction: {
...action.api(context),
onClicked: new EventManager({
context,
module: "pageAction",
event: "onClicked",
inputHandling: true,
extensionApi: this,
}).api(),
openPopup: () => {
let window = windowTracker.topWindow;
this.triggerAction(window);
},
},
};
}
};
global.pageActionFor = this.pageAction.for;
|