-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathmouthful-cli.js
executable file
·62 lines (62 loc) · 1.67 KB
/
mouthful-cli.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
#!/usr/bin/env node
const phantom = require('phantom');
const https = require('https');
const fs = require('fs');
const cheerio = require('cheerio');
const args = process.argv.slice(2);
let mouthful = async (url, path) => {
const instance = await phantom.create();
const page = await instance.createPage();
const download = (index) => {
https.get(urls[index], (resp) => {
let data = '';
resp.on('data', (chunk) => {
data += chunk;
});
resp.on('end', () => {
stylesheet += data;
if (urls[index + 1]) {
download(index + 1);
} else {
stylesheet += inline;
let _stylesheet = stylesheet.replace(/\n/g, '');
fs.writeFile(path, _stylesheet, function(err) {
if (err) {
console.log('Error: ' + err);
process.exit(1);
}
console.log('The file was saved!');
process.exit(0);
});
}
});
}).on('error', (err) => {
console.log('Error: ' + err.message);
process.exit(1);
});
}
let urls = [];
let stylesheet = '';
let inline = '';
let status = null;
let content = null;
await page.on('onResourceRequested', (requestData) => {
if (requestData.headers[0].value.indexOf('text/css') !== -1) {
urls.push(requestData.url);
}
});
status = await page.open(url);
content = await page.property('content');
const $ = cheerio.load(content);
$('style').each((i, el) => {
inline += $(el).html();
});
await instance.exit();
download(0);
}
if (args[0] && args[1]) {
mouthful(args[0], args[1]);
} else {
console.log('Error: Argument error');
process.exit(1);
}