Skip to content

Instantly share code, notes, and snippets.

@pczajkowski
Created April 10, 2020 11:19
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save pczajkowski/dac5d384c0478b3258e5281b7b4e8707 to your computer and use it in GitHub Desktop.
Save pczajkowski/dac5d384c0478b3258e5281b7b4e8707 to your computer and use it in GitHub Desktop.
printTextForGivenElement.js
const fs = require('fs');
const cheerio = require('cheerio');
const puppeteer = require('puppeteer');
let app = {};
app.parse = async function(data, elementSelector) {
if (data.length === 0) {
console.log("No data!");
return;
}
const $ = cheerio.load(data);
const elements = $(elementSelector);
elements.each(function(_, elem) {
const text = $(this).text();
if (text.length > 0) {
console.log(text);
} else {
console.log($.html(this));
}
});
};
app.getDataFromURL = async function getPage(link) {
const device = puppeteer.devices['iPad Pro landscape'];
const browser = await puppeteer.launch({args: ['--no-sandbox']});
const page = await browser.newPage();
await page.emulate(device);
await page.goto(link, {waitUntil: 'networkidle2'});
const content = await page.content();
await browser.close();
return content;
};
app.getDataFromFile = function(file, elementSelector) {
fs.readFile(file, "utf8", function read(err, data) {
if (err) {
throw err;
}
app.parse(data, elementSelector);
});
};
app.usage = function() {
console.log(`You must specify path to the file and element selector!
Possible selectors:
<element_name>
.<class_name>
#<id_name>`);
};
app.main = async function() {
if (process.argv.length < 4) {
app.usage();
return;
}
const path = process.argv[2];
const elementSelector = process.argv[3];
if (fs.existsSync(path)) {
app.getDataFromFile(path, elementSelector);
} else {
const data = await app.getDataFromURL(path);
app.parse(data, elementSelector);
}
};
app.main();
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment