This repository has been archived by the owner on Mar 29, 2024. It is now read-only.
-
-
Notifications
You must be signed in to change notification settings - Fork 2
/
index.js
78 lines (72 loc) · 2.78 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
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
const puppeteer = require('puppeteer');
const sh = require('shelljs');
const open = require('open');
const fs = require('fs');
const readline = require('readline');
const args = process.argv.slice(2);
const data = require('../vimshot-config.json');
function generateJsFile(pathJsonFile, pathOutput){
fs.readFile(pathJsonFile, "utf-8", (err, data) => {
if (err) console.log(err);
fs.writeFile(pathOutput, `const data=${data.trim()};`, (err) => {
if (err) console.log(err);
});
});
}
function encodeHtml(rawFile){
const data = fs.readFileSync(rawFile, 'utf-8');
const lines = data.split(/\r?\n/);
let newContent = "", i = 0;
lines.forEach((line) => {
if(i >= 115 && i <= lines.length - 9){
let newline = line.replaceAll("&", "&").replaceAll("<", "<").replaceAll(">", ">") + ((i < lines.length - 2) ? "\n" : "");
newContent += newline;
console.log(newline);
} else
newContent += line + "\n";
i++;
});
fs.writeFile(rawFile, newContent, err => console.log);
}
var adjustNum = function adjustNum(value) {
return value < 10 ? "0" + value : value;
};
async function main() {
if (args.length <= 0) throw new Error("This Application require arguments");
console.dir(args);
switch(args[0]){
case "genjs":
generateJsFile(args[1], args[2]);
break;
case "screenshot":
sh.mkdir("-p", args[2]);
encodeHtml(args[1]);
puppeteer.launch({
args: [
'--no-sandbox',
'--disable-setuid-sandbox',
'--disable-dev-shm-usage'
]}).then(async browser => {
const page = await browser.newPage();
await page.goto("file://" + args[1], {
waitUntil: 'domcontentloaded'
}); // index File
await page.waitForSelector('#my-node');
try {
const url = await page.$('#my-node')
const contentBuffer = await url.screenshot({ omitBackground: (data.backgroundTransparent == 'true') });
var d = new Date();
var date = "_" + adjustNum(d.getMonth()) + adjustNum(d.getDay())+ d.getFullYear() + "_" + adjustNum(d.getHours()) + adjustNum(d.getMinutes()) + adjustNum(d.getSeconds());
fs.writeFileSync(args[2]+"/Vim-Screenshot"+date+".png", contentBuffer, 'base64');
} catch (e) {
console.log(e);
}
browser.close();
});
break;
default:
open(args[0]);
break;
}
}
main();