-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathbackground.js
More file actions
49 lines (45 loc) · 1.13 KB
/
background.js
File metadata and controls
49 lines (45 loc) · 1.13 KB
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
/*
Open a new tab, and load "my-page.html" into it.
*/
function openMyPage() {
browser.tabs.create({
"url": "/my-zone.html"
});
}
/*
Add openMyPage() as a listener to clicks on the browser action.
*/
browser.browserAction.onClicked.addListener(openMyPage);
// Add a context menu action on every image element in the page.
browser.contextMenus.create({
id: 'collect-data',
title: 'Add to your data collection',
contexts: ['image', 'selection']
});
browser.contextMenus.onClicked.addListener(async (info) => {
let data, type;
if (info.srcUrl) {
data = info.srcUrl;
type = 'new-image';
}
else if (info.selectionText) {
data = info.selectionText;
type = 'new-text';
}
try {
await browser.runtime.sendMessage({
type: type,
data: data
});
chrome.tabs.query({active: true, currentWindow: true}, function(tabs) {
chrome.tabs.sendMessage(tabs[0].id, 'remove', function(response) {
console.log('byeee');
});
});
}
catch (error) {
if (error.message.includes("Could not establish connection. Receiving end does not exist.")) {
openMyPage()
}
}
});