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

  • HTML/CSS
  • Thread starter Darkmisc
  • Start date
  • Tags
    Javascript
  • #1
Darkmisc
220
31
TL;DR Summary
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.

Puppeteer:
const puppeteer = require("puppeteer")

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.

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));

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

Thanks
 
Technology news on Phys.org
  • #2
The console says

error:
index.js:2 Uncaught ReferenceError: require is not defined
    at index.js:2:19
(anonymous) @ index.js:2

The code that it refers to is
puppeteer:
const puppeteer = require("puppeteer")

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:
  • #3
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?
 
  • #4
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.
 
  • #5
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 Darkmisc
  • #6
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 Darkmisc

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

1. Can I access JavaScript variables in my HTML when using Puppeteer?

Yes, you can access JavaScript variables in your HTML when using Puppeteer. You can inject JavaScript into the page context using the `page.evaluate()` method, which allows you to execute JavaScript code in the context of the page and manipulate the DOM accordingly.

2. How do I pass variables from Node.js to the HTML page in Puppeteer?

You can pass variables from Node.js to the HTML page by using the `page.evaluate()` function. This function takes a function as an argument, and you can pass variables as parameters to that function, allowing you to use them within the page context.

3. Can I modify the HTML content with JavaScript variables using Puppeteer?

Yes, you can modify the HTML content using JavaScript variables. By using `page.evaluate()`, you can manipulate the DOM elements directly, such as changing text content or attributes based on the JavaScript variables you passed.

4. Is it possible to use Puppeteer to scrape data that includes JavaScript variables?

Yes, Puppeteer can be used to scrape data that includes JavaScript variables. Since Puppeteer operates a headless browser, it can execute JavaScript on the page, allowing you to extract dynamically generated content that relies on JavaScript variables.

5. What are some common pitfalls when using JavaScript variables in HTML with Puppeteer?

Some common pitfalls include scope issues, where variables defined outside of the `page.evaluate()` function are not accessible inside it. Additionally, timing issues may arise if the page has not fully loaded or if JavaScript execution is asynchronous, which can lead to unexpected results. Always ensure that the page is fully loaded before attempting to access or manipulate elements.

Similar threads

Replies
13
Views
2K
Replies
3
Views
2K
Replies
8
Views
6K
Replies
6
Views
6K
Replies
2
Views
4K
Back
Top