-
Notifications
You must be signed in to change notification settings - Fork 98
/
Copy pathbackground.js
105 lines (93 loc) · 2.59 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
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
import {
executeSaveScroll,
executeGetScroll,
executeDeleteScroll
} from "./helpers.js";
function getUrlWithoutHash(url) {
return url.split("?")[0];
}
chrome.runtime.onInstalled.addListener(() => {
chrome.storage.local.set({ "scroll-mark": {} });
});
const updateIcon = () => {
chrome.tabs.query({ active: true, currentWindow: true }, tabs => {
const url = getUrlWithoutHash(tabs[0].url);
chrome.storage.local.get("scroll-mark", data => {
const scrollMarkData = data["scroll-mark"];
if (!scrollMarkData.hasOwnProperty(url)) {
setInactiveIcon();
} else {
setActiveIcon();
}
});
});
};
chrome.tabs.onActivated.addListener(() => {
chrome.tabs.query({ active: true, currentWindow: true }, tabs => {
const url = getUrlWithoutHash(tabs[0].url);
chrome.storage.local.get("scroll-mark", data => {
const scrollMarkData = data["scroll-mark"];
if (!scrollMarkData.hasOwnProperty(url)) {
setInactiveIcon();
} else {
setActiveIcon();
}
});
});
});
chrome.tabs.onUpdated.addListener((tabId, updateObj) => {
chrome.tabs.get(tabId, tab => {
const url = getUrlWithoutHash(tab.url);
if (url) {
chrome.storage.local.get("scroll-mark", data => {
const scrollMarkData = data["scroll-mark"];
if (!scrollMarkData.hasOwnProperty(url)) {
setInactiveIcon();
} else {
setActiveIcon();
}
});
}
});
});
chrome.runtime.onMessage.addListener((request, sender) => {
if (request === "setActive") {
setActiveIcon();
} else {
setInactiveIcon();
}
});
chrome.commands.onCommand.addListener(function(command) {
chrome.tabs.query({ active: true, currentWindow: true }, tabs => {
const currentTabId = tabs[0].id;
if (command === "save-or-update-scroll") {
executeSaveScroll(currentTabId);
} else if (command === "delete-scroll") {
executeDeleteScroll(currentTabId);
} else if (command === "fetch-scroll") {
executeGetScroll(currentTabId);
}
});
});
const setActiveIcon = () => {
chrome.browserAction.setIcon({
path: {
"16": "images/icon-16.png",
"32": "images/icon-32.png",
"48": "images/icon-48.png",
"128": "images/icon-128.png",
"256": "images/icon-256.png"
}
});
};
const setInactiveIcon = () => {
chrome.browserAction.setIcon({
path: {
"16": "images/icon-16-inactive.png",
"32": "images/icon-32-inactive.png",
"48": "images/icon-48-inactive.png",
"128": "images/icon-128-inactive.png",
"256": "images/icon-256-inactive.png"
}
});
};