-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcontent.js
41 lines (34 loc) · 1.24 KB
/
content.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
function extractProductInfo() {
const title = document.querySelector('.product-detail-info__header-name');
const productName = title ? title.innerText : null;
if (!productName) {
console.log('Not a product page.');
return;
}
const price = document.querySelector('.price__amount');
const productPrice = price ? price.innerText : 'Price not found';
const image = document.querySelector(`.media-image__image, .media__wrapper--media[alt="${productName}"]`);
const imageUrl = image ? image.src : 'Image not found';
const productInfo = {
name: productName,
price: productPrice,
imageUrl: imageUrl
};
chrome.storage.local.set({ productInfo }, () => {
if (chrome.runtime.lastError) {
console.error('Error in product info:', chrome.runtime.lastError);
} else {
console.log('Product : \n', productInfo.name, '\n', productInfo.price, '\n', productInfo.imageUrl);
}
});
}
let lastUrl = location.href;
function detectUrlChange() {
const currentUrl = location.href;
if (currentUrl !== lastUrl) {
lastUrl = currentUrl;
window.location.reload();
}
}
extractProductInfo();
setInterval(detectUrlChange, 1000);