From 87376decd1ca693d16e779269f30fc094351c6e7 Mon Sep 17 00:00:00 2001 From: Garrett Bjerkhoel Date: Tue, 5 Dec 2023 00:51:50 -0800 Subject: [PATCH 1/2] Start tracking amazon products --- build.js | 2 +- chrome/manifest.json | 8 ++++- firefox/manifest.json | 12 +++++++ src/content_scripts/amazon.tsx | 61 ++++++++++++++++++++++++++++++++++ 4 files changed, 81 insertions(+), 2 deletions(-) create mode 100644 src/content_scripts/amazon.tsx diff --git a/build.js b/build.js index 15a1e98..53adbe3 100755 --- a/build.js +++ b/build.js @@ -60,7 +60,7 @@ const cssAsStringPlugin = { const config = { logLevel: 'info', - entryPoints: ['./src/background.ts', './src/content_scripts/content_script.tsx'], + entryPoints: ['./src/background.ts', './src/content_scripts/content_script.tsx', './src/content_scripts/amazon.tsx'], bundle: true, sourcemap: !isProduction, watch, diff --git a/chrome/manifest.json b/chrome/manifest.json index 3d64213..f4821d9 100644 --- a/chrome/manifest.json +++ b/chrome/manifest.json @@ -80,7 +80,13 @@ "content_scripts": [ { "js": ["content_scripts/content_script.js"], - "matches": [""], + "matches": ["*://*/*"], + "exclude_matches": ["*://*.amazon.com/*"], + "run_at": "document_start" + }, + { + "js": ["content_scripts/amazon.js"], + "matches": ["*://*.amazon.com/*"], "run_at": "document_start" } ] diff --git a/firefox/manifest.json b/firefox/manifest.json index 3b34b91..d3dea1f 100644 --- a/firefox/manifest.json +++ b/firefox/manifest.json @@ -25,10 +25,22 @@ "run_at": "document_start", "match_about_blank": false, "all_frames": false, + "exclude_matches": ["*://*.amazon.com/*"], "matches": [ "http://*/*", "https://*/*" ] + }, + { + "js": [ + "content_scripts/amazon.js" + ], + "run_at": "document_start", + "match_about_blank": false, + "all_frames": false, + "matches": [ + "*://*.amazon.com/*" + ] } ], "icons": { diff --git a/src/content_scripts/amazon.tsx b/src/content_scripts/amazon.tsx new file mode 100644 index 0000000..2866b42 --- /dev/null +++ b/src/content_scripts/amazon.tsx @@ -0,0 +1,61 @@ +import browser from 'webextension-polyfill' + +import {MessageAction} from '../@types/messages' +import {ObservableElement} from '../@types/observables' +import {insertIsInStockButton, removeIsInStockButton} from '../elements/isinstock-button' +import ExclusiveValidationRequestCache from '../utils/exclusive-validation-request-cache' +import {observeSelector} from '../utils/observers' +import {notFoundCallback} from '../utils/products' + +const validationRequests = new ExclusiveValidationRequestCache() + +// We're observing changes to the DOM to know when to insert the button. +// +// link[rel="canonical"] - The canonical link element. This is the link that points to the canonical URL of the page, which contains the URL. +// #ppd - The product details div. This is the div that contains the product title and price. +const {search, observe, disconnect} = observeSelector( + `link[rel="canonical"], #ppd`, + async (observedElements: ObservableElement[], containsProductCandidates: boolean) => { + if (/\/dp\//.test(window.location.href)) { + console.debug('observeSelector.callback: Product found in link', window.location.href) + validationRequests.fetchWithLock(window.location.href, productValidation => { + insertIsInStockButton({productValidation}) + }) + } else if (!containsProductCandidates) { + // Because we don't fire the MutationObserver twice on the same