feat: add HLS playback support with hls.js and modular viewer architecture
CI / test (pull_request) Successful in 23s
CI / test (pull_request) Successful in 23s
This PR adds end-to-end HLS stream detection, interception, and playback to the viewer. The following root causes from the QA audit are fixed: - Restore dropped HLS detection strategies (6-strategy per-tab query) - Add deep-probe scanning for config globals and iframe manifests - Add standalone interceptors.js for CSP-compliant runtime injection - Fix viewer.js string entry handling for legacy storage entries - Fix manifest.json web_accessible_resources for interceptors.js - Disable modulePreload polyfill for Chrome MV3 compatibility - Run deep probe and scraper in MAIN world for page global access - Fallback to src as HLS manifest when hlsManifest field is missing The viewer.js codebase has been refactored from a monolithic wirePlaybackTracking() into modular single-responsibility functions: PanelState, EventWiring, BufferMonitor, PanelRenderer, AggregateRenderer, PanelFactory. Additionally: - Remove debug console.log statements from production code - Fix dead code in m3u8DeepProbeFn walk() that prevented config global manifest detection - Add eslint config with 141 rules and .eslintignore - Add 44 new tests (83/83 total passing) covering HLS detection, config validation, edge cases, and integration scenarios - Pin HLS.js CDN version to 1.5.17 (not floating/latest) - Add grid-optimized HLS config with 10s buffer cap - Add cleanup hooks for HLS instance destruction on page unload Changed files (18): - src/viewer/viewer.js (+834) - src/viewer/index.js (+870) - src/viewer/viewer.test.js (+976, new) - src/viewer/index.test.js (+481, new) - src/inject/interceptors.js (+169, new) - src/content/content.js (+139) - __tests__/hls_qa.test.mjs (+1168, new) - manifest.json (+6) - vite.config.ts (+5) - __tests__/run.mjs (+2) - __tests__/config_validation.test.mjs (+102, new) - eslint.config.js (+24, new) - .eslintignore (+1, new) - QA_REPORT_HLS.md (+160, new) - __tests__/data_flow.test.mjs (+/-5) - __tests__/schema.test.mjs (+/-5) - __tests__/viewer_integration.test.mjs (+/-4) - src/background/background.js (-2)
This commit is contained in:
1 parent
7416ae8ff9
commit
542c5786be
18 files changed
+4316
-731
No files matched your search
@@ -0,0 +1,169 @@
|
||||
/**
|
||||
* interceptors.js — Main-world interceptors for HLS manifest detection.
|
||||
*
|
||||
* This file is injected into the main world via a <script src="..."> tag
|
||||
* (NOT inline textContent) to bypass strict CSP on sites like Pornhub.
|
||||
*
|
||||
* When loaded as an external script from the extension origin, Chrome treats
|
||||
* it as an extension content script — not as page inline code — so the
|
||||
* page's script-src CSP does not apply.
|
||||
*
|
||||
* Loaded by content.js at document_start before any page JS runs.
|
||||
*/
|
||||
|
||||
(function () {
|
||||
'use strict';
|
||||
|
||||
if (window.__goon_plugin_intercepted) return;
|
||||
window.__goon_plugin_intercepted = true;
|
||||
|
||||
window.__goon_plugin_data = window.__goon_plugin_data || {
|
||||
manifests: [],
|
||||
sources: [],
|
||||
};
|
||||
|
||||
// Helper: resolve relative URLs
|
||||
function resolveUrl(url) {
|
||||
if (!url) return null;
|
||||
try {
|
||||
if (url.startsWith('//')) return 'https:' + url;
|
||||
if (url.startsWith('http')) return url;
|
||||
return new URL(url, window.location.origin).href;
|
||||
} catch {
|
||||
return null;
|
||||
}
|
||||
}
|
||||
|
||||
// Helper: report manifest to content script via postMessage
|
||||
function reportManifest(url, source) {
|
||||
var resolved = resolveUrl(url);
|
||||
if (!resolved) return;
|
||||
if (window.__goon_plugin_data.manifests.includes(resolved)) return;
|
||||
window.__goon_plugin_data.manifests.push(resolved);
|
||||
|
||||
window.postMessage(
|
||||
{
|
||||
type: 'GOON_PLUGIN_HLS_INTERCEPT',
|
||||
url: resolved,
|
||||
source: source,
|
||||
timestamp: Date.now(),
|
||||
},
|
||||
'*',
|
||||
);
|
||||
}
|
||||
|
||||
// === INTERCEPTORS ===
|
||||
|
||||
// 1. Intercept XMLHttpRequest for manifest-like requests
|
||||
var originalOpen = XMLHttpRequest.prototype.open;
|
||||
var originalSend = XMLHttpRequest.prototype.send;
|
||||
XMLHttpRequest.prototype.open = function (method, url) {
|
||||
this._goon_url = url;
|
||||
return originalOpen.apply(this, arguments);
|
||||
};
|
||||
XMLHttpRequest.prototype.send = function (_body) {
|
||||
if (this._goon_url && typeof this._goon_url === 'string') {
|
||||
var resolved = resolveUrl(this._goon_url);
|
||||
if (
|
||||
resolved &&
|
||||
(resolved.includes('.m3u8') ||
|
||||
resolved.includes('/playlist') ||
|
||||
resolved.includes('/manifest'))
|
||||
) {
|
||||
reportManifest(resolved, 'xhr');
|
||||
}
|
||||
}
|
||||
return originalSend.apply(this, arguments);
|
||||
};
|
||||
|
||||
// 2. Intercept fetch() for manifest-like requests
|
||||
var originalFetch = window.fetch;
|
||||
window.fetch = function (resource, _init) {
|
||||
var url = typeof resource === 'string' ? resource : resource && resource.url;
|
||||
if (url && typeof url === 'string') {
|
||||
var resolved = resolveUrl(url);
|
||||
if (
|
||||
resolved &&
|
||||
(resolved.includes('.m3u8') ||
|
||||
resolved.includes('/playlist') ||
|
||||
resolved.includes('/manifest'))
|
||||
) {
|
||||
reportManifest(resolved, 'fetch');
|
||||
}
|
||||
}
|
||||
return originalFetch.apply(this, arguments);
|
||||
};
|
||||
|
||||
// 3. Intercept HLS.js loadSource (catches HLS.js instances)
|
||||
function patchHlsPrototype() {
|
||||
if (typeof Hls === 'undefined') return;
|
||||
var originalLoadSource = Hls.prototype.loadSource;
|
||||
if (!originalLoadSource) return;
|
||||
|
||||
Hls.prototype.loadSource = function (url) {
|
||||
reportManifest(url, 'hls_loadSource');
|
||||
return originalLoadSource.apply(this, arguments);
|
||||
};
|
||||
|
||||
var originalAttachMedia = Hls.prototype.attachMedia;
|
||||
if (originalAttachMedia) {
|
||||
Hls.prototype.attachMedia = function (media) {
|
||||
if (media && media.tagName === 'VIDEO') {
|
||||
media._goon_hls_manifest = this.url;
|
||||
reportManifest(this.url, 'hls_attachMedia');
|
||||
}
|
||||
return originalAttachMedia.apply(this, arguments);
|
||||
};
|
||||
}
|
||||
}
|
||||
|
||||
// Patch immediately if HLS is already defined
|
||||
patchHlsPrototype();
|
||||
|
||||
// Watch for HLS.js to be dynamically defined later.
|
||||
// Use a combination of:
|
||||
// 1. Longer polling window (30s instead of 3s) to catch lazy-loaded players.
|
||||
// 2. MutationObserver on <head> for new <script> tags — catches late hls.js loads.
|
||||
var hlsPatched = false;
|
||||
if (!hlsPatched) {
|
||||
var pollCount = 0;
|
||||
var maxPolls = 600; // poll for up to ~30s (50ms interval)
|
||||
|
||||
var pollHls = setInterval(function () {
|
||||
pollCount++;
|
||||
if (typeof Hls !== 'undefined' && !hlsPatched) {
|
||||
hlsPatched = true;
|
||||
patchHlsPrototype();
|
||||
clearInterval(pollHls);
|
||||
}
|
||||
if (pollCount >= maxPolls) clearInterval(pollHls);
|
||||
}, 50);
|
||||
|
||||
// MutationObserver: watches for new <script> tags that may load hls.js late.
|
||||
// This catches dynamically-injected players that appear after the poll window.
|
||||
try {
|
||||
var observer = new MutationObserver(function (mutations) {
|
||||
if (hlsPatched) { observer.disconnect(); return; }
|
||||
for (var m = 0; m < mutations.length; m++) {
|
||||
var nodes = mutations[m].addedNodes;
|
||||
if (!nodes) continue;
|
||||
for (var n = 0; n < nodes.length; n++) {
|
||||
var node = nodes[n];
|
||||
if (node.tagName === 'SCRIPT' && node.src) {
|
||||
var src = node.src.toLowerCase();
|
||||
if (src.includes('hls') || src.includes('videojs') || src.includes('player')) {
|
||||
// Script tag loaded — check if Hls is now defined
|
||||
if (typeof Hls !== 'undefined' && !hlsPatched) {
|
||||
hlsPatched = true;
|
||||
patchHlsPrototype();
|
||||
observer.disconnect();
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
});
|
||||
observer.observe(document.head || document.documentElement, { childList: true, subtree: true });
|
||||
} catch(_e) { /* MutationObserver not available — polling still works */ }
|
||||
}
|
||||
})();
|
||||
Reference in new issue
Block a user