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
|
/* -*- indent-tabs-mode: nil; js-indent-level: 2 -*-
* 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/. */
// This file is loaded into the browser window scope.
/* eslint-env mozilla/browser-window */
ChromeUtils.defineESModuleGetters(this, {
OriginControls: "resource://gre/modules/ExtensionPermissions.sys.mjs",
});
/**
* The `unified-extensions-item` custom element is used to manage an extension
* in the list of extensions, which is displayed when users click the unified
* extensions (toolbar) button.
*
* This custom element must be initialized with `setExtension()`:
*
* ```
* let item = document.createElement("unified-extensions-item");
* item.setExtension(extension);
* document.body.appendChild(item);
* ```
*/
customElements.define(
"unified-extensions-item",
class extends HTMLElement {
/**
* Set the extension for this item. The item will be populated based on the
* extension when it is rendered into the DOM.
*
* @param {Extension} extension The extension to use.
*/
setExtension(extension) {
this.extension = extension;
}
connectedCallback() {
if (this._menuButton) {
return;
}
const template = document.getElementById(
"unified-extensions-item-template"
);
this.appendChild(template.content.cloneNode(true));
this._actionButton = this.querySelector(
".unified-extensions-item-action-button"
);
this._menuButton = this.querySelector(
".unified-extensions-item-menu-button"
);
this._messageDeck = this.querySelector(
".unified-extensions-item-message-deck"
);
// Focus/blur events are fired on specific elements only.
this._actionButton.addEventListener("blur", this);
this._actionButton.addEventListener("focus", this);
this._menuButton.addEventListener("blur", this);
this._menuButton.addEventListener("focus", this);
this.addEventListener("command", this);
this.addEventListener("mouseout", this);
this.addEventListener("mouseover", this);
this.render();
}
handleEvent(event) {
const { target } = event;
switch (event.type) {
case "command":
if (target === this._menuButton) {
const popup = target.ownerDocument.getElementById(
"unified-extensions-context-menu"
);
// Anchor to the visible part of the button.
const anchor = target.firstElementChild;
popup.openPopup(
anchor,
"after_end",
0,
0,
true /* isContextMenu */,
false /* attributesOverride */,
event
);
} else if (target === this._actionButton) {
const win = event.target.ownerGlobal;
const tab = win.gBrowser.selectedTab;
this.extension.tabManager.addActiveTabPermission(tab);
this.extension.tabManager.activateScripts(tab);
}
break;
case "blur":
case "mouseout":
this._messageDeck.selectedIndex =
gUnifiedExtensions.MESSAGE_DECK_INDEX_DEFAULT;
break;
case "focus":
case "mouseover":
if (target === this._menuButton) {
this._messageDeck.selectedIndex =
gUnifiedExtensions.MESSAGE_DECK_INDEX_MENU_HOVER;
} else if (target === this._actionButton) {
this._messageDeck.selectedIndex =
gUnifiedExtensions.MESSAGE_DECK_INDEX_HOVER;
}
break;
}
}
#setStateMessage() {
const messages = OriginControls.getStateMessageIDs({
policy: this.extension.policy,
tab: this.ownerGlobal.gBrowser.selectedTab,
});
if (!messages) {
return;
}
const messageDefaultElement = this.querySelector(
".unified-extensions-item-message-default"
);
this.ownerDocument.l10n.setAttributes(
messageDefaultElement,
messages.default
);
const messageHoverElement = this.querySelector(
".unified-extensions-item-message-hover"
);
this.ownerDocument.l10n.setAttributes(
messageHoverElement,
messages.onHover || messages.default
);
}
#hasAction() {
const state = OriginControls.getState(
this.extension.policy,
this.ownerGlobal.gBrowser.selectedTab
);
return state && state.whenClicked && !state.hasAccess;
}
render() {
if (!this.extension) {
throw new Error(
"unified-extensions-item requires an extension, forgot to call setExtension()?"
);
}
this.setAttribute("extension-id", this.extension.id);
this.classList.add(
"toolbaritem-combined-buttons",
"unified-extensions-item"
);
// The data-extensionid attribute is used by context menu handlers
// to identify the extension being manipulated by the context menu.
this._actionButton.dataset.extensionid = this.extension.id;
this.toggleAttribute(
"attention",
OriginControls.getAttention(this.extension.policy, this.ownerGlobal)
);
this.querySelector(".unified-extensions-item-name").textContent =
this.extension.name;
AddonManager.getAddonByID(this.extension.id).then(addon => {
const iconURL = AddonManager.getPreferredIconURL(addon, 32, window);
if (iconURL) {
this.querySelector(".unified-extensions-item-icon").setAttribute(
"src",
iconURL
);
}
});
this._actionButton.disabled = !this.#hasAction();
// The data-extensionid attribute is used by context menu handlers
// to identify the extension being manipulated by the context menu.
this._menuButton.dataset.extensionid = this.extension.id;
this._menuButton.setAttribute(
"data-l10n-args",
JSON.stringify({ extensionName: this.extension.name })
);
this.#setStateMessage();
}
}
);
|