-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcontent.js
95 lines (81 loc) · 2.71 KB
/
content.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
let running = false;
let status;
let fontZip;
// create download button
const downloadButton = document.createElement('a');
downloadButton.classList = 'o-button o-button--primary-big o-button-buy-flow';
downloadButton.style.marginRight = '.14286em';
setStatus('Download');
const buyButton = document.getElementsByClassName('o-button o-button--primary-big o-button-buy-flow js-button')[0];
// insert download button after buy button
buyButton.parentNode.insertBefore(downloadButton, buyButton.nextSibling);
downloadButton.addEventListener('click', function() {
if (running) {
alert('There is a download already in progress!');
} else {
running = true;
fontZip = new JSZip();
main();
}
});
function main() {
setStatus('Downloading...', '#FF6447');
let fontObjects = [];
const re_url = /url\(\"\/\/(.*?)\"\)/;
const re_name = /Buy (.*?) fonts/;
const re_meta = /Designed by (.*?) in (.*?)\./;
let fonts = document.getElementsByClassName('font-loader-injection');
let fontTitle = document.getElementsByClassName('m-cover__title')[0].innerText;
let fontMeta = document.getElementsByClassName('m-cover__meta')[0].innerText.match(re_meta);
let fontAuthor = fontMeta[1], fontYear = fontMeta[2];
let zipName = `${fontTitle} by ${fontAuthor} (${fontYear})`;
for (let i = 0; i < fonts.length; i++) {
let rules = fonts[i].sheet.cssRules;
for (let j = 0; j < rules.length; j++) {
let style = rules[j].style;
let fontId = style.fontFamily.replaceAll('\"', '');
let fontUrl = style.src.match(re_url);
if (fontUrl && !fontId.includes('FSFallback')) {
fontObjects.push({ name: '', id: fontId, url: 'https://' + fontUrl[1], base64: '' });
}
}
}
// populate font name fields
for (obj of fontObjects) {
let fontElement = document.querySelector(`span[data-fontfamily="${obj.id}"]`);
let fontName = fontElement.title.match(re_name)[1];
obj.name = fontName;
}
chrome.runtime.sendMessage(
{ array: fontObjects },
array => {
// add all fonts to ZIP
array.forEach(obj => fontZip.file(
obj.name + '.woff',
obj.base64,
{ base64: true }
));
// serve ZIP to user
download(fontZip, zipName);
}
);
}
function download(zip, name) {
zip.generateAsync({ type:'blob' }).then(blob => {
const url = window.URL.createObjectURL(blob);
const a = document.createElement('a');
a.style.display = 'none';
a.href = url;
a.download = name + '.zip';
document.body.appendChild(a);
a.click();
window.URL.revokeObjectURL(url);
setStatus('Downloaded!', '#71E690');
running = false;
});
}
// helper function to update download button text
function setStatus(text, color = null) {
downloadButton.style.backgroundColor = downloadButton.style.borderColor = color;
downloadButton.innerText = text;
}