I'm building an application that takes two parameters; the request URL and a CSS query selector. I'm having a hard time getting the request to look like: "http://localhost:5000/scrapeme/us-central1/scraperSelector?requestURL=https://www.google.com&selector=#hplogo". The request is not accepting the selector variable and returning not defined.
I'm not really sure what I'm doing wrong and I've tried different methods such as to request.body or creating an object and pass that down in the code. I've read the Google documentation and couldn't really find a good example of passing multiple parameters in a cloud function.
const admin = require('firebase-admin');
const functions = require('firebase-functions');
const puppeteer = require("puppeteer");
const chalk = require("chalk");
admin.initializeApp();
// for yellow console logging
const checking = chalk.bold.yellow;
// const uri = "http://localhost:5000/scrapeme/us-central1/scraperSelector";
// const appURL = "scrapeme.firebaseapp.com";
exports.scraperSelector = functions.runWith({ memory: '1GB' }).https.onRequest(async(request, response) => {
// initialize varialbe to request params
const requestURL = request.query.requestURL;
console.log("Evaluating " + requestURL);
let selector = request.query.selector;
console.log("Evaluating " + selector);
console.log("Evaluating " + request.originalUrl);
// Launch a browser
const browser = await puppeteer.launch({
headless: true,
args: ['--no-sandbox', '--disable-setuid-sandbox']
});
// Visit the page a get content
const page = await browser.newPage();
// Go to requested URL
await page.goto(requestURL, { waitUntil: 'networkidle0' });
console.log(checking("Evaluating " + requestURL));
// find the css selector
const content = await page.evaluate(() => {
console.log(JSON.stringify(selector));
let selectorCSS = document.querySelector(selector).innerText;
console.log(selectorCSS);
return selectorCSS;
},);
// Send the response
response.json(content);
});
// Example URL of how request should look
// http://localhost:5000/scrapeme/us-central1/scraperSelector?requestURL=https://www.google.com&selector=#hplogo
I expect the output to resolve to a JSON response. I'm trying to grab a single item from a page. { "result": "$18.41" }
However, I'm getting this output and error:
Evaluating https://www.google.com
Evaluating
Evaluating /scrapeme/us-central1/scraperSelector?requestURL=https://www.google.com&selector=
Evaluating https://www.google.com
! functions: Error: Evaluation failed: ReferenceError: selector is not defined at puppeteer_evaluation_script:2:36