forked from dimdenGD/OldTwitter
-
Notifications
You must be signed in to change notification settings - Fork 0
/
pack.js
179 lines (166 loc) · 8.67 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
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
// This script generates Firefox version of the extension and packs Chrome and Firefox versions to zip files.
const fsp = require('fs').promises;
const fs = require('fs');
const path = require('path');
const AdmZip = require('adm-zip');
const args = process.argv.slice(2);
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 === 'node_modules') 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('../OldTwitterTempChrome')) {
fs.rmSync('../OldTwitterTempChrome', { recursive: true });
}
if(fs.existsSync('../OldTwitterFirefox')) {
fs.rmSync('../OldTwitterFirefox', { recursive: true });
}
console.log("Copying...");
copyDir('./', '../OldTwitterFirefox').then(async () => {
await copyDir('./', '../OldTwitterTempChrome');
console.log("Copied!");
console.log("Patching...");
let manifest = JSON.parse(fs.readFileSync('../OldTwitterFirefox/manifest.json', 'utf8'));
manifest.manifest_version = 2;
manifest.background.scripts = ['scripts/background.js'];
manifest.web_accessible_resources = manifest.web_accessible_resources[0].resources;
manifest.permissions = manifest.permissions.filter(p => p !== 'declarativeNetRequest' && p !== 'contextMenus');
manifest.browser_specific_settings = {
"gecko": {
"strict_min_version": "78.0"
},
"gecko_android": {
"strict_min_version": "78.0"
}
}
if(args[0] === '-a') {
manifest.browser_specific_settings.gecko.id = "oldtwitter@dimden.dev";
} else {
setTimeout(() => console.warn("Warning: Extension ID is not set."), 1500);
}
manifest.permissions = [
...manifest.permissions,
...manifest.host_permissions,
"https://dimden.dev/*",
"https://raw.githubusercontent.com/*",
"webRequest",
"webRequestBlocking"
];
delete manifest.sandbox;
delete manifest.host_permissions;
delete manifest.declarative_net_request;
delete manifest.background.service_worker;
delete manifest.action;
let config = fs.readFileSync('../OldTwitterFirefox/scripts/config.js', 'utf8');
config = config.replace(/chrome\.storage\.sync\./g, "chrome.storage.local.");
let helpers = fs.readFileSync('../OldTwitterFirefox/scripts/helpers.js', 'utf8');
helpers = helpers.replace(/chrome\.storage\.sync\./g, "chrome.storage.local.");
let tweetviewer = fs.readFileSync('../OldTwitterFirefox/scripts/tweetviewer.js', 'utf8');
tweetviewer = tweetviewer.replace(/chrome\.storage\.sync\./g, "chrome.storage.local.");
let content = fs.readFileSync('../OldTwitterFirefox/scripts/injection.js', 'utf8');
content = content.replace(/chrome\.storage\.sync\./g, "chrome.storage.local.");
let apis = fs.readFileSync('../OldTwitterFirefox/scripts/apis.js', 'utf8');
apis = apis.replace(/chrome\.storage\.sync\./g, "chrome.storage.local.");
if(apis.includes("&& true") || apis.includes("&& false") || apis.includes("|| true") || apis.includes("|| false") || apis.includes("&&true") || apis.includes("&&false") || apis.includes("||true") || apis.includes("||false")) {
if(args[0] === '-a') {
let line = apis.split("\n").findIndex(l => l.includes("&& true") || l.includes("&& false") || l.includes("|| true") || l.includes("|| false") || l.includes("&&true") || l.includes("&&false") || l.includes("||true") || l.includes("||false"));
console.warn("::warning file=scripts/api.js,line=" + (line+1) + "::Probably temporary boolean left in code.");
} else {
for(let i = 0; i < 5; i++) {
console.warn("\x1b[33m", "Warning: probably temporary boolean left in code.", '\x1b[0m');
}
}
}
if(args[0] !== '-a') {
try {
require('node-fetch')('https://twitter.com/manifest.json').then(res => res.text()).then(json => {
if(json.includes("content_security_policy")) {
for(let i = 0; i < 5; i++) {
console.warn("\x1b[33m", "Warning: Twitter returned CSP in manifest.json!!!!", '\x1b[0m');
}
}
});
} catch(e) {}
}
let background = fs.readFileSync('../OldTwitterFirefox/scripts/background_v2.js', 'utf8');
fs.writeFileSync('../OldTwitterFirefox/manifest.json', JSON.stringify(manifest, null, 2));
fs.writeFileSync('../OldTwitterFirefox/scripts/injection.js', content);
fs.writeFileSync('../OldTwitterFirefox/scripts/helpers.js', helpers);
fs.writeFileSync('../OldTwitterFirefox/scripts/tweetviewer.js', tweetviewer);
fs.writeFileSync('../OldTwitterFirefox/scripts/config.js', config);
fs.writeFileSync('../OldTwitterFirefox/scripts/background.js', background);
fs.writeFileSync('../OldTwitterFirefox/scripts/apis.js', apis);
fs.unlinkSync('../OldTwitterFirefox/ruleset.json');
fs.unlinkSync('../OldTwitterFirefox/pack.js');
fs.unlinkSync('../OldTwitterTempChrome/pack.js');
fs.unlinkSync('../OldTwitterTempChrome/scripts/background_v2.js');
fs.unlinkSync('../OldTwitterFirefox/scripts/background_v2.js');
fs.unlinkSync('../OldTwitterFirefox/.gitignore');
fs.unlinkSync('../OldTwitterTempChrome/.gitignore');
fs.unlinkSync('../OldTwitterFirefox/test.js');
fs.unlinkSync('../OldTwitterTempChrome/test.js');
fs.unlinkSync('../OldTwitterFirefox/package.json');
fs.unlinkSync('../OldTwitterTempChrome/package.json');
fs.unlinkSync('../OldTwitterFirefox/_locales/locales-support.html');
fs.unlinkSync('../OldTwitterTempChrome/_locales/locales-support.html');
if (fs.existsSync('../OldTwitterFirefox/package-lock.json')) // Delete NPM package-lock (if exists)
fs.unlinkSync('../OldTwitterFirefox/package-lock.json');
if (fs.existsSync('../OldTwitterTempChrome/package-lock.json'))
fs.unlinkSync('../OldTwitterTempChrome/package-lock.json');
if (fs.existsSync('../OldTwitterFirefox/yarn.lock')) // Delete yarn package-lock (if exists)
fs.unlinkSync('../OldTwitterFirefox/yarn.lock');
if (fs.existsSync('../OldTwitterTempChrome/yarn.lock'))
fs.unlinkSync('../OldTwitterTempChrome/yarn.lock');
let layouts = fs.readdirSync('../OldTwitterFirefox/layouts');
for (let layout of layouts) {
let script = fs.readFileSync(`../OldTwitterFirefox/layouts/${layout}/script.js`, 'utf8');
script = script.replace(/chrome\.storage\.sync\./g, "chrome.storage.local.");
script = script.replace("https://chrome.google.com/webstore/detail/old-twitter-layout-2022/jgejdcdoeeabklepnkdbglgccjpdgpmf", "https://addons.mozilla.org/en-US/firefox/addon/old-twitter-layout-2022/");
fs.writeFileSync(`../OldTwitterFirefox/layouts/${layout}/script.js`, script);
let style = fs.readFileSync(`../OldTwitterFirefox/layouts/${layout}/style.css`, 'utf8');
style = style.replaceAll("chrome-extension://", "moz-extension://");
fs.writeFileSync(`../OldTwitterFirefox/layouts/${layout}/style.css`, style);
let html = fs.readFileSync(`../OldTwitterFirefox/layouts/${layout}/index.html`, 'utf8');
html = html.replaceAll("chrome-extension://", "moz-extension://");
fs.writeFileSync(`../OldTwitterFirefox/layouts/${layout}/index.html`, html);
}
console.log("Patched!");
if (fs.existsSync('../OldTwitterFirefox.zip')) {
console.log("Deleting old zip...");
fs.unlinkSync('../OldTwitterFirefox.zip');
console.log("Deleted old zip!");
}
console.log("Zipping Firefox version...");
try {
const zip = new AdmZip();
const outputDir = "../OldTwitterFirefox.zip";
zip.addLocalFolder("../OldTwitterFirefox");
zip.writeZip(outputDir);
} catch (e) {
console.log(`Something went wrong ${e}`);
}
console.log(`Zipped Firefox version into ${path.resolve('../OldTwitterFirefox.zip')}!`);
console.log("Zipping Chrome version...");
try {
const zip = new AdmZip();
const outputDir = "../OldTwitterChrome.zip";
zip.addLocalFolder("../OldTwitterTempChrome");
zip.writeZip(outputDir);
} catch (e) {
console.log(`Something went wrong ${e}`);
}
console.log(`Zipped Chrome version into ${path.resolve('../OldTwitterChrome.zip')}!`);
console.log("Deleting temporary folders...");
fs.rmSync('../OldTwitterTempChrome', { recursive: true });
fs.rmSync('../OldTwitterFirefox', { recursive: true });
console.log("Deleted!");
});