Repository navigation
Expand file tree
/
Copy pathcontent.js
More file actions
65 lines (52 loc) · 2.4 KB
/
Copy pathcontent.js
File metadata and controls
65 lines (52 loc) · 2.4 KB
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
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
console.log("Content script is running");
url=window.location.href;
if (url.includes("amazon.com")||url.includes("amazon.in")){
url=url.replace(/\/dp\//g, "/product-reviews/");
}
else if(url.includes("flipkart.com")){
url=url.replace(/\/p\//g, "/product-reviews/");
}
// Send a message to the background script to fetch the reviews from the current page
chrome.runtime.sendMessage({ action: "fetchReviews", url: url }, (response) => {
if (response && response.reviews) {
const parser = new DOMParser();
const doc = parser.parseFromString(response.reviews, "text/html");
// Define selectors for each website
let reviewSelector;
let productTitle;
let featureSelector;
if (url.includes("amazon.in")) {
// Amazon.in selectors
reviewSelector = 'span[data-hook="review-body"]';
productTitle = document.querySelector("#productTitle")?.textContent.trim(); // Optional chaining for safety
featureSelector = '#featurebullets_feature_div #feature-bullets .a-unordered-list.a-vertical > li.a-spacing-mini > span.a-list-item';
} else if (url.includes("amazon.com")) {
// Amazon.com selectors (similar to Amazon.in)
reviewSelector = 'span[data-hook="review-body"]';
productTitle = document.querySelector("#productTitle")?.textContent.trim();
featureSelector = '#feature-bullets .a-unordered-list.a-vertical > li.a-spacing-mini > span.a-list-item';
} else if (url.includes("flipkart.com")) {
// Flipkart selectors
reviewSelector = '.ZmyHeo > div > div';
productTitle = document.querySelector(".VU-ZEz")?.textContent.trim();
featureSelector = '.xFVion > ul > li._7eSDEz'; // Define if Flipkart has a feature list selector
}
// Fetch and process reviews
const reviewContents = Array.from(doc.querySelectorAll(reviewSelector), element =>
element.textContent.trim().replace(/\n/g, ' ')
);
// Fetch and process feature list (only if featureSelector is defined)
let featureContents = [];
if (featureSelector) {
featureContents = Array.from(document.querySelectorAll(featureSelector), element =>
element.textContent.trim().replace(/\n/g, ' ')
);
}
// Output the results
// Send the processed reviews to the background script
chrome.runtime.sendMessage({ type: 'data', data: reviewContents, title: productTitle,productinfo:featureContents }, (response) => {
});
} else {
console.log("No response received or no reviews found.");
}
});