-
Notifications
You must be signed in to change notification settings - Fork 1
/
contextMenu.js
34 lines (32 loc) · 1.12 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
function createContextMenu() {
chrome.contextMenus.create({
"title": 'Speak "%s"',
"contexts": ["selection"],
"id": "readPage"
});
};
chrome.contextMenus.onClicked.addListener(function (context) {
chrome.tabs.executeScript({ code: "window.getSelection().toString();" }, function (selection) {
if (typeof selection === 'undefined' || selection === undefined || selection === null) {
if ((/[^\s]+/g).test(context.selectionText)) {
play(context.selectionText);
}
sendAlert()
} else if ((/[^\s]+/g).test(selection.toString())) {
play(selection.toString());
} else if ((/[^\s]+/g).test(context.selectionText)) {
play(context.selectionText);
}
});
});
function play(text) {
if ((/[^\s]+/g).test(text)) {
format(text);
chrome.storage.local.set({ "text": text, timeout: false });
} else {
chrome.storage.local.get(["text"], function (result) {
format(result.text);
});
}
preventBug("play");
}