-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathpolystream.user.js
More file actions
592 lines (519 loc) · 23.3 KB
/
Copy pathpolystream.user.js
File metadata and controls
592 lines (519 loc) · 23.3 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
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
// ==UserScript==
// @name PolyStream - Multi-Tab Video Stream Enabler
// @namespace https://github.com/pdev-labs/polystream
// @version 1.0.0
// @description Stream multiple videos simultaneously across tabs without auto-pausing or single-tab lockouts on PhysicsWallah, LMS, and streaming sites.
// @author pdev-labs
// @match *://*/*
// @run-at document-start
// @grant none
// ==/UserScript==
(function () {
'use strict';
// Prevent multiple injections in the same window context
if (window.__MULTI_TAB_ENABLER_LOADED__) return;
window.__MULTI_TAB_ENABLER_LOADED__ = true;
// Generate a unique identifier for this specific tab session
const TAB_ID = (() => {
try {
let id = sessionStorage.getItem('__mte_tab_session_id__');
if (!id) {
id = 'tab_' + Math.random().toString(36).substring(2, 10) + '_' + Date.now();
sessionStorage.setItem('__mte_tab_session_id__', id);
}
return id;
} catch {
return 'tab_' + Math.random().toString(36).substring(2, 10);
}
})();
const config = {
enabled: true,
visibilityLock: true,
broadcastIsolation: true,
storageIsolation: true,
webLocksShim: true,
antiPauseShield: true,
debugLogging: false
};
const stats = {
blockedPauses: 0,
blockedVisibilityEvents: 0,
isolatedChannels: 0,
blockedStorageEvents: 0,
bypassedLocks: 0
};
function log(...args) {
if (config.debugLogging) {
console.log('%c[Multi-Tab Enabler (Userscript)]', 'background: #6366f1; color: #fff; padding: 2px 6px; border-radius: 4px;', ...args);
}
}
// Track recent user gestures (clicks, keydown) to allow intentional pauses
let lastUserInteractionTime = 0;
let lastInteractedElement = null;
function recordUserInteraction(event) {
if (event.isTrusted) {
lastUserInteractionTime = Date.now();
lastInteractedElement = event.target;
}
}
window.addEventListener('click', recordUserInteraction, { capture: true, passive: true });
window.addEventListener('pointerdown', recordUserInteraction, { capture: true, passive: true });
window.addEventListener('keydown', (event) => {
if (event.isTrusted && (event.code === 'Space' || event.code === 'KeyK' || event.code.startsWith('Media'))) {
lastUserInteractionTime = Date.now();
}
}, { capture: true, passive: true });
/* =========================================================================
* 1. VISIBILITY & FOCUS SPOOFING
* ========================================================================= */
try {
const originalHidden = Object.getOwnPropertyDescriptor(Document.prototype, 'hidden');
const originalVisibilityState = Object.getOwnPropertyDescriptor(Document.prototype, 'visibilityState');
Object.defineProperty(Document.prototype, 'hidden', {
get() {
return (!config.enabled || !config.visibilityLock) ? (originalHidden ? originalHidden.get.call(this) : false) : false;
},
configurable: true
});
Object.defineProperty(Document.prototype, 'visibilityState', {
get() {
return (!config.enabled || !config.visibilityLock) ? (originalVisibilityState ? originalVisibilityState.get.call(this) : 'visible') : 'visible';
},
configurable: true
});
if ('webkitHidden' in Document.prototype) {
Object.defineProperty(Document.prototype, 'webkitHidden', {
get() { return (!config.enabled || !config.visibilityLock) ? false : false; },
configurable: true
});
}
if ('webkitVisibilityState' in Document.prototype) {
Object.defineProperty(Document.prototype, 'webkitVisibilityState', {
get() { return (!config.enabled || !config.visibilityLock) ? 'visible' : 'visible'; },
configurable: true
});
}
const originalHasFocus = Document.prototype.hasFocus;
Document.prototype.hasFocus = function () {
if (config.enabled && config.visibilityLock) return true;
return originalHasFocus.apply(this, arguments);
};
const originalDocAddEventListener = Document.prototype.addEventListener;
const originalWinAddEventListener = Window.prototype.addEventListener;
const blockedDocEvents = new Set(['visibilitychange', 'webkitvisibilitychange']);
const blockedWinEvents = new Set(['blur', 'focusout', 'pagehide']);
Document.prototype.addEventListener = function (type, listener, options) {
if (typeof type === 'string' && blockedDocEvents.has(type.toLowerCase())) {
const wrappedListener = function (event) {
if (config.enabled && config.visibilityLock) {
stats.blockedVisibilityEvents++;
log('Blocked document visibilitychange event dispatch');
return;
}
return typeof listener === 'function' ? listener.apply(this, arguments) : listener.handleEvent(event);
};
return originalDocAddEventListener.call(this, type, wrappedListener, options);
}
return originalDocAddEventListener.apply(this, arguments);
};
Window.prototype.addEventListener = function (type, listener, options) {
if (typeof type === 'string' && blockedWinEvents.has(type.toLowerCase())) {
const wrappedListener = function (event) {
if (config.enabled && config.visibilityLock) {
stats.blockedVisibilityEvents++;
log(`Blocked window ${type} event dispatch`);
return;
}
return typeof listener === 'function' ? listener.apply(this, arguments) : listener.handleEvent(event);
};
return originalWinAddEventListener.call(this, type, wrappedListener, options);
}
return originalWinAddEventListener.apply(this, arguments);
};
try {
Object.defineProperty(document, 'onvisibilitychange', {
get() { return null; },
set(handler) {
if (typeof handler === 'function') document.addEventListener('visibilitychange', handler);
},
configurable: true
});
} catch {}
} catch (e) {
console.warn('[Multi-Tab Enabler] Visibility spoofing init error:', e);
}
/* =========================================================================
* 2. BROADCASTCHANNEL ISOLATION
* ========================================================================= */
try {
if (typeof window.BroadcastChannel !== 'undefined') {
const OriginalBroadcastChannel = window.BroadcastChannel;
window.BroadcastChannel = function (name) {
if (!config.enabled || !config.broadcastIsolation) {
return new OriginalBroadcastChannel(name);
}
stats.isolatedChannels++;
const isolatedName = `__mte_${TAB_ID}__${name}`;
const channelInstance = new OriginalBroadcastChannel(isolatedName);
const originalPostMessage = channelInstance.postMessage.bind(channelInstance);
channelInstance.postMessage = function (msg) {
try {
const str = typeof msg === 'object' ? JSON.stringify(msg) : String(msg);
if (/pause|stop|concurrent|active_tab|heartbeat/i.test(str)) {
log(`BroadcastChannel "${name}" message intercepted:`, msg);
}
} catch {}
return originalPostMessage(msg);
};
return channelInstance;
};
window.BroadcastChannel.prototype = OriginalBroadcastChannel.prototype;
}
} catch (e) {
console.warn('[Multi-Tab Enabler] BroadcastChannel isolation error:', e);
}
/* =========================================================================
* 3. STORAGE VIRTUALIZATION & EVENT FILTERING (PW & LMS Player Tab Sync)
* ========================================================================= */
try {
const tabScopedStorage = new Map();
const originalWinAddEventListenerForStorage = Window.prototype.addEventListener;
const hasStorage = typeof Storage !== 'undefined';
const originalSetItem = hasStorage ? Storage.prototype.setItem : null;
const originalGetItem = hasStorage ? Storage.prototype.getItem : null;
const originalRemoveItem = hasStorage ? Storage.prototype.removeItem : null;
const PLAYER_STORAGE_REGEX = /player|video|media|play|pause|session|token|active|tab|penpencil|watch|stream|concurr/i;
if (hasStorage) {
Storage.prototype.setItem = function (key, value) {
if (this === window.localStorage && config.enabled && config.storageIsolation) {
if (typeof key === 'string' && PLAYER_STORAGE_REGEX.test(key)) {
tabScopedStorage.set(key, String(value));
try {
originalSetItem.call(this, `__mte_${TAB_ID}__${key}`, String(value));
} catch {}
stats.blockedStorageEvents++;
log(`Virtualized localStorage.setItem for key: "${key}"`);
return;
}
}
return originalSetItem.apply(this, arguments);
};
Storage.prototype.getItem = function (key) {
if (this === window.localStorage && config.enabled && config.storageIsolation) {
if (typeof key === 'string' && PLAYER_STORAGE_REGEX.test(key)) {
if (tabScopedStorage.has(key)) {
return tabScopedStorage.get(key);
}
try {
const scopedVal = originalGetItem.call(this, `__mte_${TAB_ID}__${key}`);
if (scopedVal !== null) return scopedVal;
} catch {}
}
}
return originalGetItem.apply(this, arguments);
};
Storage.prototype.removeItem = function (key) {
if (this === window.localStorage && config.enabled && config.storageIsolation) {
if (typeof key === 'string' && PLAYER_STORAGE_REGEX.test(key)) {
tabScopedStorage.delete(key);
try {
originalRemoveItem.call(this, `__mte_${TAB_ID}__${key}`);
} catch {}
return;
}
}
return originalRemoveItem.apply(this, arguments);
};
}
Window.prototype.addEventListener = function (type, listener, options) {
if (typeof type === 'string' && type.toLowerCase() === 'storage') {
const wrappedListener = function (event) {
if (config.enabled && config.storageIsolation) {
const key = event.key || '';
const val = event.newValue || '';
if (!key || PLAYER_STORAGE_REGEX.test(key + val)) {
stats.blockedStorageEvents++;
log(`Blocked cross-tab storage sync event on key: "${key}"`);
return;
}
}
return typeof listener === 'function' ? listener.apply(this, arguments) : listener.handleEvent(event);
};
return originalWinAddEventListenerForStorage.call(this, type, wrappedListener, options);
}
return originalWinAddEventListenerForStorage.apply(this, arguments);
};
try {
let _onstorage = null;
Object.defineProperty(window, 'onstorage', {
get() { return _onstorage; },
set(fn) {
_onstorage = fn;
if (typeof fn === 'function') window.addEventListener('storage', fn);
},
configurable: true
});
} catch {}
} catch (e) {
console.warn('[Multi-Tab Enabler] Storage event isolation error:', e);
}
/* =========================================================================
* 3.5 POPUP & MODAL AUTO-KILLER ("Playback Interrupted" / "Streaming on another device")
* Specifically neutralizes PW's "Playback Interrupted / streaming on another device" dialog
* ========================================================================= */
try {
const INTERRUPT_PATTERNS = /playback\s*interrupted|already\s*streaming\s*on\s*another\s*device|refresh\s*to\s*watch\s*on\s*this\s*device|playback\s*will\s*stop\s*on\s*the\s*other\s*device|streaming\s*on\s*another\s*device|watching\s*on\s*another\s*(tab|device|window)|signed\s*in\s*with\s*another\s*tab|opened\s*in\s*another\s*tab|another\s*session/i;
const killInterruptionModals = () => {
if (!config.enabled) return;
const candidates = document.querySelectorAll('div, section, aside, dialog, .modal, [role="dialog"], [role="alertdialog"], .popup');
for (const el of candidates) {
if (el.children.length > 25) continue;
const text = (el.innerText || el.textContent || '').trim();
if (INTERRUPT_PATTERNS.test(text)) {
log('Detected "Playback Interrupted / Another Device" modal overlay. Annihilating...');
stats.blockedPauses++;
// Find root modal container (not body or main content)
let targetContainer = el;
while (
targetContainer.parentElement &&
targetContainer.parentElement !== document.body &&
targetContainer.parentElement !== document.documentElement &&
targetContainer.parentElement.children.length <= 3 &&
!targetContainer.parentElement.querySelector('video')
) {
targetContainer = targetContainer.parentElement;
}
// Instantly hide and remove
targetContainer.style.setProperty('display', 'none', 'important');
targetContainer.style.setProperty('visibility', 'hidden', 'important');
targetContainer.style.setProperty('opacity', '0', 'important');
targetContainer.style.setProperty('pointer-events', 'none', 'important');
try { targetContainer.remove(); } catch {}
// Remove any dimmed backdrops or overlay blockers
document.querySelectorAll('.modal-backdrop, [class*="backdrop"], [class*="overlay"], [class*="dimmer"]').forEach((bd) => {
if (!bd.querySelector('video')) {
bd.style.setProperty('display', 'none', 'important');
try { bd.remove(); } catch {}
}
});
// Unfreeze page scrolling & pointer events
if (document.body) {
document.body.style.setProperty('overflow', 'auto', 'important');
document.body.style.setProperty('pointer-events', 'auto', 'important');
}
if (document.documentElement) {
document.documentElement.style.setProperty('overflow', 'auto', 'important');
}
// Check if there is an affirmative continue button (DO NOT click Refresh / Reload!)
const actionBtn = el.querySelector('button, a, [role="button"]');
if (actionBtn) {
const btnText = (actionBtn.textContent || '').trim().toLowerCase();
if (!/refresh|reload|logout|sign out/.test(btnText) && /continue|resume|play|dismiss|close|okay|got it/.test(btnText)) {
log('Clicking safe continue button:', btnText);
actionBtn.click();
}
}
// Force unpause video in case PW tried to pause it
document.querySelectorAll('video').forEach((v) => {
if (v.paused) v.play().catch(() => {});
});
break;
}
}
};
// Fast interval check
setInterval(killInterruptionModals, 150);
// Instant DOM MutationObserver hook to kill on arrival
if (typeof MutationObserver !== 'undefined') {
const modalObserver = new MutationObserver((mutations) => {
for (const m of mutations) {
for (const node of m.addedNodes) {
if (node.nodeType === Node.ELEMENT_NODE) {
const text = (node.innerText || node.textContent || '');
if (INTERRUPT_PATTERNS.test(text)) {
killInterruptionModals();
return;
}
}
}
}
});
const attachObserver = () => {
const root = document.body || document.documentElement;
if (root) {
modalObserver.observe(root, { childList: true, subtree: true });
}
};
if (document.body) {
attachObserver();
} else {
document.addEventListener('DOMContentLoaded', attachObserver);
}
}
} catch (e) {
console.warn('[Multi-Tab Enabler] Modal auto-dismiss error:', e);
}
/* =========================================================================
* 3.6 WEBSOCKET & HEARTBEAT INTERCEPTION (Server-Side Concurrency Shield)
* Drops server-sent interruption messages before they trigger the UI dialog
* ========================================================================= */
try {
if (typeof window.WebSocket !== 'undefined') {
const OriginalWebSocket = window.WebSocket;
window.WebSocket = function (url, protocols) {
const ws = protocols !== undefined ? new OriginalWebSocket(url, protocols) : new OriginalWebSocket(url);
const originalAddEventListener = ws.addEventListener.bind(ws);
ws.addEventListener = function (type, listener, options) {
if (type === 'message') {
const wrappedListener = function (event) {
try {
const dataStr = typeof event.data === 'string' ? event.data : '';
if (/playback\s*interrupted|streaming\s*on\s*another\s*device|already\s*streaming|concurrency|conflict|device_limit/i.test(dataStr)) {
stats.blockedPauses++;
log('Dropped incoming WebSocket server interruption event:', dataStr);
return;
}
} catch {}
return typeof listener === 'function' ? listener.apply(this, arguments) : listener.handleEvent(event);
};
return originalAddEventListener(type, wrappedListener, options);
}
return originalAddEventListener(type, listener, options);
};
let _onmessage = null;
Object.defineProperty(ws, 'onmessage', {
get() { return _onmessage; },
set(handler) {
if (typeof handler === 'function') {
_onmessage = function (event) {
try {
const dataStr = typeof event.data === 'string' ? event.data : '';
if (/playback\s*interrupted|streaming\s*on\s*another\s*device|already\s*streaming|concurrency|conflict|device_limit/i.test(dataStr)) {
stats.blockedPauses++;
log('Dropped onmessage server interruption event:', dataStr);
return;
}
} catch {}
return handler.apply(this, arguments);
};
ws.addEventListener('message', _onmessage);
} else {
_onmessage = null;
}
},
configurable: true
});
return ws;
};
window.WebSocket.prototype = OriginalWebSocket.prototype;
}
} catch (e) {
console.warn('[Multi-Tab Enabler] WebSocket interception error:', e);
}
/* =========================================================================
* 4. WEB LOCKS API SHIM (navigator.locks)
* ========================================================================= */
try {
if (navigator.locks && typeof navigator.locks.request === 'function') {
const originalLocksRequest = navigator.locks.request.bind(navigator.locks);
navigator.locks.request = function (name, ...args) {
if (!config.enabled || !config.webLocksShim) {
return originalLocksRequest(name, ...args);
}
const scopedName = `__mte_${TAB_ID}__${name}`;
stats.bypassedLocks++;
log(`Scoped Web Lock "${name}" -> "${scopedName}"`);
return originalLocksRequest(scopedName, ...args);
};
}
} catch (e) {
console.warn('[Multi-Tab Enabler] Web Locks shim error:', e);
}
/* =========================================================================
* 5. ANTI-PAUSE SHIELD (HTMLMediaElement.prototype.pause)
* ========================================================================= */
try {
const originalPause = HTMLMediaElement.prototype.pause;
const originalPlay = HTMLMediaElement.prototype.play;
HTMLMediaElement.prototype.pause = function () {
if (!config.enabled || !config.antiPauseShield) {
return originalPause.apply(this, arguments);
}
const now = Date.now();
const timeSinceUserGesture = now - lastUserInteractionTime;
const isNaturalEnded = this.ended || (this.duration && Math.abs(this.currentTime - this.duration) < 0.5);
const isDirectUserAction = timeSinceUserGesture < 450 || (
lastInteractedElement && (
lastInteractedElement === this ||
this.contains(lastInteractedElement) ||
(lastInteractedElement.closest && (lastInteractedElement.closest('video, audio, .video-player, .player-container, button') !== null))
) && timeSinceUserGesture < 800
);
const isUserActive = (navigator.userActivation && navigator.userActivation.isActive);
if (isNaturalEnded || isDirectUserAction || isUserActive) {
log('User pause permitted.');
return originalPause.apply(this, arguments);
}
stats.blockedPauses++;
log('Shielded against programmatic/background pause call on media element:', this);
return Promise.resolve();
};
const registerMediaGuards = (media) => {
if (media.__mte_guarded__) return;
media.__mte_guarded__ = true;
media.addEventListener('pause', (e) => {
if (!config.enabled || !config.antiPauseShield) return;
const now = Date.now();
const timeSinceUserGesture = now - lastUserInteractionTime;
const isNaturalEnded = media.ended || (media.duration && Math.abs(media.currentTime - media.duration) < 0.5);
if (!isNaturalEnded && timeSinceUserGesture > 600 && !document.hasFocus()) {
log('Video was paused in background tab without user gesture! Auto-resuming...');
stats.blockedPauses++;
setTimeout(() => {
if (media.paused && !media.ended) {
originalPlay.call(media).catch(() => {});
}
}, 50);
}
}, { capture: true });
};
document.querySelectorAll('video, audio').forEach(registerMediaGuards);
if (typeof MutationObserver !== 'undefined') {
const observer = new MutationObserver((mutations) => {
for (const mutation of mutations) {
for (const node of mutation.addedNodes) {
if (node.nodeType === Node.ELEMENT_NODE) {
if (node.tagName === 'VIDEO' || node.tagName === 'AUDIO') {
registerMediaGuards(node);
} else if (node.querySelectorAll) {
node.querySelectorAll('video, audio').forEach(registerMediaGuards);
}
}
}
}
});
if (document.documentElement) {
observer.observe(document.documentElement, { childList: true, subtree: true });
} else {
document.addEventListener('DOMContentLoaded', () => {
if (document.documentElement) {
observer.observe(document.documentElement, { childList: true, subtree: true });
}
});
}
}
} catch (e) {
console.warn('[Multi-Tab Enabler] Anti-pause shield error:', e);
}
// Expose global controller for easy devtools inspection or custom scripting
window.__MultiTabEnabler = {
config,
stats,
enable: () => { config.enabled = true; console.log('[Multi-Tab Enabler] ENABLED'); },
disable: () => { config.enabled = false; console.log('[Multi-Tab Enabler] DISABLED'); },
playAll: () => {
document.querySelectorAll('video, audio').forEach(m => m.play().catch(() => {}));
}
};
log('Multi-Tab Video Stream Enabler initialized (Userscript)');
})();