-
-
Notifications
You must be signed in to change notification settings - Fork 9
/
pack.js
139 lines (123 loc) · 4.89 KB
/
pack.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
/*
This script generates Firefox version of the extension and packs Chrome and Firefox versions to zip files.
Node.js v16.6.1 recommended.
*/
const fsp = require('fs').promises;
const fs = require('fs');
const path = require('path');
const AdmZip = require('adm-zip');
const sleep = (ms) => new Promise(resolve => setTimeout(resolve, ms));
async function copyDir(src, dest) {
const entries = await fsp.readdir(src, { withFileTypes: true });
await fsp.mkdir(dest);
for (let entry of entries) {
if(entry.name === '.git' || entry.name === '.github' || entry.name === '_metadata' || entry.name === 'build') continue;
const srcPath = path.join(src, entry.name);
const destPath = path.join(dest, entry.name);
if (entry.isDirectory()) {
await copyDir(srcPath, destPath);
} else {
await fsp.copyFile(srcPath, destPath);
}
}
}
if(!fs.existsSync('./build')) {
fs.mkdirSync('./build');
} else {
fs.rmSync('./build', { recursive: true });
fs.mkdirSync('./build');
}
console.log("Creating userscript...");
let manifest = JSON.parse(fs.readFileSync('./manifest.json', 'utf8'));
let rosetta = fs.readFileSync('./fonts/rosetta.woff', 'base64');
let fullCode = fs.readFileSync('./us.js', 'utf8')
.replaceAll('${manifest.version}', manifest.version)
.replaceAll('{{font_url}}', rosetta);
// Embed images due to CSP
const embeddedImages = ['yeah_on32.png', 'yeah_off32.png', 'loading.svg'];
for(let img of embeddedImages) {
fullCode += `\nYEAH_images['${img}'] = 'data:${img.endsWith('.svg') ? 'image/svg+xml' : 'image/png'};charset=utf-8;base64,${fs.readFileSync(`./images/${img}`, 'base64')}'`
}
for(let i in manifest.content_scripts) {
let script = manifest.content_scripts[i];
for(let j in script.js) {
let code = fs.readFileSync(script.js[j], 'utf8')
.replaceAll('fetch(', 'GM_fetch(')
.replace(/chrome\.runtime\.getURL\('(.+?)'\)/gm, "'https://raw.githubusercontent.com/dimdenGD/YeahTwitter/main/$1'");
for(let img of embeddedImages) {
code = code.replaceAll(`'https://raw.githubusercontent.com/dimdenGD/YeahTwitter/main/images/${img}'`, `YEAH_images['${img}']`);
}
fullCode += `\n\n// ${script.js[j]}\n` + code;
}
}
fs.writeFileSync('./build/userscript.js', fullCode);
if(fs.existsSync('./build/chrome')) {
fs.rmSync('./build/chrome', { recursive: true });
}
if(fs.existsSync('./build/firefox')) {
fs.rmSync('./build/firefox', { recursive: true });
}
console.log("Copying...");
copyDir('./', './build/firefox').then(async () => {
await copyDir('./', './build/chrome');
console.log("Copied!");
console.log("Patching...");
let manifest = JSON.parse(await fsp.readFile('./build/chrome/manifest.json', 'utf8'));
manifest.manifest_version = 2;
manifest.permissions.push("webRequest", "webRequestBlocking", ...manifest.host_permissions);
delete manifest.host_permissions;
manifest.web_accessible_resources = manifest.web_accessible_resources[0].resources;
manifest.browser_specific_settings = {
gecko: {
id: "yeah@dimden.dev",
strict_min_version: "101.0"
},
gecko_android: {
strict_min_version: "101.0"
}
};
manifest.browser_action = manifest.action;
delete manifest.action;
fs.writeFileSync('./build/firefox/manifest.json', JSON.stringify(manifest, null, 2));
fs.unlinkSync('./build/firefox/pack.js');
fs.unlinkSync('./build/chrome/pack.js');
fs.unlinkSync('./build/firefox/.gitignore');
fs.unlinkSync('./build/chrome/.gitignore');
fs.unlinkSync('./build/firefox/us.js');
fs.unlinkSync('./build/chrome/us.js');
console.log("Patched!");
if (fs.existsSync('./build/firefox.zip')) {
console.log("Deleting old zip...");
fs.unlinkSync('./build/firefox.zip');
console.log("Deleted old zip!");
}
console.log("Zipping Firefox version...");
try {
const zip = new AdmZip();
const outputDir = "./build/firefox.zip";
zip.addLocalFolder("./build/firefox");
zip.writeZip(outputDir);
} catch (e) {
console.log(`Something went wrong ${e}`);
}
if (fs.existsSync('./build/chrome.zip')) {
console.log("Deleting old zip...");
fs.unlinkSync('./build/chrome.zip');
console.log("Deleted old zip!");
}
console.log("Zipping Chrome version...");
try {
const zip = new AdmZip();
const outputDir = "./build/chrome.zip";
zip.addLocalFolder("./build/chrome");
zip.writeZip(outputDir);
} catch (e) {
console.log(`Something went wrong ${e}`);
}
console.log("Zipped!");
console.log("Deleting temporary folders...");
await sleep(100);
fs.rmSync('./build/chrome', { recursive: true, force: true });
fs.rmSync('./build/firefox', { recursive: true, force: true });
console.log("Deleted!");
});