Is it possible to use js variables in html while using Puppeteer?

  • Context: HTML/CSS 
  • Thread starter Thread starter Darkmisc
  • Start date Start date
  • Tags Tags
    Javascript
Click For Summary

Discussion Overview

The discussion revolves around the challenges of using JavaScript variables in HTML while employing Puppeteer, particularly in the context of retrieving and displaying sports scores. Participants explore various methods and potential issues related to integrating Puppeteer with HTML and JavaScript.

Discussion Character

  • Technical explanation
  • Debate/contested
  • Exploratory

Main Points Raised

  • One participant expresses difficulty in using a JavaScript variable in HTML after including Puppeteer, suggesting a workaround involving fetching data from a text file.
  • Another participant points out an error related to the use of 'require', indicating that the code is not compatible with the browser environment.
  • A participant questions the intended use of Puppeteer within a browser context, suggesting that it is not typically how Puppeteer is utilized.
  • One participant clarifies their goal of using Puppeteer to scrape sports scores and display them in a popup.
  • Another participant argues that Puppeteer is not designed for the intended use and suggests using the Fetch API or axios for data retrieval instead.
  • A note on copyright issues related to scraping data from sports websites is mentioned, highlighting potential legal concerns.

Areas of Agreement / Disagreement

Participants express differing views on the appropriate use of Puppeteer and the best methods for retrieving data. There is no consensus on how to directly use JavaScript variables in HTML while utilizing Puppeteer.

Contextual Notes

Participants note limitations regarding the compatibility of 'require' in the browser environment and the legal implications of scraping data from certain websites.

Darkmisc
Messages
222
Reaction score
31
TL;DR
I'd like to use a variable from my JS file in my html file. This works fine until I use Puppeteer in the JS file. Then, it won't work.
Hi everyone

I'm using Visual Studio Code and would like to use a variable from a JavaScript file in my html code.

This is easy enough to do until I try using Puppeteer in the JS file. If I add the following line to the JS code, I can no longer call my JS variable in the html file.

[CODE lang="javascript" title="Puppeteer"]const puppeteer = require("puppeteer")[/CODE]

I can work around this by saving the JS variable to a text file and then using the fetch method to get the data out of the file, but it's rather clunky.

[CODE lang="javascript" title="fetch"] fetch('blah.txt')
.then(response => response.text())
.then(data => {
var myVariable = data;
var paragraphElement = document.getElementById("myParagraph");
paragraphElement.innerHTML = "wowee" + myVariable;
})
.catch(error => console.error(error));
[/CODE]

Is there a more direct way to use JS variables in html while using Puppeteer?

Thanks
 
Technology news on Phys.org
The console says

[CODE lang="javascript" title="error"]index.js:2 Uncaught ReferenceError: require is not defined
at index.js:2:19
(anonymous) @ index.js:2[/CODE]

The code that it refers to is
[CODE lang="javascript" title="puppeteer"]const puppeteer = require("puppeteer")
[/CODE]

This is the part in sources that is underlined
1710716444381.png


I think flow might not be an issue here. My js variable is only there for testing. It's a fixed string. It doesn't depend on anything retrieved by Puppeteer.

EDIT: going to try fix the problem with Browserify
 
Last edited:
You are not using Puppeteer because your code is crashing because require doesn't exist in JavaScript.

You may be able to achieve what you want with Browserify, but this is not a way of working that is supported by current versions of Node. It is also not clear why you would want to run puppeteer within a browser, that is not normally how it is used.

What are you actually trying to achieve?
 
I wanted to use Puppeteer to get sports scores from a page and put them in a variable. I then wanted to use that variable to display updates for the scores in a popup.
 
Darkmisc said:
I wanted to use Puppeteer to get sports scores from a page and put them in a variable.
That is not what Puppeteer is for.

To scrape data from a page you need to retrieve the page either using the browser's built-in Fetch API (or the outdated XMLHttpRequest) or a module such as axios. Beware that [EDIT] this may be a breach of copyright of [/EDIT] the page you are targeting, and it may prevent it using the same-origin policy.
 
Last edited:
  • Like
Likes   Reactions: Darkmisc
I have added a note on copyright. Note that most publishers of sports results expressly prohibit this kind of thing see eg https://www.nfl.com/legal/terms/
Systematic retrieval of data or other content from the Services, whether to create or compile, directly or indirectly, a collection, compilation, database, or directory, is prohibited absent our express prior written consent.
 
  • Like
Likes   Reactions: Darkmisc

Similar threads

  • · Replies 1 ·
Replies
1
Views
3K
  • · Replies 13 ·
Replies
13
Views
2K
  • · Replies 3 ·
Replies
3
Views
2K
  • · Replies 3 ·
Replies
3
Views
2K
  • · Replies 8 ·
Replies
8
Views
3K
  • · Replies 8 ·
Replies
8
Views
6K
  • · Replies 2 ·
Replies
2
Views
3K
  • · Replies 1 ·
Replies
1
Views
2K
  • · Replies 2 ·
Replies
2
Views
3K
  • · Replies 16 ·
Replies
16
Views
2K