-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcontentScript.js
More file actions
196 lines (165 loc) · 6.35 KB
/
Copy pathcontentScript.js
File metadata and controls
196 lines (165 loc) · 6.35 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
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
console.log("contentScript")
// chrome.runtime.onMessage.addListener((message, sender, sendResponse) => {
// console.log('Received message from background.js:', message);
// if (message.message === 'start') {
// highlighting = true;
// sendResponse('Message received');
// }
// });
// -----------------------This is the hover-highlighter--------------------------
// let highlightedElement = null;
// document.addEventListener('mouseover', (event) => {
// const target = event.target;
// if (highlightedElement !== target) {
// if (highlightedElement) {
// highlightedElement.style.backgroundColor = '';
// }
// target.style.backgroundColor = 'yellow';
// highlightedElement = target;
// }
// });
// document.addEventListener('mouseout', (event) => {
// if (highlightedElement) {
// highlightedElement.style.backgroundColor = '';
// highlightedElement = null;
// }
// });
// ---------------------------------------------------
let highlightedElements = [];
let copiedText = '';
let isHighlighting = false;
chrome.runtime.onMessage.addListener((request, sender, sendResponse) => {
if (request.message === 'startHighlighting') {
isHighlighting = true;
} else if (request.message === 'stopHighlighting') {
isHighlighting = false;
removeHighlighting();
}
});
function highlightElement(element) {
if (element) {
element.style.outline = '2px solid red';
element.style.backgroundColor = 'rgba(255, 0, 0, 0.5)';
highlightedElements.push(element);
}
}
function removeHighlighting() {
highlightedElements.forEach(element => {
element.style.outline = '';
element.style.backgroundColor = '';
});
highlightedElements = [];
copiedText = '';
}
function handleCopy(e) {
e.preventDefault();
let elementsToCopy = []; // Initialize elementsToCopy here
let emptyTextContents = 0;
let totalTextContents = 0;
if (highlightedElements.length > 0) {
highlightedElements.forEach(element => {
let attributes = Array.from(element.attributes).reduce((acc, attribute) => {
acc[attribute.name] = attribute.value + " ";
return acc;
}, {});
let textContent = element.innerText.trim();
totalTextContents++;
if (textContent !== '') {
elementsToCopy.push({
tagName: element.tagName,
attributes: attributes,
textContent: textContent + " "
});
} else {
emptyTextContents++;
}
});
}
copiedText = JSON.stringify({
elements: elementsToCopy,
ignoredEmptyTextContents: emptyTextContents,
totalTextContents: totalTextContents
}, null, 2); // Format JSON with 2 spaces for indentation
navigator.clipboard.writeText(copiedText);
}
function handleHover(e) {
if (!isHighlighting) return;
let target = e.target;
removeHighlighting();
let similarElements = Array.from(document.querySelectorAll('div, ul, li'));
let targetHtml = getEmptyElementHtml(target);
similarElements.forEach(element => {
if (getEmptyElementHtml(element) === targetHtml) {
highlightElement(element);
}
});
if (target.tagName.toLowerCase() === 'div') {
let ulElements = Array.from(target.querySelectorAll('ul'));
ulElements.forEach(ulElement => {
highlightElement(ulElement);
let liElements = Array.from(ulElement.querySelectorAll('li'));
liElements.forEach(liElement => {
highlightElement(liElement);
});
});
} else if (target.tagName.toLowerCase() === 'ul') {
let liElements = Array.from(target.querySelectorAll('li'));
liElements.forEach(liElement => {
highlightElement(liElement);
});
} else if (target.tagName.toLowerCase() === 'li') {
let ulElement = target.closest('ul');
if (ulElement) {
highlightElement(ulElement);
let liElements = Array.from(ulElement.querySelectorAll('li'));
liElements.forEach(liElement => {
highlightElement(liElement);
});
}
}
target.addEventListener('click', handleCopy);
}
function getEmptyElementHtml(element) {
let html = element.outerHTML;
let contentStart = html.indexOf('>') + 1;
let contentEnd = html.lastIndexOf('<');
return html.slice(0, contentStart) + html.slice(contentEnd);
}
function extractProductData(element) {
let productData = {};
let titleElement = element.querySelector('.prdct-desc-cntnr-name.hasRatings');
let nameElement = element.querySelector('.prdct-desc-cntnr-ttl');
if (titleElement) {
productData.productTitle = titleElement.getAttribute('title');
}
if (nameElement) productData.productName = nameElement.innerText;
let ratingCountElement = element.querySelector('.ratingCount');
if (ratingCountElement) productData.ratingCount = parseInt(ratingCountElement.innerText.replace('(', '').replace(')', ''));
let imgElement = element.querySelector('.p-card-img');
if (imgElement) productData.productImageUrl = imgElement.getAttribute('src');
let priceElement = element.querySelector('.prc-box-dscntd');
if (priceElement) productData.price = priceElement.innerText;
let badgesElement = element.querySelector('.badges-wrapper');
if (badgesElement) productData.badgesContent = badgesElement.innerText;
let ratingsElement = element.querySelector('.ratings');
if (ratingsElement) productData.ratingsContent = ratingsElement.innerText;
let linkElement = element.querySelector('a');
if (linkElement) productData.productLink = linkElement.getAttribute('href');
productData.dataId = element.getAttribute('data-id'); // Extract data-id attribute
return productData;
}
let productElements = document.querySelectorAll('[data-id]'); // Select all elements with a data-id attribute
let productsData = Array.from(productElements).map(extractProductData);
console.log(productElements)
console.log(productsData);
// Separate event listeners for 'div', 'ul', and 'li' elements
document.body.addEventListener('mouseover', function (e) {
if (isHighlighting && e.target.tagName.toLowerCase() === 'div') {
handleHover(e);
}
}, false);
document.body.addEventListener('mouseover', function (e) {
if (isHighlighting && (e.target.tagName.toLowerCase() === 'ul' || e.target.tagName.toLowerCase() === 'li')) {
handleHover(e);
}
}, false);