forked from dzc34/headingsMap
-
Notifications
You must be signed in to change notification settings - Fork 0
/
background.js
55 lines (40 loc) · 1.41 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
var tabId;
// open/close when clicking the toolbar button
chrome.browserAction.onClicked.addListener(injectHeadingsMapScript);
// listen for messages
chrome.runtime.onConnect.addListener(connected);
function injectHeadingsMapScript(tab) {
tabId = tab.id;
chrome.tabs
.executeScript(tabId, {file: 'content_scripts/headingsMap.js'}, showHeadingsMap);
chrome.tabs
.insertCSS({file: 'content_scripts/headingsMap.css'});
}
function connected(portFromCS) {
portFromCS.onMessage.addListener(function (message) {
if(message.action === 'update'){
updateHeadingsMap();
} else if (message.action === 'settings') {
var openOptionsPage = chrome.runtime.openOptionsPage();
openOptionsPage.then(reportSuccess, reportError);
}
});
}
function sendActionToHeadingsMapScript(action){
var message = {action: action};
chrome.storage
.local.get(['showHeadLevels', 'showHeadError', 'showHeadErrorH1', 'showOutLevels', 'showOutElem', 'showOutError'], sendActionWithSettings);
function sendActionWithSettings(settings) {
message.settings = settings;
chrome.tabs
.sendMessage(tabId, message);
}
}
function showHeadingsMap() {
sendActionToHeadingsMapScript('toggle');
}
function updateHeadingsMap() {
sendActionToHeadingsMapScript('update');
}
function reportSuccess() {}
function reportError(error) {}