-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathreading-line-bg.js
82 lines (71 loc) · 2.26 KB
/
reading-line-bg.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
chrome.storage.local.get(['ReadingLine'], function(result) {
if (result.ReadingLine === null) {
chrome.storage.local.set({ReadingLine: true});
result.ReadingLine = true;
}
toggleIcon(result.ReadingLine);
chrome.tabs.query({windowType: 'normal'}, function (tabs) {
for (var i in tabs) {
try {
chrome.scripting.insertCSS({
target: {tabId: tabs[i].id},
css: "reading-line.css",
});
chrome.scripting.executeScript({
target: {tabId: tabs[i].id},
files: ['reading-line.js'],
});
chrome.tabs.sendMessage(tabs[i].id, {
action: result.ReadingLine ? "ReadingLine-enable" : "ReadingLine-disable"
});
}
catch (e) {}
}
});
});
function toggleIcon(isEnabled) {
if (isEnabled) {
chrome.action.setIcon({
path: "icons/icon19-active.png",
});
}
else {
chrome.action.setIcon({
path: "icons/icon19.png",
});
}
}
function toggleExtensionStatus(isEnabled) {
toggleIcon(isEnabled);
chrome.storage.local.set({ ReadingLine: isEnabled }, function(){
chrome.tabs.query({windowType: 'normal'}, function (tabs) {
for (var i in tabs) {
try {
chrome.tabs.sendMessage(tabs[i].id, {
action: isEnabled ? "ReadingLine-enable" : "ReadingLine-disable"
});
}
catch (e) {}
}
});
});
}
chrome.action.onClicked.addListener(function (tab) {
chrome.storage.local.get(['ReadingLine'], function(result) {
toggleExtensionStatus(!result.ReadingLine);
});
});
chrome.runtime.onMessage.addListener(function (message, sender, sendResponse) {
if (message === "ReadingLine-status") {
chrome.storage.local.get(['ReadingLine'], function(result) {
sendResponse(result.ReadingLine);
});
return true;
}
if (message === "ReadingLine-enable") {
toggleExtensionStatus(true);
}
if (message === "ReadingLine-disable") {
toggleExtensionStatus(false);
}
});