-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathbackground.js
76 lines (63 loc) · 1.83 KB
/
background.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
// import {THEMES, add_contextual_menu} from 'lib';
var script_to_call = {file:'anchors-reveal.js'};
var menu_id = 'anchors-reveal';
var THEMES = {
'ClassicalYellow' : {
background : 'yellow',
color : 'black'
},
'LightBlue' : {
background : '#aaf',
color : 'black'
},
'WhitePaper' : {
background : 'white',
color : 'black'
},
'GothicAddict' : {
background : 'black',
color : 'white'
},
};
function add_contextual_menu() {
browser.contextMenus.create({
id: menu_id,
title: browser.i18n.getMessage('buttonDescription'),
icons: {
'16': "data/icon-16.png",
'32': "data/icon-32.png"
}
});
browser.contextMenus.onClicked.addListener(function(info, tab) {
if (info.menuItemId === menu_id) {
browser.tabs.executeScript(script_to_call);
}
});
};
// end import
function instantiate_contextual_menu(result) {
if (result.menu) {
add_contextual_menu();
} else {
try {
browser.contextMenus.remove(menu_id);
} catch (e) {
}
}
}
//The following was modified from http://stackoverflow.com/a/40517692/3773011 Copied by the author of the post.
function handleExecuteScriptAndInsertCSSErrors(tabId){
if (chrome.runtime.lastError){
let isFirefox = !!window.InstallTrigger;
let message = chrome.runtime.lastError.message ? chrome.runtime.lastError : chrome.runtime.lastError.message;
window.console.error(message);
}
}
function oops(e) {
window.console.error(e);
}
let getting = browser.storage.local.get('menu');
getting.then(instantiate_contextual_menu, oops);
chrome.browserAction.onClicked.addListener(function(tab){
chrome.tabs.executeScript(tab.id, script_to_call, handleExecuteScriptAndInsertCSSErrors);
});