-
-
Notifications
You must be signed in to change notification settings - Fork 208
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Is it possible to load and render files? #1120
Comments
Hi @grandemestre! 🙂 You would do something like this: import { Window } from 'happy-dom';
async function main() {
const url = 'https://google.com';
const window = new Window({ url });
const document = window.document;
const response = await window.fetch(url);
const html = await response.text();
document.write(html);
// Wait for async tasks such as scripts, styles, fetches and timers to complete
await window.happyDOM.whenAsyncComplete();
// Output HTML of page
console.log(document.documentElement.outerHTML);
}
main(); I can also mention that I'm working right now on improving this. After the new update it will look something like this: import { Browser } from 'happy-dom';
async function main() {
const browser = new Browser();
const page = browser.newPage();
await page.goto('https://github.com');
await page.whenComplete();
}
main();
// Do something with the result |
With a file: import { Window } from 'happy-dom';
import FS from 'fs';
const url = 'https://google.com';
const window = new Window({ url });
const document = window.document;
const html = (await FS.promises.readFile('index.html')).toString();
document.write(html);
// Wait for async tasks such as scripts, styles, fetches and timers to complete
await window.happyDOM.whenAsyncComplete();
// Do something with the result
const field = document.querySelector('.field');
console.log(field.textContent); |
I don't know why but neither of the two codes works for me. It doesn't return any errors, it just doesn't do anything |
@grandemestre there was a problem in the example, but I have fixed it now. I also changed the URL to https://google.com as https://github.com is using a function that is not supported in Happy DOM yet. Here is a working example: |
Thanks a lot for the help. Now it worked correctly. Out of curiosity, what functionality is not yet supported? |
Sorry, I forgot to answer this and I don't remember anymore. |
There is a new way now using the Happy DOM Browser API: I will close this ticket now. |
Hello, I have a question, I don't know if the resource is missing or I don't know how to use it. My goal is to scrape a dynamic page. I used jsdom but I found this project better because it appears to be compatible with fetch.
I wanted to know how to load an .html file and render it including javascripts. The equivalent of this in jsdom:
index.html
The text was updated successfully, but these errors were encountered: