-
Notifications
You must be signed in to change notification settings - Fork 11
/
background.js
146 lines (131 loc) · 4.87 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
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
document.getElementById("copyButton").addEventListener("click", function () {
handleAction(copyToClipboard);
changeButtonTextTemporarily("copyButton", "Copied!");
});
document
.getElementById("downloadButton")
.addEventListener("click", function () {
handleAction(saveToFile);
changeButtonTextTemporarily("downloadButton", "Downloaded!");
});
const CODE = `
var projectName = document.title.split(" | ")[0];
var tasks = [];
var learningObjectives = [];
var resources = [];
document.querySelectorAll('div[id^="task-num"]').forEach(taskDiv => {
const title = taskDiv.querySelector('h3.panel-title')?.outerText;
const task_type = taskDiv.querySelector('span.label.label-info')?.outerText;
const files = taskDiv.querySelector('div.list-group-item ul li:nth-child(3) code')?.outerText;
const files_array = files ? files.split(", ") : null;
tasks.push({ title, task_type, files: files_array });
});
document.querySelectorAll('h2').forEach(h2 => {
if (h2.textContent.includes("Learning Objectives")) {
h2.nextElementSibling.nextElementSibling.nextElementSibling.querySelectorAll("li").forEach(li => {
learningObjectives.push(li.innerHTML);
});
}
if (h2.textContent.includes("Resources")) {
h2.nextElementSibling.nextElementSibling.querySelectorAll("li").forEach(li => {
const link = li.querySelector("a");
const url = link?.href || "";
const title = link?.title || li.textContent;
resources.push({ resource: title, link: url });
});
}
});
[projectName, tasks, learningObjectives, resources];
`;
function handleAction(actionCallback) {
chrome.tabs.query({ active: true, currentWindow: true }, function (tabs) {
const tab = tabs[0];
if (!tab.url.startsWith("https://intranet.alxswe.com/projects/")) {
showPopup("This is not an ALX project page! 🐞");
return;
}
try {
chrome.tabs.executeScript(
tab.id,
{
code: CODE,
},
function (results) {
var projectName = results[0][0];
var tasks = results[0][1];
var learningObjectives = results[0][2];
var resources = results[0][3];
var copiedText = `# ${projectName}\n\n`;
if (resources.length !== 0) {
copiedText += "## Resources\n\n";
copiedText += "#### Read or watch:\n\n";
resources.forEach(function (obj) {
copiedText += `* [${obj.resource}](${obj.link})\n`;
});
}
if (learningObjectives.length !== 0) {
copiedText += "## Learning Objectives\n\n";
copiedText += "### General\n\n";
learningObjectives.forEach(function (obj) {
copiedText += "* " + obj + "\n";
});
}
copiedText += "## Tasks\n\n";
copiedText += "| Task | File |\n";
copiedText += "| ---- | ---- |\n";
tasks.forEach(function (task) {
let string_files = "";
if (task.files) {
task.files.forEach((file) => {
string_files += `[${file}](./${file}), `;
});
string_files = string_files.slice(0, -2);
} else {
string_files = "[SOON](./)";
}
copiedText += `| ${task.title} | ${string_files} |\n`;
});
if (copiedText.length === 0) {
showPopup("Nothing found to scan 😫");
return;
}
actionCallback(copiedText);
}
);
} catch (e) {
showPopup("Something went wrong, Try again! 🐞");
}
});
}
function saveToFile(text) {
var blob = new Blob([text], { type: "text/plain" });
var fileName = "README.md";
var downloadLink = document.createElement("a");
downloadLink.href = URL.createObjectURL(blob);
downloadLink.download = fileName;
downloadLink.click();
}
function copyToClipboard(text) {
var textArea = document.createElement("textarea");
textArea.value = text;
document.body.appendChild(textArea);
textArea.select();
document.execCommand("copy");
document.body.removeChild(textArea);
}
function showPopup(message) {
var popup = document.getElementById("popup");
popup.textContent = message;
popup.style.display = "block";
setTimeout(function () {
popup.style.display = "none";
}, 3000);
}
function changeButtonTextTemporarily(buttonId, newText) {
var button = document.getElementById(buttonId);
var originalText = button.textContent;
button.textContent = newText;
setTimeout(function () {
button.textContent = originalText;
}, 3000);
}