-
Notifications
You must be signed in to change notification settings - Fork 50
/
Copy pathbundle-extension.js
289 lines (253 loc) · 7.84 KB
/
bundle-extension.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
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
import { exec } from "child_process";
import { copy } from "fs-extra";
import { copyFile, rm, writeFile } from "fs/promises";
import process from "process";
import readline from "readline";
import zipper from "zip-local";
const runCommand = (command, yes) =>
new Promise((resolve, reject) => {
exec(yes ? `echo "y" | ${command}` : command, (error, stdout, stderr) => {
if (error) {
reject(error);
} else {
resolve({ stdout, stderr });
}
});
});
let manifest = {
name: "Minimal Theme for Twitter / X",
short_name: "Minimal Twitter",
description: "Refine and declutter the 𝕏/Twitter web experience.",
version: "6.1.1",
icons: {
16: "images/MinimalTwitterIcon16.png",
32: "images/MinimalTwitterIcon32.png",
48: "images/MinimalTwitterIcon48.png",
128: "images/MinimalTwitterIcon128.png",
},
permissions: ["storage"],
options_ui: {
page: "index.html",
open_in_tab: true,
},
};
const MANIFEST_CHROME = {
...manifest,
manifest_version: 3,
background: {
service_worker: "background.js",
type: "module",
},
content_scripts: [
{
run_at: "document_end",
matches: [
"https://twitter.com/*",
"https://mobile.twitter.com/*",
"https://x.com/*",
],
js: ["dist/main.js"],
},
],
web_accessible_resources: [
{
resources: [
"css/main.css",
"css/typefully.css",
"fonts/inter-subset.woff2",
"https://cdn.jsdelivr.net/gh/typefully/[email protected]/css/main.css",
"https://cdn.jsdelivr.net/gh/typefully/[email protected]/css/typefully.css",
],
matches: [
"https://twitter.com/*",
"https://mobile.twitter.com/*",
"https://x.com/*",
],
},
],
action: {
default_icon: {
16: "images/MinimalTwitterIcon16.png",
32: "images/MinimalTwitterIcon32.png",
48: "images/MinimalTwitterIcon48.png",
},
default_title: "Minimal Twitter",
default_popup: "index.html",
},
};
const MANIFEST_FIREFOX = {
...manifest,
manifest_version: 2,
browser_specific_settings: {
gecko: {
id: "{e7476172-097c-4b77-b56e-f56a894adca9}",
},
},
background: {
scripts: ["background.js"],
persistent: false,
},
content_scripts: [
{
run_at: "document_idle",
matches: [
"https://twitter.com/*",
"https://mobile.twitter.com/*",
"https://x.com/*",
],
js: ["dist/main.js"],
},
],
web_accessible_resources: [
"css/main.css",
"css/typefully.css",
"fonts/inter-subset.woff2",
"https://cdn.jsdelivr.net/gh/typefully/[email protected]/css/main.css",
"https://cdn.jsdelivr.net/gh/typefully/[email protected]/css/typefully.css",
],
browser_action: {
default_icon: {
16: "images/MinimalTwitterIcon16.png",
32: "images/MinimalTwitterIcon32.png",
48: "images/MinimalTwitterIcon48.png",
},
default_title: "Minimal Twitter",
default_popup: "index.html",
},
};
const bundle = async (manifest, bundleDirectory) => {
try {
// Remove old bundle directory
await rm(bundleDirectory, { recursive: true, force: true }); // requires node 14+
console.log(`🧹 Cleaned up \`${bundleDirectory}\` directory.`);
// Run both build scripts
const runBuildScript = (directory) => {
return new Promise(async (resolve, reject) => {
let intervalId;
let spinner = "\\";
const startBuilding = () => {
let P = ["\\", "|", "/", "-"];
intervalId = setInterval(() => {
process.stdout.clearLine();
process.stdout.cursorTo(0);
spinner = P[P.indexOf(spinner) + 1] || P[0];
process.stdout.write(
`${spinner} Building popup and content scripts...`
);
}, 250);
};
startBuilding();
try {
await runCommand(`cd ./${directory} && yarn && yarn build`);
clearInterval(intervalId);
resolve();
} catch (error) {
clearInterval(intervalId);
console.error(
`Error running build script for ${directory}: ${error}`
);
reject(error);
}
});
};
await runBuildScript("popup");
await runBuildScript("content-scripts");
process.stdout.clearLine();
process.stdout.cursorTo(0);
console.log("🔥 Built popup and content scripts.");
// Bundle popup Next.js export
await copy("popup/out", `${bundleDirectory}`);
console.log(`🚗 Moved export to bundle.`);
// Bundle content-scripts
await copy("content-scripts/dist", `${bundleDirectory}/dist`);
console.log(`🚗 Moved content_scripts to bundle.`);
// Bundle background.js
await copyFile("background.js", `${bundleDirectory}/background.js`);
console.log(`🚗 Moved background.js to bundle.`);
// Bundle css
await copy("css", `${bundleDirectory}/css`);
console.log(`🚗 Moved css to bundle.`);
// Bundle fonts
await copy("fonts", `${bundleDirectory}/fonts`);
console.log(`🚗 Moved fonts to bundle.`);
// Bundle images
await copy("images", `${bundleDirectory}/images`);
console.log(`🚗 Moved images to bundle.`);
// Create manifest
await writeFile(
`${bundleDirectory}/manifest.json`,
Buffer.from(JSON.stringify(manifest, null, 2)),
"utf8"
);
// Done.
console.log(`📦 Bundled \`${bundleDirectory}\`.`);
// Zip the directory
zipper.sync
.zip(`./${bundleDirectory}`)
.compress()
.save(`./bundle/${bundleDirectory.replace("bundle/", "")}.zip`);
console.log(
`🧬 Zipped \`${bundleDirectory}\` to \`bundle/${bundleDirectory.replace(
"bundle/",
""
)}.zip\`.`
);
} catch (error) {
console.error(error);
}
};
const bundleAll = async () => {
await bundle(MANIFEST_CHROME, "bundle/chrome");
await bundle(MANIFEST_FIREFOX, "bundle/firefox");
};
const rl = readline.createInterface({
input: process.stdin,
output: process.stdout,
});
rl.question(
"Which browser would you like to bundle for? [All / Chrome / Firefox / Safari] ",
async (browser) => {
switch (browser) {
case "Chrome":
await bundle(MANIFEST_CHROME, "bundle/chrome");
break;
case "Firefox":
await bundle(MANIFEST_FIREFOX, "bundle/firefox");
break;
case "Safari":
await bundle(MANIFEST_FIREFOX, "bundle/firefox");
let intervalId;
let spinner = "\\";
const startBuilding = () => {
let P = ["\\", "|", "/", "-"];
intervalId = setInterval(() => {
process.stdout.clearLine();
process.stdout.cursorTo(0);
spinner = P[P.indexOf(spinner) + 1] || P[0];
process.stdout.write(`${spinner} Bundling Safari...`);
}, 250);
};
startBuilding();
await runCommand(generateSafariProjectCommand, true);
await runCommand(fixBundleIdentifierCommand, true);
clearInterval(intervalId);
break;
case "All":
await bundleAll();
break;
default:
await bundleAll();
}
rl.close();
}
);
rl.on("close", () => {
process.exit(0);
});
const generateSafariProjectCommand = `xcrun safari-web-extension-converter bundle/firefox --project-location bundle/safari --app-name 'Minimal Twitter' --bundle-identifier 'com.typefully.minimal-twitter'`;
// The first command currently ignores the full --bundle-identifier flag (it still take the company name), so a replace is required to make sure it matches our bundle identifier
const fixBundleIdentifierCommand = `find "bundle/safari/Minimal Twitter" \\( -name "*.swift" -or -name "*.pbxproj" \\) -type f -exec sed -i '' 's/com.typefully.Minimal-Twitter/com.typefully.minimal-twitter/g' {} +`;
/*--- Bundle without prompting
await bundleAll();
process.exit(0);
---*/