-
Notifications
You must be signed in to change notification settings - Fork 99
/
background.js
128 lines (113 loc) · 3.57 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
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
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();
// }
// });
// });
// };
// This updates the popup icon based on whether
// the tab has a saved scroll or not when the user
// is switching between tabs
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();
}
});
});
});
// This runs when a page on a tab is loaded and
// if it has a saved scroll then fetch the latest
// scroll
chrome.tabs.onUpdated.addListener((tabId, changeInfo) => {
chrome.tabs.get(tabId, tab => {
const url = getUrlWithoutHash(tab.url);
if (url && changeInfo.status === 'complete') {
chrome.storage.local.get('scroll-mark', data => {
const scrollMarkData = data['scroll-mark'];
chrome.scripting.insertCSS({
target: { tabId },
files: ['contentScripts/index.css'],
});
if (scrollMarkData && scrollMarkData[url] !== undefined) {
executeGetScroll(tabId, null, true);
setActiveIcon();
} else {
setInactiveIcon();
}
});
}
});
});
// This listens for messages from content scripts
// and updates popup icon accordingly
chrome.runtime.onMessage.addListener(request => {
if (request === 'setActive') {
setActiveIcon();
} else {
setInactiveIcon();
}
});
// This listends for keyboard shortcuts and
// performs actions accordingly
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);
}
});
});
}
});
});