-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathinjector.js
More file actions
158 lines (141 loc) · 4.81 KB
/
Copy pathinjector.js
File metadata and controls
158 lines (141 loc) · 4.81 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
(function () {
const BLOCKED_AD_KEYS = [
'adPlacements',
'adSlots',
'adBreakParams',
'playerAds',
'adBreakHeartbeatParams',
'instreamAdBreakConfig'
];
const AD_URL_PATTERNS = [
'/pagead/',
'/ptracking',
'/api/stats/ads',
'/api/stats/atr',
'/get_midroll_info',
'googlesyndication.com',
'doubleclick.net',
'googleadservices.com',
'adsserver.yt'
];
function isActive() {
return document.documentElement.dataset.skipadActive === '1';
}
function stripAdsFromObject(obj) {
if (!obj || typeof obj !== 'object') return obj;
for (const key of BLOCKED_AD_KEYS) {
if (key in obj) {
delete obj[key];
}
}
return obj;
}
function isAdUrl(url) {
if (!url) return false;
const s = typeof url === 'string' ? url : url.toString();
return AD_URL_PATTERNS.some(p => s.includes(p));
}
const originalFetch = window.fetch;
window.fetch = function (input, init) {
if (!isActive()) return originalFetch.apply(this, arguments);
const url = typeof input === 'string' ? input : (input && input.url ? input.url : (input && input.href ? input.href : ''));
if (isAdUrl(url)) {
return Promise.resolve(new Response('{}', {
status: 200,
statusText: 'OK',
headers: { 'Content-Type': 'application/json' }
}));
}
return originalFetch.apply(this, arguments);
};
const originalOpen = XMLHttpRequest.prototype.open;
const originalSend = XMLHttpRequest.prototype.send;
XMLHttpRequest.prototype.open = function (method, url) {
this._skipad_url = typeof url === 'string' ? url : url.toString();
this._skipad_blocked = isActive() && isAdUrl(this._skipad_url);
return originalOpen.apply(this, arguments);
};
XMLHttpRequest.prototype.send = function () {
if (this._skipad_blocked) {
Object.defineProperty(this, 'readyState', { value: 4, configurable: true });
Object.defineProperty(this, 'status', { value: 200, configurable: true });
Object.defineProperty(this, 'responseText', { value: '{}', configurable: true });
Object.defineProperty(this, 'response', { value: '{}', configurable: true });
const evt = new Event('load');
this.dispatchEvent(evt);
if (typeof this.onload === 'function') this.onload(evt);
const readyEvt = new Event('readystatechange');
this.dispatchEvent(readyEvt);
if (typeof this.onreadystatechange === 'function') this.onreadystatechange(readyEvt);
return;
}
return originalSend.apply(this, arguments);
};
function patchInitialData() {
if (!isActive()) return;
try {
if (window.ytInitialData) {
stripAdsFromObject(window.ytInitialData);
if (window.ytInitialData.contents) {
const tabs = window.ytInitialData.contents.twoColumnBrowseResultsRenderer;
if (tabs && tabs.tabs) {
for (const tab of tabs.tabs) {
if (tab.tabRenderer && tab.tabRenderer.content) {
const section = tab.tabRenderer.content.sectionListRenderer;
if (section && section.contents) {
section.contents = section.contents.filter(item =>
!item.adSlotRenderer &&
!item.promotedSparklesTextSearchRenderer &&
!item.statementBannerRenderer
);
}
}
}
}
}
}
} catch (_) {}
}
const origDefineProperty = Object.defineProperty;
Object.defineProperty = function (obj, prop, descriptor) {
if (obj === window && prop === 'ytInitialData' && isActive()) {
if (descriptor && descriptor.value) {
stripAdsFromObject(descriptor.value);
}
if (descriptor && descriptor.get) {
const origGet = descriptor.get;
descriptor.get = function () {
const val = origGet.call(this);
return stripAdsFromObject(val);
};
}
}
return origDefineProperty(obj, prop, descriptor);
};
if (document.readyState === 'loading') {
document.addEventListener('DOMContentLoaded', patchInitialData, { once: true });
} else {
patchInitialData();
}
let patchAttempts = 0;
const patchInterval = setInterval(() => {
patchInitialData();
patchAttempts++;
if (patchAttempts >= 10) clearInterval(patchInterval);
}, 500);
document.addEventListener('yt-navigate-finish', patchInitialData);
document.addEventListener('yt-page-data-updated', patchInitialData);
document.addEventListener('skipad-force-skip', () => {
if (!isActive()) return;
try {
const mp = document.getElementById('movie_player');
if (mp) {
if (typeof mp.skipAd === 'function') {
mp.skipAd();
} else if (typeof mp.skipVideo === 'function') {
mp.skipVideo();
}
}
} catch (_) {}
});
})();