-
Notifications
You must be signed in to change notification settings - Fork 98
/
Copy pathbackground.js
112 lines (99 loc) · 3.01 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
106
107
108
109
110
111
112
import { executeSaveScroll, executeGetScroll } from './contentScripts';
function getUrlWithoutHash(url) {
return url.split('?')[0];
}
const setActiveIcon = () => {
chrome.action.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.action.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',
},
});
};
chrome.runtime.setUninstallURL('https://prateeksurana3255.typeform.com/to/VMfEV6');
chrome.runtime.onInstalled.addListener(details => {
if (details.reason === 'install') {
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 && scrollMarkData[url] !== undefined) {
setActiveIcon();
} else {
setInactiveIcon();
}
});
});
});
chrome.tabs.onUpdated.addListener(tabId => {
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 && scrollMarkData[url] !== undefined) {
executeGetScroll(tabId, null, true);
setActiveIcon();
} else {
setInactiveIcon();
}
});
}
});
});
chrome.runtime.onMessage.addListener(request => {
if (request === 'setActive') {
setActiveIcon();
} else {
setInactiveIcon();
}
});
chrome.commands.onCommand.addListener(command => {
chrome.tabs.query({ active: true, currentWindow: true }, tabs => {
const currentTabId = tabs[0].id;
if (command === 'save-scroll') {
executeSaveScroll(currentTabId);
} else if (command === 'fetch-scroll') {
chrome.tabs.get(currentTabId, tab => {
const url = getUrlWithoutHash(tab.url);
chrome.storage.local.get('scroll-mark', data => {
const scrollMarkData = data['scroll-mark'];
if (scrollMarkData && scrollMarkData[url] !== undefined) {
executeGetScroll(currentTabId, null, true);
}
});
});
}
});
});