-
Notifications
You must be signed in to change notification settings - Fork 4
/
popup.js
44 lines (39 loc) · 1.53 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
function copyToClipboard(text) {
var copyFrom = document.createElement("textarea");
copyFrom.textContent = text;
var body = document.getElementsByTagName('body')[0];
body.appendChild(copyFrom);
copyFrom.select();
document.execCommand('copy');
body.removeChild(copyFrom);
}
function formatResponse(response) {
var result = "";
var separator = "\t";
var lineBreak = "\n";
result = result + "Facebook Name" + separator + response.questions.join(separator) + lineBreak;
response.members.forEach((member) => {
result = result + member.facebookName;
response.questions.forEach((question) => {
var qa = member.answers.find((item) => item.question === question);
if (qa !== undefined)
result = result + separator + qa.answer.replace(/(\r\n|\n|\r|\t)/gm, "");
else
result = result + separator;
});
result = result + lineBreak;
});
return result;
}
$(function() {
chrome.tabs.getSelected(null, function(tab) {
chrome.tabs.sendMessage(tab.id, { action: "extractFacebookGroupAnswers" }, function(response) {
if (response.members.length > 0) {
copyToClipboard(formatResponse(response));
$("#result").html(`<h3>Yay!</h3><p>${response.members.length} members with answers found and copied to clipboard.</p><p>Paste data into Google Sheets or Excel.</p>`);
} else {
$("#result").html("<h3>Ooops!</h3><p>No answers found.</p><p>Wait a little bit before using the extension and scroll to the bottom to load the all data first.</p>");
}
});
});
});