-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcontent.js
More file actions
167 lines (139 loc) · 5.97 KB
/
content.js
File metadata and controls
167 lines (139 loc) · 5.97 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
//Amazon Power Filter
let currentFilter = null;
// Send a message to confirm the content script is loaded
chrome.runtime.sendMessage({ action: "contentScriptLoaded" });
// Listen for messages from the popup
// In your existing message listener, update the getVisibleCount handler
chrome.runtime.onMessage.addListener((request, sender, sendResponse) => {
if (request.action === "applyFilter") {
filterElements(request.config); // config will now contain 'words' instead of keepWords/removeWords
sendResponse({ success: true });
} else if (request.action === "clearFilter") {
clearFilter();
sendResponse({ success: true });
} else if (request.action === "getVisibleCount") {
// Count only product items that are visible
const allItems = document.querySelectorAll('div[data-asin]');
const relatedSearchesContainer = findRelatedSearchesContainer();
const visibleCount = Array.from(allItems).filter(element => {
// Exclude related searches and pagination
if ((relatedSearchesContainer && element === relatedSearchesContainer) ||
element.querySelector('[cel_widget_id*="MAIN-PAGINATION"]') ||
element.closest('[cel_widget_id*="MAIN-PAGINATION"]')) {
return false;
}
// Only count elements that aren't hidden
return /*!element.classList.contains('amazon-filter-hidden') && */element.checkVisibility();
}).length;
console.log("Products found:", visibleCount);
sendResponse(visibleCount);
}
return true;
});
const observer = new MutationObserver(mutations => {
if (currentFilter) {
const hasAddedProducts = mutations.some(mutation =>
Array.from(mutation.addedNodes).some(node =>
node.nodeType === 1 && node.hasAttribute('data-asin')
)
);
if (hasAddedProducts) {
filterElements(currentFilter);
}
}
});
// Start observing with a more specific target
function startObserver() {
const searchResults = document.querySelector('.s-result-list');
if (searchResults) {
observer.observe(searchResults, {
childList: true,
subtree: true,
//characterData: true
});
} else {
// If search results container isn't ready yet, wait and try again
setTimeout(startObserver, 1000);
}
}
// Initial observer start
startObserver();
function findRelatedSearchesContainer() {
const relatedSearchesHeading = Array.from(document.querySelectorAll('h2')).find(h2 =>
h2.textContent.toLowerCase().includes('related searches')
);
if (relatedSearchesHeading) {
return relatedSearchesHeading.closest('div[data-asin]');
}
return null;
}
function filterElements(config = {
selector: 'div[data-asin]',
words: [],
removeSelectors: []
}) {
currentFilter = config; // Store current filter config
if (!document.getElementById('amazon-filter-style')) {
const style = document.createElement('style');
style.id = 'amazon-filter-style';
style.textContent = '.amazon-filter-hidden { display: none !important; }';
document.head.appendChild(style);
}
const relatedSearchesContainer = findRelatedSearchesContainer();
const elements = document.querySelectorAll(config.selector);
elements.forEach(element => {
if ((relatedSearchesContainer && element === relatedSearchesContainer) ||
element.querySelector('[cel_widget_id*="MAIN-PAGINATION"]') ||
element.closest('[cel_widget_id*="MAIN-PAGINATION"]')) {
return;
}
const text = element.textContent.toLowerCase();
// Split words into required, excluded, and optional
const requiredWords = config.words
.filter(word => word.startsWith('+'))
.map(word => word.slice(1).toLowerCase());
const excludedWords = config.words
.filter(word => word.startsWith('-'))
.map(word => word.slice(1).toLowerCase());
const optionalWords = config.words
.filter(word => !word.startsWith('+') && !word.startsWith('-'))
.map(word => word.toLowerCase());
// Element must contain ALL required words
const hasAllRequired = requiredWords.length === 0 ||
requiredWords.every(word => text.includes(word));
// Element must not contain ANY excluded words
const hasNoExcluded = excludedWords.length === 0 ||
!excludedWords.some(word => text.includes(word));
// If there are optional words, element must contain at least one
const hasOptional = optionalWords.length === 0 ||
optionalWords.some(word => text.includes(word));
// Show element only if it meets all conditions
if (hasAllRequired && hasNoExcluded && hasOptional) {
element.classList.remove('amazon-filter-hidden');
} else {
element.classList.add('amazon-filter-hidden');
}
});
// Rest of your removeSelectors code remains the same
if (config.removeSelectors && config.removeSelectors.length > 0) {
config.removeSelectors.forEach(selector => {
document.querySelectorAll(selector).forEach(element => {
if (!(relatedSearchesContainer && element === relatedSearchesContainer) &&
!element.querySelector('[cel_widget_id*="MAIN-PAGINATION"]') &&
!element.closest('[cel_widget_id*="MAIN-PAGINATION"]')) {
element.classList.add('amazon-filter-hidden');
}
});
});
}
}
function clearFilter() {
currentFilter = null; // Clear stored filter config
const styleElement = document.getElementById('amazon-filter-style');
if (styleElement) {
styleElement.remove();
}
document.querySelectorAll('.amazon-filter-hidden').forEach(element => {
element.classList.remove('amazon-filter-hidden');
});
}