-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
g4ze
committed
Apr 14, 2024
1 parent
6eb69a8
commit 9559492
Showing
4 changed files
with
983 additions
and
21 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,37 @@ | ||
import puppeteer from 'puppeteer'; | ||
|
||
export async function automateCarbonCalculation(url: string): Promise<void> { | ||
// Launch a new headless browser | ||
const browser = await puppeteer.launch(); | ||
|
||
// Create a new page | ||
const page = await browser.newPage(); | ||
|
||
// Navigate to the website | ||
await page.goto('https://www.websitecarbon.com/'); | ||
|
||
// Wait for the input field to appear and set the given URL | ||
await page.waitForSelector('input[name="wgd-cc-url"]'); | ||
await page.type('input[name="wgd-cc-url"]', url); | ||
|
||
// Click the "Calculate" button | ||
await page.click('button[type="submit"]'); | ||
|
||
// Wait for the results to appear | ||
await page.waitForSelector('.results'); | ||
|
||
// Capture the results as text | ||
const results = await page.evaluate(() => { | ||
const resultsElement = document.querySelector('.results'); | ||
return resultsElement ? resultsElement.textContent : ''; | ||
}); | ||
|
||
// Print the results | ||
console.log('Carbon calculation results:'); | ||
console.log(results); | ||
|
||
// Close the browser | ||
await browser.close(); | ||
} | ||
|
||
// Call the function with the desired URL to calculate |
Oops, something went wrong.