-
Notifications
You must be signed in to change notification settings - Fork 127
/
Copy pathContextMenu.js
163 lines (142 loc) · 4.48 KB
/
ContextMenu.js
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
import { COMMUNICATION_MESSAGE_TYPE } from "/common/modules/data/BrowserCommunicationTypes.js";
import { createMenu } from "/common/modules/ContextMenu.js";
const CONVERT_TEXT_SELECTION = "qr-convert-text-selection";
const CONVERT_LINK_TEXT_SELECTION = "qr-convert-link-text-selection";
const OPEN_OPTIONS = "qr-open-options";
const CONVERT_PAGE_URL = "qr-convert-page-url";
const MESSAGE_RESENT_TIMEOUT = 200; // ms
const MESSAGE_RESENT_MAX = 9;
let messageResentCount = 0;
/**
* Send new text for the QR code.
*
* @private
* @param {string} qrText
* @returns {void}
*/
function sendQrCodeText(qrText) {
console.info("send QR code text from background");
browser.runtime.sendMessage({
type: COMMUNICATION_MESSAGE_TYPE.SET_QR_TEXT,
qrText: qrText
}).then(() => {
console.info(`QR code text "${qrText}" sent to tab successfully`);
}).catch((e) => {
// stop retrying after some time and just throw out error
if (messageResentCount >= MESSAGE_RESENT_MAX) {
throw e;
}
messageResentCount++;
// recusively re-try message sending
// This is e.g. needed when the popup has not yet opened and could not get the message.
setTimeout(sendQrCodeText, MESSAGE_RESENT_TIMEOUT, qrText);
});
}
/**
* Creates the items in the context menu.
*
* @private
* @returns {Promise}
*/
function createItems() {
const selectionMenu = createMenu("contextMenuItemConvertSelection", {
id: CONVERT_TEXT_SELECTION,
contexts: ["selection"]
});
const linkMenu = createMenu("contextMenuItemConvertLinkSelection", {
id: CONVERT_LINK_TEXT_SELECTION,
contexts: ["link"]
});
const pageMenu = createMenu("contextMenuItemConvertPageURL", {
id: CONVERT_PAGE_URL,
contexts: ["page"]
});
browser.menus.refresh();
// if listener is set, because items were hidden -> remove it
browser.menus.onHidden.removeListener(createItems);
return Promise.all([selectionMenu, linkMenu, pageMenu]);
}
/**
* Triggers when a context menu item has been clicked.
*
* @private
* @param {event} event
* @returns {void}
*/
function menuClicked(event) {
switch (event.menuItemId) {
case CONVERT_TEXT_SELECTION:
browser.browserAction.openPopup().then(() => {
messageResentCount = 0;
// send message to popup
sendQrCodeText(event.selectionText);
});
break;
case CONVERT_LINK_TEXT_SELECTION:
browser.browserAction.openPopup().then(() => {
messageResentCount = 0;
// send message to popup
sendQrCodeText(event.linkUrl);
});
break;
case CONVERT_PAGE_URL:
browser.browserAction.openPopup().then(() => {
messageResentCount = 0;
// Send the current page URL to the popup explicitly to overwrite any setting that may use a different string
sendQrCodeText(event.pageUrl);
});
break;
case OPEN_OPTIONS:
browser.runtime.openOptionsPage();
break;
}
}
/**
* Triggers when the menu is shown.
*
*
*
* @name ContextMenu.menuShown
* @function
* @private
* @param {event} info
* @returns {void}
*/
function menuShown(info) {
if (info.viewType !== "popup" || !info.pageUrl.startsWith(browser.runtime.getURL("."))) {
return;
}
// if no of our own menus are shown, we do not need to do anything
if (info.menuIds.length === 0) {
return;
}
browser.menus.onHidden.addListener(createItems);
browser.menus.remove(CONVERT_TEXT_SELECTION);
browser.menus.remove(CONVERT_LINK_TEXT_SELECTION);
browser.menus.remove(CONVERT_PAGE_URL);
browser.menus.refresh();
}
/**
* Init context menu module.
*
* Adds menu elements.
*
* @private
* @returns {Promise}
*/
export function init() {
return createItems().then(() => {
browser.menus.onClicked.addListener(menuClicked);
browser.menus.onShown.addListener(menuShown);
browser.runtime.onMessage.addListener((message) => {
if (message.action === "enableContextMenu") {
browser.menus.update(CONVERT_PAGE_URL, { visible: true });
console.log("Context menu enabled");
} else if (message.action === "disableContextMenu") {
browser.menus.update(CONVERT_PAGE_URL, { visible: false });
console.log("Context menu disabled");
}
browser.menus.refresh();
});
});
}