-
-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathcontent.js
More file actions
244 lines (211 loc) · 6.83 KB
/
Copy pathcontent.js
File metadata and controls
244 lines (211 loc) · 6.83 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
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
// NoAdsGPT - Content Script
// Removes sponsored ads and promotional content from ChatGPT interface.
// Works via CSS injection (blocker.css) + DOM observation for dynamically loaded ads.
(function() {
'use strict';
// Ad selectors targeting ChatGPT's known ad structure
// ChatGPT ads appear as "Sponsored" tinted boxes below responses
// They use bzrcdn.openai.com for ad assets and track via /bazaar/event
const AD_SELECTORS = [
// Sponsored content data attributes
'[data-testid*="sponsored"]',
'[data-testid*="ad-unit"]',
'[data-testid*="ad_unit"]',
'[data-testid*="advertiser"]',
// Sponsored class patterns
'[class*="sponsored"]',
'[class*="Sponsored"]',
// Ad unit type containers
'[class*="single_advertiser"]',
'[class*="multi_advertiser"]',
// Ad carousel cards
'[class*="ad-carousel"]',
'[class*="ad_carousel"]',
// Generic ad containers
'div[class*="adUnit"]',
'div[class*="ad-unit"]',
'div[class*="AdUnit"]',
'section[class*="sponsor"]',
'aside[class*="sponsor"]',
// Bazaar CDN ad images
'img[src*="bzrcdn.openai.com"]',
// CPC tracking links (ad click-throughs)
'a[href*="utm_source=chatgpt"][href*="utm_medium=cpc"]'
];
const COMBINED_SELECTOR = AD_SELECTORS.join(', ');
let blockedCount = 0;
let isEnabled = true;
// Load protection state
try {
chrome.storage.local.get(['protectionEnabled'], (result) => {
if (chrome.runtime.lastError) return;
isEnabled = result.protectionEnabled !== false;
if (isEnabled) {
blockAds();
startObserver();
}
});
} catch (e) {
// If storage fails, default to enabled
isEnabled = true;
blockAds();
startObserver();
}
// Listen for toggle changes from popup
try {
chrome.storage.onChanged.addListener((changes) => {
if (changes.protectionEnabled) {
isEnabled = changes.protectionEnabled.newValue !== false;
if (isEnabled) {
blockAds();
}
}
});
} catch (e) {
// Silently fail
}
// Main ad blocking function
function blockAds() {
if (!isEnabled) return 0;
let count = 0;
try {
const elements = document.querySelectorAll(COMBINED_SELECTOR);
elements.forEach(element => {
if (element.dataset.noadsgptHidden) return;
element.style.setProperty('display', 'none', 'important');
element.dataset.noadsgptHidden = 'true';
count++;
});
// Also check for elements containing "Sponsored" text that might be ad labels
// Only target small elements (likely labels, not content)
const walker = document.createTreeWalker(
document.body,
NodeFilter.SHOW_ELEMENT,
{
acceptNode: function(node) {
if (node.dataset && node.dataset.noadsgptChecked) {
return NodeFilter.FILTER_REJECT;
}
// Only check small elements that could be ad labels
const text = node.textContent.trim();
if (text === 'Sponsored' || text === 'Ad' || text === 'Sponsored ·') {
return NodeFilter.FILTER_ACCEPT;
}
return NodeFilter.FILTER_SKIP;
}
}
);
let node;
while (node = walker.nextNode()) {
node.dataset.noadsgptChecked = 'true';
// Walk up to find the ad container (usually 3-5 levels up)
let container = node;
for (let i = 0; i < 6; i++) {
if (!container.parentElement || container.parentElement === document.body) break;
container = container.parentElement;
// Check if this looks like an ad container (has link with CPC params or bzrcdn image)
const hasCpcLink = container.querySelector('a[href*="utm_medium=cpc"]');
const hasBzrImage = container.querySelector('img[src*="bzrcdn.openai.com"]');
if (hasCpcLink || hasBzrImage) {
if (!container.dataset.noadsgptHidden) {
container.style.setProperty('display', 'none', 'important');
container.dataset.noadsgptHidden = 'true';
count++;
}
break;
}
}
}
} catch (e) {
// Silently fail
}
// Update blocked count
if (count > 0) {
blockedCount += count;
updateStoredCount(count);
}
return count;
}
// Block network requests to ad tracking endpoints
function blockAdTracking() {
if (!isEnabled) return;
// Override fetch to block bazaar tracking calls
const originalFetch = window.fetch;
window.fetch = function(...args) {
const url = args[0]?.url || args[0] || '';
if (typeof url === 'string' && url.includes('/bazaar/event')) {
// Block ad tracking request silently
return Promise.resolve(new Response('{}', { status: 200 }));
}
return originalFetch.apply(this, args);
};
// Override XMLHttpRequest for bazaar tracking
const originalXHROpen = XMLHttpRequest.prototype.open;
XMLHttpRequest.prototype.open = function(method, url, ...rest) {
if (typeof url === 'string' && url.includes('/bazaar/event')) {
// Redirect to a no-op
this._noadsgptBlocked = true;
}
return originalXHROpen.call(this, method, url, ...rest);
};
const originalXHRSend = XMLHttpRequest.prototype.send;
XMLHttpRequest.prototype.send = function(...args) {
if (this._noadsgptBlocked) {
// Don't send the tracking request
return;
}
return originalXHRSend.apply(this, args);
};
}
// Update stored count
function updateStoredCount(newCount) {
try {
chrome.storage.local.get(['adsBlockedCount'], (result) => {
if (chrome.runtime.lastError) return;
const currentCount = result.adsBlockedCount || 0;
chrome.storage.local.set({ adsBlockedCount: currentCount + newCount });
});
} catch (e) {
// Silently fail
}
}
// MutationObserver for dynamically loaded ads
let observer = null;
function startObserver() {
if (observer) return;
observer = new MutationObserver((mutations) => {
if (!isEnabled) return;
let shouldCheck = false;
for (const mutation of mutations) {
if (mutation.addedNodes.length > 0) {
shouldCheck = true;
break;
}
}
if (shouldCheck) {
// Debounce: wait for DOM to settle
requestAnimationFrame(() => {
blockAds();
});
}
});
const target = document.body || document.documentElement;
if (target) {
observer.observe(target, {
childList: true,
subtree: true
});
}
}
// Initialize
function init() {
blockAds();
blockAdTracking();
startObserver();
}
if (document.readyState === 'loading') {
document.addEventListener('DOMContentLoaded', init);
} else {
init();
}
})();