-
Notifications
You must be signed in to change notification settings - Fork 1
/
background.js
101 lines (92 loc) · 3.36 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
chrome.runtime.onInstalled.addListener(function () {
chrome.declarativeContent.onPageChanged.removeRules(undefined, function () {
chrome.declarativeContent.onPageChanged.addRules([
{
conditions: [
new chrome.declarativeContent.PageStateMatcher({
pageUrl: { hostEquals: 'praktikum.yandex.ru', pathPrefix: '/trainer/' }
}),
new chrome.declarativeContent.PageStateMatcher({
pageUrl: { hostEquals: 'practicum.yandex.com', pathPrefix: '/trainer/' }
})
],
actions: [new chrome.declarativeContent.ShowPageAction()]
}
]);
});
});
chrome.runtime.onMessage.addListener(function (request, sender, sendResponse) {
if (request.ext !== "Praktikum") {
return;
}
if (request.action === "download") {
sendResponse(download(request.tabCode, request.tabName));
return;
}
if (request.action === "downloadTabsAsArchive") {
sendResponse(downloadTabsAsArchive(request.tabsData));
return;
}
if (request.action === "shareTabsWithCodepen") {
sendResponse(shareTabsWithCodepen(request.tabsData));
return;
}
});
function shareTabsWithCodepen(allTabsData){
const data = {
"title": "Praktikum",
"html": allTabsData.filter(tab=>tab.tabName == 'index.html')[0].tabCode,
"css": allTabsData.filter(tab=>tab.tabName.match(/css$/))
.map(tab => `\n\n/* file: ${tab.tabName}*/\n\n${tab.tabCode}`)
.join(''),
"js": allTabsData.filter(tab=>tab.tabName.match(/js$/))
.map(tab => `\n\n/* file: ${tab.tabName}*/\n\n${tab.tabCode}`)
.join(''),
}
const div = document.createElement("div");
div.innerHTML = `
<form class="share-with-codepen" action="https://codepen.io/pen/define" method="POST" target="_blank">
<input type="hidden" name="data" value='${JSON.stringify(data).replace(/'/g, "'")}'>
<input type="submit" value="Create New Pen with Prefilled Data">
</form>
`;
document.querySelector("body").appendChild(div);
document.querySelector('.share-with-codepen').submit();
div.parentNode.removeChild(div);
}
function download(tabCode, tabName) {
const blob = new Blob([tabCode], { type: "text/plain" });
const url = URL.createObjectURL(blob);
chrome.downloads.download({
url: url,
filename: tabName
});
return { message: "OK" };
}
function getArchiveFilename() {
return "praktikum" + Date.now() + ".zip";
}
function addScreenshot(zip, callback) {
chrome.tabs.captureVisibleTab(null, { format: "png" }, function (imageData) {
var imageSplit = imageData.split(',');
zip.file("screenshot.png", imageSplit[1], {base64: true});
if (callback) {
callback(zip);
}
});
}
async function downloadTabsAsArchive(tabsData) {
const zip = new JSZip();
tabsData.forEach(tab => {
zip.file(tab.tabName, tab.tabCode);
});
addScreenshot(zip, async function () {
const blob = await zip.generateAsync({ type: "blob" });
const url = URL.createObjectURL(blob);
chrome.downloads.download({
url: url,
filename: getArchiveFilename()
});
});
return { message: "OK" };
}