From fb76408f682055fc8a03ccfc097f7d9a37f36969 Mon Sep 17 00:00:00 2001 From: Jon Oliver Date: Tue, 18 Nov 2025 14:05:52 -0500 Subject: [PATCH 1/2] feat: calculate emissions from page speed results --- package-lock.json | 10 ++++++++++ package.json | 6 +++--- src/core/CarbonCalculator.js | 21 +++++++++++++++++++++ 3 files changed, 34 insertions(+), 3 deletions(-) create mode 100644 src/core/CarbonCalculator.js diff --git a/package-lock.json b/package-lock.json index 93a477c..cccde6a 100644 --- a/package-lock.json +++ b/package-lock.json @@ -9,6 +9,7 @@ "version": "1.0.0", "license": "MIT", "dependencies": { + "@tgwf/co2": "^0.16.9", "archiver": "^5.3.1" }, "devDependencies": {}, @@ -16,6 +17,15 @@ "node": ">=14.0.0" } }, + "node_modules/@tgwf/co2": { + "version": "0.16.9", + "resolved": "https://registry.npmjs.org/@tgwf/co2/-/co2-0.16.9.tgz", + "integrity": "sha512-+nuT7HS5r22lbW4MdgSPHhirYBjHR6Q8sz5kX6dKdK+yIkRukLAGaH6L/toZwVEOuEeeIRfWYJuaJIoxEGDMXg==", + "license": "Apache-2.0", + "engines": { + "node": ">=14.0.0" + } + }, "node_modules/archiver": { "version": "5.3.2", "resolved": "https://registry.npmjs.org/archiver/-/archiver-5.3.2.tgz", diff --git a/package.json b/package.json index 19abcd7..442b8ac 100644 --- a/package.json +++ b/package.json @@ -22,7 +22,7 @@ "node": ">=14.0.0" }, "dependencies": { + "@tgwf/co2": "^0.16.9", "archiver": "^5.3.1" - }, - "devDependencies": {} -} \ No newline at end of file + } +} diff --git a/src/core/CarbonCalculator.js b/src/core/CarbonCalculator.js new file mode 100644 index 0000000..cf2ce74 --- /dev/null +++ b/src/core/CarbonCalculator.js @@ -0,0 +1,21 @@ +import { co2 } from "@tgwf/co2"; + +/** + * Calculates the carbon emissions for a web page based on the results from Google's PageSpeed Insights (Lighthouse). + * + * @param {Object} pageSpeedResults - The PageSpeed Insights (Lighthouse) result object. + * @returns {Object} An object containing: + * @property {number} emissionsOneByte - Estimated emissions CO2e (in grams) from the OneByte model. + * @property {number} emissionsSWD - Estimated emissions CO2e (in grams) from the Sustainable Web Design model. + * TODO: This function currently returns emissions for both the OneByte and SWD models. Not sure which one we ultimately want to use. + */ +export const calculateEmissionsFromPageSpeedResults = (pageSpeedResults) => { + const totalByteWeight = pageSpeedResults.lighthouseResult.audits['total-byte-weight'].numericValue; + + // Sustainable Web Design (SWD) model + // https://developers.thegreenwebfoundation.org/co2js/models/#using-the-sustainable-web-design-model-default-v0110 + const swd = new co2({ model: "swd", version: 4 }); + const emissionsPerByte = swd.perByte(totalByteWeight); + + return { bytesTransferred: totalByteWeight, totalCO2: emissionsPerByte }; +} From 17fbcdc37b66a2ec5939d59e4e444befb718896e Mon Sep 17 00:00:00 2001 From: Jon Oliver Date: Tue, 2 Dec 2025 13:41:35 -0500 Subject: [PATCH 2/2] feat: integrate carbon calculator with page speed results - adds esbuild to bundle carbon calculator dependencies - calls carbon calculator function from welcome panel with page speed results --- .gitignore | 3 + build.js | 31 ++- bundle.js | 34 +++ package-lock.json | 488 +++++++++++++++++++++++++++++++++- package.json | 3 + src/panels/welcome/welcome.js | 13 +- 6 files changed, 552 insertions(+), 20 deletions(-) create mode 100644 bundle.js diff --git a/.gitignore b/.gitignore index 572db8a..eb1b62a 100644 --- a/.gitignore +++ b/.gitignore @@ -6,3 +6,6 @@ build/ # Binary files .DS_Store + +# Bundled files +*.bundle.js diff --git a/build.js b/build.js index e007ced..3a48524 100644 --- a/build.js +++ b/build.js @@ -3,6 +3,7 @@ const fs = require('fs'); const path = require('path'); const archiver = require('archiver'); +const { bundleCarbonCalculator } = require('./bundle.js'); // Check if archiver is available, if not, provide instructions try { @@ -44,7 +45,7 @@ function copyCommonFiles(bundlePath) { const source = file; const chromeDest = path.join(chromeDir, file); const firefoxDest = path.join(firefoxDir, file); - + if (fs.lstatSync(source).isDirectory()) { // Copy directory recursively copyDirectoryRecursive(source, chromeDest); @@ -77,12 +78,12 @@ function copyDirectoryRecursive(src, dest) { if (!fs.existsSync(dest)) { fs.mkdirSync(dest, { recursive: true }); } - + const files = fs.readdirSync(src); files.forEach(file => { const srcPath = path.join(src, file); const destPath = path.join(dest, file); - + if (fs.lstatSync(srcPath).isDirectory()) { copyDirectoryRecursive(srcPath, destPath); } else { @@ -122,30 +123,30 @@ function createZips() { return new Promise((resolve, reject) => { const chromeZip = fs.createWriteStream(path.join(buildDir, 'carbon-visualizer-chrome.zip')); const firefoxZip = fs.createWriteStream(path.join(buildDir, 'carbon-visualizer-firefox.zip')); - + const chromeArchive = archiver('zip', { zlib: { level: 9 } }); const firefoxArchive = archiver('zip', { zlib: { level: 9 } }); - + chromeZip.on('close', () => { console.log('āœ… Chrome extension packaged: build/carbon-visualizer-chrome.zip'); }); - + firefoxZip.on('close', () => { console.log('āœ… Firefox extension packaged: build/carbon-visualizer-firefox.zip'); }); - + chromeArchive.on('error', reject); firefoxArchive.on('error', reject); - + chromeArchive.pipe(chromeZip); firefoxArchive.pipe(firefoxZip); - + chromeArchive.directory(chromeDir, false); firefoxArchive.directory(firefoxDir, false); - + chromeArchive.finalize(); firefoxArchive.finalize(); - + Promise.all([ new Promise(resolve => chromeArchive.on('end', resolve)), new Promise(resolve => firefoxArchive.on('end', resolve)) @@ -200,13 +201,15 @@ async function build() { console.log('šŸš€ Building Carbon Visualizer extension for Chrome and Firefox...'); const bundlePath = bundleCoreCss(); + await bundleCarbonCalculator(); + createDirectories(); copyCommonFiles(bundlePath); copyManifests(); patchFirefoxManifest(); - + await createZips(); - + console.log('\nšŸŽ‰ Build complete!'); console.log('\nšŸ“¦ Extension packages created:'); console.log(' Chrome: build/carbon-visualizer-chrome.zip'); @@ -214,7 +217,7 @@ async function build() { console.log('\nšŸ“‹ Installation instructions:'); console.log(' Chrome: Load unpacked from build/chrome/'); console.log(' Firefox: Load temporary add-on from build/firefox/'); - + } catch (error) { console.error('āŒ Build failed:', error); process.exit(1); diff --git a/bundle.js b/bundle.js new file mode 100644 index 0000000..be95285 --- /dev/null +++ b/bundle.js @@ -0,0 +1,34 @@ +#!/usr/bin/env node + +const esbuild = require('esbuild'); +const path = require('path'); + +async function bundleCarbonCalculator() { + try { + console.log('šŸ“¦ Bundling CarbonCalculator.js with dependencies...'); + + await esbuild.build({ + entryPoints: [path.join(__dirname, 'src/core/CarbonCalculator.js')], + bundle: true, + format: 'esm', + outfile: path.join(__dirname, 'src/core/CarbonCalculator.bundle.js'), + platform: 'browser', + target: 'es2020', + external: [], + minify: false, + sourcemap: false, + }); + + console.log('āœ… CarbonCalculator.js bundled successfully'); + } catch (error) { + console.error('āŒ Bundling failed:', error); + process.exit(1); + } +} + +if (require.main === module) { + bundleCarbonCalculator(); +} + +module.exports = { bundleCarbonCalculator }; + diff --git a/package-lock.json b/package-lock.json index cccde6a..11d226e 100644 --- a/package-lock.json +++ b/package-lock.json @@ -12,11 +12,455 @@ "@tgwf/co2": "^0.16.9", "archiver": "^5.3.1" }, - "devDependencies": {}, + "devDependencies": { + "esbuild": "^0.27.0" + }, "engines": { "node": ">=14.0.0" } }, + "node_modules/@esbuild/aix-ppc64": { + "version": "0.27.0", + "resolved": "https://registry.npmjs.org/@esbuild/aix-ppc64/-/aix-ppc64-0.27.0.tgz", + "integrity": "sha512-KuZrd2hRjz01y5JK9mEBSD3Vj3mbCvemhT466rSuJYeE/hjuBrHfjjcjMdTm/sz7au+++sdbJZJmuBwQLuw68A==", + "cpu": [ + "ppc64" + ], + "dev": true, + "license": "MIT", + "optional": true, + "os": [ + "aix" + ], + "engines": { + "node": ">=18" + } + }, + "node_modules/@esbuild/android-arm": { + "version": "0.27.0", + "resolved": "https://registry.npmjs.org/@esbuild/android-arm/-/android-arm-0.27.0.tgz", + "integrity": "sha512-j67aezrPNYWJEOHUNLPj9maeJte7uSMM6gMoxfPC9hOg8N02JuQi/T7ewumf4tNvJadFkvLZMlAq73b9uwdMyQ==", + "cpu": [ + "arm" + ], + "dev": true, + "license": "MIT", + "optional": true, + "os": [ + "android" + ], + "engines": { + "node": ">=18" + } + }, + "node_modules/@esbuild/android-arm64": { + "version": "0.27.0", + "resolved": "https://registry.npmjs.org/@esbuild/android-arm64/-/android-arm64-0.27.0.tgz", + "integrity": "sha512-CC3vt4+1xZrs97/PKDkl0yN7w8edvU2vZvAFGD16n9F0Cvniy5qvzRXjfO1l94efczkkQE6g1x0i73Qf5uthOQ==", + "cpu": [ + "arm64" + ], + "dev": true, + "license": "MIT", + "optional": true, + "os": [ + "android" + ], + "engines": { + "node": ">=18" + } + }, + "node_modules/@esbuild/android-x64": { + "version": "0.27.0", + "resolved": "https://registry.npmjs.org/@esbuild/android-x64/-/android-x64-0.27.0.tgz", + "integrity": "sha512-wurMkF1nmQajBO1+0CJmcN17U4BP6GqNSROP8t0X/Jiw2ltYGLHpEksp9MpoBqkrFR3kv2/te6Sha26k3+yZ9Q==", + "cpu": [ + "x64" + ], + "dev": true, + "license": "MIT", + "optional": true, + "os": [ + "android" + ], + "engines": { + "node": ">=18" + } + }, + "node_modules/@esbuild/darwin-arm64": { + "version": "0.27.0", + "resolved": "https://registry.npmjs.org/@esbuild/darwin-arm64/-/darwin-arm64-0.27.0.tgz", + "integrity": "sha512-uJOQKYCcHhg07DL7i8MzjvS2LaP7W7Pn/7uA0B5S1EnqAirJtbyw4yC5jQ5qcFjHK9l6o/MX9QisBg12kNkdHg==", + "cpu": [ + "arm64" + ], + "dev": true, + "license": "MIT", + "optional": true, + "os": [ + "darwin" + ], + "engines": { + "node": ">=18" + } + }, + "node_modules/@esbuild/darwin-x64": { + "version": "0.27.0", + "resolved": "https://registry.npmjs.org/@esbuild/darwin-x64/-/darwin-x64-0.27.0.tgz", + "integrity": "sha512-8mG6arH3yB/4ZXiEnXof5MK72dE6zM9cDvUcPtxhUZsDjESl9JipZYW60C3JGreKCEP+p8P/72r69m4AZGJd5g==", + "cpu": [ + "x64" + ], + "dev": true, + "license": "MIT", + "optional": true, + "os": [ + "darwin" + ], + "engines": { + "node": ">=18" + } + }, + "node_modules/@esbuild/freebsd-arm64": { + "version": "0.27.0", + "resolved": "https://registry.npmjs.org/@esbuild/freebsd-arm64/-/freebsd-arm64-0.27.0.tgz", + "integrity": "sha512-9FHtyO988CwNMMOE3YIeci+UV+x5Zy8fI2qHNpsEtSF83YPBmE8UWmfYAQg6Ux7Gsmd4FejZqnEUZCMGaNQHQw==", + "cpu": [ + "arm64" + ], + "dev": true, + "license": "MIT", + "optional": true, + "os": [ + "freebsd" + ], + "engines": { + "node": ">=18" + } + }, + "node_modules/@esbuild/freebsd-x64": { + "version": "0.27.0", + "resolved": "https://registry.npmjs.org/@esbuild/freebsd-x64/-/freebsd-x64-0.27.0.tgz", + "integrity": "sha512-zCMeMXI4HS/tXvJz8vWGexpZj2YVtRAihHLk1imZj4efx1BQzN76YFeKqlDr3bUWI26wHwLWPd3rwh6pe4EV7g==", + "cpu": [ + "x64" + ], + "dev": true, + "license": "MIT", + "optional": true, + "os": [ + "freebsd" + ], + "engines": { + "node": ">=18" + } + }, + "node_modules/@esbuild/linux-arm": { + "version": "0.27.0", + "resolved": "https://registry.npmjs.org/@esbuild/linux-arm/-/linux-arm-0.27.0.tgz", + "integrity": "sha512-t76XLQDpxgmq2cNXKTVEB7O7YMb42atj2Re2Haf45HkaUpjM2J0UuJZDuaGbPbamzZ7bawyGFUkodL+zcE+jvQ==", + "cpu": [ + "arm" + ], + "dev": true, + "license": "MIT", + "optional": true, + "os": [ + "linux" + ], + "engines": { + "node": ">=18" + } + }, + "node_modules/@esbuild/linux-arm64": { + "version": "0.27.0", + "resolved": "https://registry.npmjs.org/@esbuild/linux-arm64/-/linux-arm64-0.27.0.tgz", + "integrity": "sha512-AS18v0V+vZiLJyi/4LphvBE+OIX682Pu7ZYNsdUHyUKSoRwdnOsMf6FDekwoAFKej14WAkOef3zAORJgAtXnlQ==", + "cpu": [ + "arm64" + ], + "dev": true, + "license": "MIT", + "optional": true, + "os": [ + "linux" + ], + "engines": { + "node": ">=18" + } + }, + "node_modules/@esbuild/linux-ia32": { + "version": "0.27.0", + "resolved": "https://registry.npmjs.org/@esbuild/linux-ia32/-/linux-ia32-0.27.0.tgz", + "integrity": "sha512-Mz1jxqm/kfgKkc/KLHC5qIujMvnnarD9ra1cEcrs7qshTUSksPihGrWHVG5+osAIQ68577Zpww7SGapmzSt4Nw==", + "cpu": [ + "ia32" + ], + "dev": true, + "license": "MIT", + "optional": true, + "os": [ + "linux" + ], + "engines": { + "node": ">=18" + } + }, + "node_modules/@esbuild/linux-loong64": { + "version": "0.27.0", + "resolved": "https://registry.npmjs.org/@esbuild/linux-loong64/-/linux-loong64-0.27.0.tgz", + "integrity": "sha512-QbEREjdJeIreIAbdG2hLU1yXm1uu+LTdzoq1KCo4G4pFOLlvIspBm36QrQOar9LFduavoWX2msNFAAAY9j4BDg==", + "cpu": [ + "loong64" + ], + "dev": true, + "license": "MIT", + "optional": true, + "os": [ + "linux" + ], + "engines": { + "node": ">=18" + } + }, + "node_modules/@esbuild/linux-mips64el": { + "version": "0.27.0", + "resolved": "https://registry.npmjs.org/@esbuild/linux-mips64el/-/linux-mips64el-0.27.0.tgz", + "integrity": "sha512-sJz3zRNe4tO2wxvDpH/HYJilb6+2YJxo/ZNbVdtFiKDufzWq4JmKAiHy9iGoLjAV7r/W32VgaHGkk35cUXlNOg==", + "cpu": [ + "mips64el" + ], + "dev": true, + "license": "MIT", + "optional": true, + "os": [ + "linux" + ], + "engines": { + "node": ">=18" + } + }, + "node_modules/@esbuild/linux-ppc64": { + "version": "0.27.0", + "resolved": "https://registry.npmjs.org/@esbuild/linux-ppc64/-/linux-ppc64-0.27.0.tgz", + "integrity": "sha512-z9N10FBD0DCS2dmSABDBb5TLAyF1/ydVb+N4pi88T45efQ/w4ohr/F/QYCkxDPnkhkp6AIpIcQKQ8F0ANoA2JA==", + "cpu": [ + "ppc64" + ], + "dev": true, + "license": "MIT", + "optional": true, + "os": [ + "linux" + ], + "engines": { + "node": ">=18" + } + }, + "node_modules/@esbuild/linux-riscv64": { + "version": "0.27.0", + "resolved": "https://registry.npmjs.org/@esbuild/linux-riscv64/-/linux-riscv64-0.27.0.tgz", + "integrity": "sha512-pQdyAIZ0BWIC5GyvVFn5awDiO14TkT/19FTmFcPdDec94KJ1uZcmFs21Fo8auMXzD4Tt+diXu1LW1gHus9fhFQ==", + "cpu": [ + "riscv64" + ], + "dev": true, + "license": "MIT", + "optional": true, + "os": [ + "linux" + ], + "engines": { + "node": ">=18" + } + }, + "node_modules/@esbuild/linux-s390x": { + "version": "0.27.0", + "resolved": "https://registry.npmjs.org/@esbuild/linux-s390x/-/linux-s390x-0.27.0.tgz", + "integrity": "sha512-hPlRWR4eIDDEci953RI1BLZitgi5uqcsjKMxwYfmi4LcwyWo2IcRP+lThVnKjNtk90pLS8nKdroXYOqW+QQH+w==", + "cpu": [ + "s390x" + ], + "dev": true, + "license": "MIT", + "optional": true, + "os": [ + "linux" + ], + "engines": { + "node": ">=18" + } + }, + "node_modules/@esbuild/linux-x64": { + "version": "0.27.0", + "resolved": "https://registry.npmjs.org/@esbuild/linux-x64/-/linux-x64-0.27.0.tgz", + "integrity": "sha512-1hBWx4OUJE2cab++aVZ7pObD6s+DK4mPGpemtnAORBvb5l/g5xFGk0vc0PjSkrDs0XaXj9yyob3d14XqvnQ4gw==", + "cpu": [ + "x64" + ], + "dev": true, + "license": "MIT", + "optional": true, + "os": [ + "linux" + ], + "engines": { + "node": ">=18" + } + }, + "node_modules/@esbuild/netbsd-arm64": { + "version": "0.27.0", + "resolved": "https://registry.npmjs.org/@esbuild/netbsd-arm64/-/netbsd-arm64-0.27.0.tgz", + "integrity": "sha512-6m0sfQfxfQfy1qRuecMkJlf1cIzTOgyaeXaiVaaki8/v+WB+U4hc6ik15ZW6TAllRlg/WuQXxWj1jx6C+dfy3w==", + "cpu": [ + "arm64" + ], + "dev": true, + "license": "MIT", + "optional": true, + "os": [ + "netbsd" + ], + "engines": { + "node": ">=18" + } + }, + "node_modules/@esbuild/netbsd-x64": { + "version": "0.27.0", + "resolved": "https://registry.npmjs.org/@esbuild/netbsd-x64/-/netbsd-x64-0.27.0.tgz", + "integrity": "sha512-xbbOdfn06FtcJ9d0ShxxvSn2iUsGd/lgPIO2V3VZIPDbEaIj1/3nBBe1AwuEZKXVXkMmpr6LUAgMkLD/4D2PPA==", + "cpu": [ + "x64" + ], + "dev": true, + "license": "MIT", + "optional": true, + "os": [ + "netbsd" + ], + "engines": { + "node": ">=18" + } + }, + "node_modules/@esbuild/openbsd-arm64": { + "version": "0.27.0", + "resolved": "https://registry.npmjs.org/@esbuild/openbsd-arm64/-/openbsd-arm64-0.27.0.tgz", + "integrity": "sha512-fWgqR8uNbCQ/GGv0yhzttj6sU/9Z5/Sv/VGU3F5OuXK6J6SlriONKrQ7tNlwBrJZXRYk5jUhuWvF7GYzGguBZQ==", + "cpu": [ + "arm64" + ], + "dev": true, + "license": "MIT", + "optional": true, + "os": [ + "openbsd" + ], + "engines": { + "node": ">=18" + } + }, + "node_modules/@esbuild/openbsd-x64": { + "version": "0.27.0", + "resolved": "https://registry.npmjs.org/@esbuild/openbsd-x64/-/openbsd-x64-0.27.0.tgz", + "integrity": "sha512-aCwlRdSNMNxkGGqQajMUza6uXzR/U0dIl1QmLjPtRbLOx3Gy3otfFu/VjATy4yQzo9yFDGTxYDo1FfAD9oRD2A==", + "cpu": [ + "x64" + ], + "dev": true, + "license": "MIT", + "optional": true, + "os": [ + "openbsd" + ], + "engines": { + "node": ">=18" + } + }, + "node_modules/@esbuild/openharmony-arm64": { + "version": "0.27.0", + "resolved": "https://registry.npmjs.org/@esbuild/openharmony-arm64/-/openharmony-arm64-0.27.0.tgz", + "integrity": "sha512-nyvsBccxNAsNYz2jVFYwEGuRRomqZ149A39SHWk4hV0jWxKM0hjBPm3AmdxcbHiFLbBSwG6SbpIcUbXjgyECfA==", + "cpu": [ + "arm64" + ], + "dev": true, + "license": "MIT", + "optional": true, + "os": [ + "openharmony" + ], + "engines": { + "node": ">=18" + } + }, + "node_modules/@esbuild/sunos-x64": { + "version": "0.27.0", + "resolved": "https://registry.npmjs.org/@esbuild/sunos-x64/-/sunos-x64-0.27.0.tgz", + "integrity": "sha512-Q1KY1iJafM+UX6CFEL+F4HRTgygmEW568YMqDA5UV97AuZSm21b7SXIrRJDwXWPzr8MGr75fUZPV67FdtMHlHA==", + "cpu": [ + "x64" + ], + "dev": true, + "license": "MIT", + "optional": true, + "os": [ + "sunos" + ], + "engines": { + "node": ">=18" + } + }, + "node_modules/@esbuild/win32-arm64": { + "version": "0.27.0", + "resolved": "https://registry.npmjs.org/@esbuild/win32-arm64/-/win32-arm64-0.27.0.tgz", + "integrity": "sha512-W1eyGNi6d+8kOmZIwi/EDjrL9nxQIQ0MiGqe/AWc6+IaHloxHSGoeRgDRKHFISThLmsewZ5nHFvGFWdBYlgKPg==", + "cpu": [ + "arm64" + ], + "dev": true, + "license": "MIT", + "optional": true, + "os": [ + "win32" + ], + "engines": { + "node": ">=18" + } + }, + "node_modules/@esbuild/win32-ia32": { + "version": "0.27.0", + "resolved": "https://registry.npmjs.org/@esbuild/win32-ia32/-/win32-ia32-0.27.0.tgz", + "integrity": "sha512-30z1aKL9h22kQhilnYkORFYt+3wp7yZsHWus+wSKAJR8JtdfI76LJ4SBdMsCopTR3z/ORqVu5L1vtnHZWVj4cQ==", + "cpu": [ + "ia32" + ], + "dev": true, + "license": "MIT", + "optional": true, + "os": [ + "win32" + ], + "engines": { + "node": ">=18" + } + }, + "node_modules/@esbuild/win32-x64": { + "version": "0.27.0", + "resolved": "https://registry.npmjs.org/@esbuild/win32-x64/-/win32-x64-0.27.0.tgz", + "integrity": "sha512-aIitBcjQeyOhMTImhLZmtxfdOcuNRpwlPNmlFKPcHQYPhEssw75Cl1TSXJXpMkzaua9FUetx/4OQKq7eJul5Cg==", + "cpu": [ + "x64" + ], + "dev": true, + "license": "MIT", + "optional": true, + "os": [ + "win32" + ], + "engines": { + "node": ">=18" + } + }, "node_modules/@tgwf/co2": { "version": "0.16.9", "resolved": "https://registry.npmjs.org/@tgwf/co2/-/co2-0.16.9.tgz", @@ -241,6 +685,48 @@ "once": "^1.4.0" } }, + "node_modules/esbuild": { + "version": "0.27.0", + "resolved": "https://registry.npmjs.org/esbuild/-/esbuild-0.27.0.tgz", + "integrity": "sha512-jd0f4NHbD6cALCyGElNpGAOtWxSq46l9X/sWB0Nzd5er4Kz2YTm+Vl0qKFT9KUJvD8+fiO8AvoHhFvEatfVixA==", + "dev": true, + "hasInstallScript": true, + "license": "MIT", + "bin": { + "esbuild": "bin/esbuild" + }, + "engines": { + "node": ">=18" + }, + "optionalDependencies": { + "@esbuild/aix-ppc64": "0.27.0", + "@esbuild/android-arm": "0.27.0", + "@esbuild/android-arm64": "0.27.0", + "@esbuild/android-x64": "0.27.0", + "@esbuild/darwin-arm64": "0.27.0", + "@esbuild/darwin-x64": "0.27.0", + "@esbuild/freebsd-arm64": "0.27.0", + "@esbuild/freebsd-x64": "0.27.0", + "@esbuild/linux-arm": "0.27.0", + "@esbuild/linux-arm64": "0.27.0", + "@esbuild/linux-ia32": "0.27.0", + "@esbuild/linux-loong64": "0.27.0", + "@esbuild/linux-mips64el": "0.27.0", + "@esbuild/linux-ppc64": "0.27.0", + "@esbuild/linux-riscv64": "0.27.0", + "@esbuild/linux-s390x": "0.27.0", + "@esbuild/linux-x64": "0.27.0", + "@esbuild/netbsd-arm64": "0.27.0", + "@esbuild/netbsd-x64": "0.27.0", + "@esbuild/openbsd-arm64": "0.27.0", + "@esbuild/openbsd-x64": "0.27.0", + "@esbuild/openharmony-arm64": "0.27.0", + "@esbuild/sunos-x64": "0.27.0", + "@esbuild/win32-arm64": "0.27.0", + "@esbuild/win32-ia32": "0.27.0", + "@esbuild/win32-x64": "0.27.0" + } + }, "node_modules/fs-constants": { "version": "1.0.0", "resolved": "https://registry.npmjs.org/fs-constants/-/fs-constants-1.0.0.tgz", diff --git a/package.json b/package.json index 442b8ac..a9d1c43 100644 --- a/package.json +++ b/package.json @@ -24,5 +24,8 @@ "dependencies": { "@tgwf/co2": "^0.16.9", "archiver": "^5.3.1" + }, + "devDependencies": { + "esbuild": "^0.27.0" } } diff --git a/src/panels/welcome/welcome.js b/src/panels/welcome/welcome.js index 0ca4d25..f5e639c 100644 --- a/src/panels/welcome/welcome.js +++ b/src/panels/welcome/welcome.js @@ -1,19 +1,20 @@ // Welcome panel JavaScript -import { extensionManager } from "../../core/ExtensionManager.js"; +import { extensionManager } from "../../core/ExtensionManager.js"; import { makePageSpeedAPIRequest } from "../../core/PageSpeedService.js"; +import { calculateEmissionsFromPageSpeedResults } from "../../core/CarbonCalculator.bundle.js"; export function initializePanel(panelType, data) { // Get the container element const container = data.container; - + if (!container) { return; } - + // Get analyze button from within the container. const analyzeBtn = container.querySelector('#analyze-page-btn'); const analyzeErrorMessage = container.querySelector('#analyze-page-error'); - + // Check if button exists before doing more with it. if (!analyzeBtn) { return; @@ -21,7 +22,7 @@ export function initializePanel(panelType, data) { // Store original/default button text. const analyzeBtnText = analyzeBtn.textContent; - + // Event listener for analyze button analyzeBtn.addEventListener('click', async () => { // For now, just show a simple message within the button. @@ -45,6 +46,8 @@ export function initializePanel(panelType, data) { analyzeBtn.textContent = analyzeBtnText; analyzeBtn.disabled = false; } else { + const { bytesTransferred, totalCO2 } = calculateEmissionsFromPageSpeedResults(pageSpeedResults); + console.log('Emissions Calculation Results:', { bytesTransferred, totalCO2 }); // Success. Open results panel. await extensionManager.openPanel('results'); }