Skip to content

Commit

Permalink
updates
Browse files Browse the repository at this point in the history
  • Loading branch information
g4ze committed Apr 14, 2024
1 parent 6eb69a8 commit 9559492
Show file tree
Hide file tree
Showing 4 changed files with 983 additions and 21 deletions.
1 change: 1 addition & 0 deletions app/entry/page.tsx
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
"use client";
import { automateCarbonCalculation } from '@/lib/calc';
import React, { useState } from 'react';

const CarbonFootprintForm = () => {
Expand Down
37 changes: 37 additions & 0 deletions lib/calc.ts
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
Loading

0 comments on commit 9559492

Please sign in to comment.