-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcontent.js
106 lines (90 loc) · 3.29 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
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
(function() {
'use strict';
// Keywords indicating sponsored content in different languages
const SPONSORED_KEYWORDS = [
'sponsored',
'patrocinado',
'sponsorisé',
'gesponsert',
'スポンサー',
'赞助'
];
// Specific sponsored content selectors
const SPONSORED_SELECTORS = [
'[data-component-type="sp-sponsored-result"]',
'.AdHolder',
'.sp_desktop_sponsored_label',
'[data-cel-widget*="adplacements"]',
'[class*="_adPlacements"]',
'.celwidget .s-sponsored-info-icon'
];
// Check if element contains sponsored text
function containsSponsoredText(element) {
const text = element.textContent.toLowerCase().trim();
return SPONSORED_KEYWORDS.some(keyword => text.includes(keyword));
}
// Apply dimming effect to element
function dimElement(element) {
if (element.classList.contains('sponsored-content-dimmed')) {
return; // Already dimmed
}
// Ensure relative positioning
element.style.position = 'relative';
// Add dimming class
element.classList.add('sponsored-content-dimmed');
// Add sponsored badge
const badge = document.createElement('div');
badge.className = 'sponsored-badge';
badge.textContent = 'Sponsored';
element.appendChild(badge);
// Make content interactive on hover
element.style.cursor = 'pointer';
}
// Main function to process elements
function processSponsoredContent() {
// Process by specific selectors
SPONSORED_SELECTORS.forEach(selector => {
document.querySelectorAll(selector).forEach(element => {
const productCard = element.closest('[data-asin]') ||
element.closest('[data-component-type="s-search-result"]') ||
element;
if (productCard) {
dimElement(productCard);
}
});
});
// Process by text content
document.querySelectorAll('span').forEach(span => {
if (containsSponsoredText(span)) {
const productCard = span.closest('[data-asin]') ||
span.closest('[data-component-type="s-search-result"]');
if (productCard) {
dimElement(productCard);
}
}
});
}
// Create and configure mutation observer
const observer = new MutationObserver((mutations) => {
let shouldProcess = false;
mutations.forEach(mutation => {
if (mutation.addedNodes.length > 0) {
shouldProcess = true;
}
});
if (shouldProcess) {
setTimeout(processSponsoredContent, 100);
}
});
// Start observer
observer.observe(document.body, {
childList: true,
subtree: true
});
// Initial processing
setTimeout(processSponsoredContent, 1000);
// Run on load events
window.addEventListener('load', processSponsoredContent);
window.addEventListener('urlchange', processSponsoredContent);
console.log('Amazon Sponsored Content Dimmer - Loaded Successfully');
})();