-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathpopup.js
162 lines (140 loc) · 5.78 KB
/
popup.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
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
let countBtn = document.getElementById(`countButton`);
let scrollBtn = document.getElementById(`scrollButton`);
let markCheckedBtn = document.getElementById(`markCheckedButton`);
let clearLogBtn = document.getElementById(`clearLogButton`);
let logOutput = document.getElementById(`logOutputTextArea`);
const logPort = chrome.runtime.connect({ name: 'log' });
window.addEventListener(`load`, function () {
initInputElementFromStorage(`scrollNumberInput`, `maximumScrollNumber`);
initInputElementFromStorage(`groupNameInput`, `groupName`);
initReadOnlyElementFromStorage(logOutput, `logHistory`);
var buttons = document.querySelectorAll('button');
for (var i = 0; i < buttons.length; i++) {
buttons[i].addEventListener('click', trackClick);
}
var links = document.querySelectorAll('a');
for (var i = 0; i < links.length; i++) {
links[i].addEventListener('click', trackClick);
}
});
scrollBtn.onclick = function (e) {
chrome.storage.sync.get(`maximumScrollNumber`, function (data) {
if (scrollBtn.innerText == `Stop scrolling`) {
scrollBtn.style.backgroundColor = `#00ffff`;
scrollBtn.innerText = `Scroll to the bottom`;
chrome.tabs.query({ active: true, currentWindow: true }, function (tabs) {
chrome.tabs.sendMessage(tabs[0].id, { continueScrolling: false }, function (response) {
addToLogOutput(response.message);
});
});
} else {
scrollBtn.innerText = `Stop scrolling`;
scrollBtn.style.backgroundColor = `#ffff99`;
let logMessage = `Starting to scroll [${data.maximumScrollNumber}] times`;
logPort.postMessage({ content: logMessage });
addToLogOutput(logMessage);
chrome.tabs.query({ active: true, currentWindow: true }, function (tabs) {
chrome.tabs.executeScript(
tabs[0].id,
{
code: `var scrollNumber = ${data.maximumScrollNumber}; var continueScrolling = true;`,
},
function () {
chrome.tabs.executeScript(tabs[0].id, {
file: `scrollToBottom.js`,
});
}
);
});
}
});
};
countBtn.onclick = function (e) {
chrome.tabs.query({ active: true, currentWindow: true }, function (tabs) {
chrome.tabs.executeScript(tabs[0].id, {
file: `groupButtonCount.js`,
});
});
};
clearLogBtn.onclick = function (e) {
logOutput.value = '';
const clearPort = chrome.runtime.connect({ name: `clear` });
clearPort.postMessage({ content: 'clearLog' });
};
markCheckedBtn.onclick = function (e) {
if (markCheckedBtn.innerText == `Stop adding to group`) {
markCheckedBtn.style.backgroundColor = `#00ffff`;
markCheckedBtn.innerText = `Add the people on this page to the group`;
chrome.storage.sync.set({ [`continueMarking`]: false });
} else {
markCheckedBtn.innerText = `Stop adding to group`;
markCheckedBtn.style.backgroundColor = `#ffff99`;
chrome.storage.sync.set({ [`continueMarking`]: true });
chrome.storage.sync.get(`groupName`, function (data) {
let logMessage = `Adding people to group [${data.groupName}]`;
logPort.postMessage({ content: logMessage });
addToLogOutput(logMessage);
chrome.tabs.query({ active: true, currentWindow: true }, function (tabs) {
chrome.tabs.executeScript(
tabs[0].id,
{
code: `var groupNamePassthrough = "${data.groupName}"; var continueMarking = true;`,
},
function () {
chrome.tabs.executeScript(tabs[0].id, {
file: `markChecked.js`,
});
}
);
});
});
}
};
function initInputElementFromStorage(elementName, storageKey) {
let element = document.getElementById(elementName);
chrome.storage.sync.get(storageKey, function (data) {
element.value = data[storageKey];
logPort.postMessage({ content: `${storageKey} is [${element.value}]` });
});
element.addEventListener(`change`, function () {
chrome.storage.sync.set({ [`${storageKey}`]: element.value }, function () {
logPort.postMessage({ content: `${storageKey} set [${element.value}]` });
});
});
}
function initReadOnlyElementFromStorage(element, storageKey) {
chrome.storage.sync.get(storageKey, function (data) {
if (data[storageKey] != null) {
element.value = data[storageKey];
let logMessage = `${storageKey} has been loaded`;
logPort.postMessage({ content: logMessage });
addToLogOutput(logMessage);
logOutput.scrollTop = logOutput.scrollHeight;
}
});
}
chrome.runtime.onConnect.addListener(function (port) {
if (port.name == `log`) {
port.onMessage.addListener(function (message) {
addToLogOutput(message.content);
});
}
});
function addToLogOutput(message) {
logOutput.value += `\n` + message;
logOutput.scrollTop = logOutput.scrollHeight;
}
var _gaq = _gaq || [];
_gaq.push(['_setAccount', '{{ GOOGLE_ANALYTICS_TRACKING_ID }}']);
_gaq.push(['_trackPageview']);
(function () {
var ga = document.createElement('script');
ga.type = 'text/javascript';
ga.async = true;
ga.src = 'https://ssl.google-analytics.com/ga.js';
var s = document.getElementsByTagName('script')[0];
s.parentNode.insertBefore(ga, s);
})();
function trackClick(e) {
_gaq.push(['_trackEvent', e.target.id, 'clicked']);
}