This repository has been archived by the owner on Feb 12, 2021. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathindex.js
51 lines (45 loc) · 1.6 KB
/
index.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
const fs = require('fs');
const util = require('util');
const args = require('mri')(process.argv.slice(2));
const asar = require('asar');
const fse = require('fs-extra');
const readFile = util.promisify(fs.readFile);
const writeFile = util.promisify(fs.writeFile);
(async () => {
const js = `mainWindow.webContents.on('dom-ready', () => {
setTimeout(() => {
mainWindow.webContents.executeJavaScript(\`${fs.readFileSync('for-injection.js')}\`);
}, 3000);
});mainWindow.webContents.`;
const corePath = args.core || 'core.asar';
const targetPath = 'tmp/app/mainScreen.js';
const backupPath = `${targetPath}.backup`;
await fse.remove('tmp').catch(err => {
console.log(err);
});
util.promisify(fs.copyFile)(corePath, 'core.asar.backup');
asar.extractAll(corePath, 'tmp');
if (args.revert) {
const backup = await readFile(backupPath);
await writeFile(targetPath, backup);
console.log('Reverting from backup...');
} else if (args.inject) {
const target = await readFile(targetPath, 'utf-8');
if (target.includes('mainWindow.webContents.on(\'dom-ready\', () => {')) {
console.log('Maybe already injected.');
}
const injected = target.replace('mainWindow.webContents.', js);
await writeFile(targetPath, injected);
await writeFile(backupPath, target, {flag: 'wx'}).catch(() => {
// File already exists
});
console.log('Injecting...');
} else {
console.log('Just extract core.asar and pack core.asar.');
}
asar.createPackage('tmp', 'core.asar', err => {
if (err) {
console.log('Failed to create core.asar');
}
});
})();