Skip to content

Instantly share code, notes, and snippets.

@drdrsh
Last active January 14, 2019 05:01
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save drdrsh/9c69fa3ba32dde4adbea6d59305b9314 to your computer and use it in GitHub Desktop.
Save drdrsh/9c69fa3ba32dde4adbea6d59305b9314 to your computer and use it in GitHub Desktop.
'use strict';
const Promise = require("bluebird");
const records = [{
'title': null,
'lat' : 9.1111,
'lng' : 5.1111,
'desc' : 'A record missing a title'
},{
'title': "Not missing data",
'lat' : 9.1111,
'lng' : 5.1111,
'desc' : 'Some description'
},{
'title': null,
'lat' : 9.1111,
'lng' : 5.1111,
'desc' : 'Another record missing a title with unicode chars (هذا نص عربي)'
}];
async function getUserInput(missing_record) {
const puppeteer = require('puppeteer');
return new Promise(async (resolve, reject) => {
//This message we will show the user
const prompt_msg = `Missing data: ${missing_record.desc}, please input value \n`;
//Construct a URL using longitude and latitude values from the record
const url = `https://www.google.com/maps/@?api=1&map_action=map&center=${missing_record.lat},${missing_record.lng}&zoom=10`;
//Create a browser object, we want the user to interact so we set headless to be false
const browser = await puppeteer.launch({'headless': false});
//Create a new page (think new tab)
const page = await browser.newPage();
//Goto Google maps
await page.goto(url);
const result = await page.evaluate((msg) => {
/*
This function will run within our puppeteer browser window, and if we return a promise,
page.evaluate() will wait for it to resolve, so we return a promise that resolves
when the user responds
*/
return new Promise((resolve, reject) => {
resolve(prompt(msg));
});
}, prompt_msg);
//Close our browser
await browser.close();
//Return user input to the calling method
resolve(result);
});
}
async function processData() {
for(const record of records) {
if(!record.title) {
console.log("Prompting user for response");
record.title = await getUserInput(record);
console.log("Got user response");
}
console.log(`Record title is ${record.title}`);
}
}
processData();
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment