fix: redesign HLS detection with 6-strategy per-tab query
CI / test (pull_request) Canceled after 0s

- Query HLS data structures directly from page JS state per tab
- Strategies: video element HLS instances, window-level player vars,
  intercepted data, inline scripts, global config objects, content script
- Fixed merge logic: index.js now correctly processes interceptedManifests
  and hlsQueryResults instead of non-existent playerData
- Skip blob: URLs in DOM scan (HLS-internal, can't be played directly)
- Only attach HLS manifest when actually detected for that tab
This commit is contained in:
hermes committed 2026-08-10 20:39:49 +00:00
1 parent c22631d1f1
commit e17b228ee0
2 files changed
+578 -370

No files matched your search

+406 -149
View File
@@ -1,6 +1,11 @@
/**
* index.js — Popup logic for the Goon Plugin.
* Runs inside the popup; scans the current tab for videos and opens the viewer.
* Runs inside the popup; scans all tabs for videos and opens the viewer.
*
* Video detection strategy (per tab):
* 1. executeScript in main world: deeply query HLS player state for manifests
* 2. chrome.tabs.sendMessage to content script: get intercepted manifests + poster
* 3. executeScript DOM scan: fallback to <video> element attributes
*/
const STORAGE_KEY = 'goon_videos';
@@ -50,7 +55,6 @@ function renderList(items) {
listEl.appendChild(link);
}
// Copy all URLs button
const copyBtn = document.createElement('button');
copyBtn.className = 'copy-btn';
copyBtn.textContent = 'Copy All URLs';
@@ -82,9 +86,222 @@ function showActions() {
});
}
/* ====================================================================
* HLS DATA STRUCTURE QUERY — runs in the MAIN world via executeScript
*
* This is the primary detection strategy. We deeply probe the page's
* JavaScript state for HLS player instances, config objects, and
* playlist URLs. Works regardless of whether the page uses HLS.js,
* video.js, DASH.js, FLV.js, or a custom player.
* ==================================================================== */
const hlsQueryFn = function () {
// 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 (e) { return null; }
}
function isManifestUrl(url) {
if (!url || typeof url !== 'string') return false;
return url.includes('.m3u8') ||
url.includes('/playlist') ||
url.includes('/manifest') ||
url.includes('.mpd');
}
// Collect unique manifest URLs
const manifests = [];
const manifestSet = new Set();
function addManifest(url) {
const resolved = resolveUrl(url);
if (resolved && !manifestSet.has(resolved)) {
manifestSet.add(resolved);
manifests.push(resolved);
}
}
// --- STRATEGY 1: Probe video elements for HLS player instances ---
try {
const videos = document.querySelectorAll('video');
const hlsProps = [
'hls', 'hlsInstance', '_hls', '__hls', 'player', '_player',
'videoPlayer', '_videoPlayer', 'mediaPlayer', '_mediaPlayer',
'flvjsPlayer', '_flvjsPlayer', 'dashPlayer', '_dashPlayer',
'__hlsPlayer', '__player', '__videoPlayer', 'phPlayer',
'_phPlayer', 'phVideoPlayer', '_phVideoPlayer'
];
for (const video of videos) {
for (const prop of hlsProps) {
const instance = video[prop];
if (!instance || typeof instance !== 'object') continue;
// HLS.js: .url, .opts.url, ._stream.url
if (instance.url) addManifest(instance.url);
if (instance.opts && instance.opts.url) addManifest(instance.opts.url);
if (instance._stream && instance._stream.url) addManifest(instance._stream.url);
// HLS.js levels (quality variants)
if (instance.levels) {
for (const level of instance.levels) {
if (level.uri) addManifest(level.uri);
if (level._uri) addManifest(level._uri);
}
}
// video.js: .currentSource_.src or .src_
if (instance.currentSource_ && instance.currentSource_.src) addManifest(instance.currentSource_.src);
if (instance.src_) addManifest(instance.src_);
// Generic: .playlist, .source, .mediaUrl
if (instance.playlist) addManifest(instance.playlist);
if (instance.source) addManifest(instance.source);
if (instance.mediaUrl) addManifest(instance.mediaUrl);
}
}
} catch (e) { /* DOM access error */ }
// --- STRATEGY 2: Probe window-level player variables ---
try {
const windowPlayerVars = [
'Hls', '_Hls', 'hls', 'FLV', 'flvjs', 'Video', 'Videojs',
'videojs', 'p7m', 'P7m', 'Player', 'player', '_player',
'PornhubPlayer', 'PHPlayer', '_PHPlayer', 'phVideoPlayer',
'phPlayer', '_phPlayer', 'videoPlayer', 'MediaPlayer',
'CustomPlayer', 'phVideo', 'ph_media', '_PH_VIDEO',
'_hls', '_dash', '_video', '__player', '__hls'
];
// Deep-probe an object for manifest URLs (up to depth 3)
function deepProbe(obj, depth, seen) {
if (!obj || typeof obj !== 'object' || depth <= 0) return;
if (seen.has(obj)) return;
seen.add(obj);
for (const key of Object.keys(obj)) {
const val = obj[key];
if (typeof val === 'string' && isManifestUrl(val)) {
addManifest(val);
} else if (typeof val === 'object' && val !== null) {
deepProbe(val, depth - 1, seen);
}
}
}
for (const varName of windowPlayerVars) {
const obj = window[varName];
if (obj && typeof obj === 'object') {
deepProbe(obj, 3, new Set());
// Also check prototype chain
const proto = Object.getPrototypeOf(obj);
if (proto && proto !== Object.prototype) {
deepProbe(proto, 2, new Set());
}
}
}
} catch (e) { /* Ignore */ }
// --- STRATEGY 3: Probe intercepted data (from content script interceptors) ---
try {
if (window.__goon_plugin_data && window.__goon_plugin_data.manifests) {
for (const url of window.__goon_plugin_data.manifests) {
addManifest(url);
}
}
} catch (e) { /* Ignore */ }
// --- STRATEGY 4: Extract from inline scripts ---
try {
const scripts = document.querySelectorAll('script');
for (const script of scripts) {
const text = script.textContent || '';
if (!text.includes('.m3u8') && !text.includes('manifest') &&
!text.includes('playlist') && !text.includes('/hls')) continue;
// m3u8 URLs in quotes
const m3u8Regex = /['"]([^'"]*\.m3u8[^'"]*)['"]/g;
let match;
while ((match = m3u8Regex.exec(text)) !== null) {
addManifest(match[1]);
}
// CDN-style HLS URLs (Pornhub CDN patterns)
const cdnRegex = /['"]([^'"]*(?:phncdn|pornhub|porn\.com|pornhubpremium)[^'"]*(?:m3u8|playlist|manifest|\/hls\/)[^'"]*)['"]/g;
while ((match = cdnRegex.exec(text)) !== null) {
addManifest(match[1]);
}
// Generic manifest/playlist URLs
const genericRegex = /['"]([^'"]*(?:\/manifest\/|\/playlist\/|\/hls\/)[^'"]*)['"]/g;
while ((match = genericRegex.exec(text)) !== null) {
addManifest(match[1]);
}
}
} catch (e) { /* Ignore */ }
// --- STRATEGY 5: Probe __dataLayer, __INITIAL_STATE__, and similar ---
try {
const dataKeys = Object.keys(window).filter(k =>
k.startsWith('__') && k !== '__goon_plugin_data' &&
k !== '__goon_plugin_intercepted'
);
const maxProbe = Math.min(dataKeys.length, 15);
for (let i = 0; i < maxProbe; i++) {
const key = dataKeys[i];
const obj = window[key];
if (!obj || typeof obj !== 'object') continue;
// Quick heuristic: only deep-probe if it looks media-related
try {
const peek = JSON.stringify(obj).substring(0, 300);
if (peek.includes('video') || peek.includes('m3u8') ||
peek.includes('stream') || peek.includes('playlist') ||
peek.includes('hls') || peek.includes('manifest') ||
peek.includes('phncdn') || peek.includes('pornhubpremium')) {
function deepProbeLimited(o, d) {
if (!o || typeof o !== 'object' || d <= 0) return;
for (const k of Object.keys(o)) {
const v = o[k];
if (typeof v === 'string' && isManifestUrl(v)) {
addManifest(v);
} else if (typeof v === 'object' && v !== null) {
deepProbeLimited(v, d - 1);
}
}
}
deepProbeLimited(obj, 3);
}
} catch (e) { /* Ignore stringify errors */ }
}
} catch (e) { /* Ignore */ }
// --- STRATEGY 6: Query intercepted data from main world ---
try {
if (window.__goon_plugin_data && Array.isArray(window.__goon_plugin_data.manifests)) {
for (const m of window.__goon_plugin_data.manifests) {
addManifest(m);
}
}
} catch (e) {}
return {
manifests: manifests,
count: manifests.length,
};
};
/* === "Find Videos" — scan all tabs in the window === */
findBtn.addEventListener('click', async () => {
setStatus('Scanning tabs…');
setStatus('Scanning tabs...');
// Get all tabs in the current window
const allTabs = await chrome.tabs.query({ currentWindow: true });
@@ -105,172 +322,215 @@ findBtn.addEventListener('click', async () => {
}
const tabsToScan = validTabs;
setStatus(`Scanning ${tabsToScan.length} tab(s)…`);
setStatus(`Scanning ${tabsToScan.length} tab(s)...`);
try {
// Execute scraping on all tabs in parallel
const scrapeFn = () => {
const dataKeys = ['src', 'video', 'mp4', 'webm', 'poster'];
// DOM scrape function (runs in main world)
const scrapeFn = () => {
const dataKeys = ['src', 'video', 'mp4', 'webm', 'poster'];
// Collect all visible video elements, measure by rendered pixel area
const allVideos = Array.from(document.querySelectorAll('video')).filter(v => {
const style = window.getComputedStyle(v);
return style.display !== 'none' && v.clientWidth > 0 && v.clientHeight > 0;
}).map(v => {
const area = v.clientWidth * v.clientHeight;
const src = dataKeys.map(k => v.getAttribute('data-' + k)).find(Boolean)
|| (v.currentSrc || v.src || '')
|| null;
const poster = (v.poster || '')
|| dataKeys.filter(k => k !== 'poster').map(k => v.getAttribute('data-' + k)).find(Boolean)
|| v.getAttribute('data-poster') || null;
return { src, poster, area, width: v.clientWidth, height: v.clientHeight };
});
// Sort by displayed size descending, pick the largest
allVideos.sort((a, b) => b.area - a.area);
const largest = allVideos.length > 0 ? allVideos[0] : null;
const sources = Array.from(document.querySelectorAll('video source[src]')).map(s => s.src);
const audios = Array.from(document.querySelectorAll('audio')).length;
// Collect all visible video elements, measure by rendered pixel area
const allVideos = Array.from(document.querySelectorAll('video')).filter(v => {
const style = window.getComputedStyle(v);
return style.display !== 'none' && v.clientWidth > 0 && v.clientHeight > 0;
}).map(v => {
const area = v.clientWidth * v.clientHeight;
const src = dataKeys.map(k => v.getAttribute('data-' + k)).find(Boolean)
|| (v.currentSrc || v.src || '')
|| null;
const poster = (v.poster || '')
|| dataKeys.filter(k => k !== 'poster').map(k => v.getAttribute('data-' + k)).find(Boolean)
|| v.getAttribute('data-poster') || null;
return { src, poster, area, width: v.clientWidth, height: v.clientHeight };
});
allVideos.sort((a, b) => b.area - a.area);
const largest = allVideos.length > 0 ? allVideos[0] : null;
const sources = Array.from(document.querySelectorAll('video source[src]')).map(s => s.src);
const audios = Array.from(document.querySelectorAll('audio')).length;
return { videos: largest ? [largest] : [], videoCount: largest ? 1 : 0, sources, audios };
};
return { videos: largest ? [largest] : [], videoCount: largest ? 1 : 0, sources, audios };
};
// Query content scripts for real-time HLS interception data
async function queryContentScript(tabId) {
// Query content script for intercepted data + poster
async function queryContentScript(tabId) {
try {
return await chrome.tabs.sendMessage(tabId, { type: 'GOON_GET_VIDEO_DATA' });
} catch {
return null;
}
}
const results = await Promise.all(
tabsToScan.map(async t => {
try {
return await chrome.tabs.sendMessage(tabId, { type: 'GOON_GET_VIDEO_DATA' });
const [hlsQueryResults, scriptResult, contentData] = await Promise.all([
// STRATEGY 1: Deep query main-world HLS data structures
chrome.scripting.executeScript({
target: { tabId: t.id, allFrames: true },
func: hlsQueryFn,
}),
// STRATEGY 2: DOM scrape (existing behavior)
chrome.scripting.executeScript({
target: { tabId: t.id, allFrames: true },
func: scrapeFn,
}),
// STRATEGY 3: Content script intercepted manifests + poster
queryContentScript(t.id),
]);
return { hlsQueryResults: hlsQueryResults.filter(Boolean), scriptResult: scriptResult.filter(Boolean), contentData };
} catch {
// Content script not available (e.g., chrome:// pages)
console.warn('[goon] skipping tab', t.id, ':', t.url);
return null;
}
}
})
);
const results = await Promise.all(
tabsToScan.map(async t => {
try {
const [scriptResult, contentData] = await Promise.all([
chrome.scripting.executeScript({
target: { tabId: t.id, allFrames: true },
func: scrapeFn,
}),
queryContentScript(t.id),
]);
return { scriptResult: scriptResult.filter(Boolean), contentData };
} catch {
console.warn('[goon] skipping tab', t.id, ':', t.url);
return null;
}
})
);
// Merge all results into unique video entries
const seen = new Map();
let totalFound = 0;
// Build tab results — merge DOM snapshot with content script interception data
const tabResults = tabsToScan.map((tab, i) => {
const res = results[i];
if (!res) return null;
return {
tabTitle: tab.title || 'Unknown',
frames: res.scriptResult || [],
contentData: res.contentData || null,
};
}).filter(Boolean);
const scanned = tabResults.length;
for (let ti = 0; ti < tabsToScan.length; ti++) {
const tab = tabsToScan[ti];
const res = results[ti];
if (!res) continue;
const seen = new Map();
let totalFound = 0;
for (const group of tabResults) {
// Collect HLS manifests from the content script (real-time interception)
const hlsManifests = (group.contentData && group.contentData.hlsManifests) || [];
const contentPoster = (group.contentData && group.contentData.posterUrl) || null;
const tabTitle = tab.title || 'Unknown';
const contentPoster = (res.contentData && res.contentData.posterUrl) || null;
// Process DOM snapshot frames
for (const r of group.frames) {
if (!r.result) continue;
for (const v of r.result.videos) {
totalFound++;
const key = (v.src || '(empty)') + '||' + (v.poster || '(none)');
if (!seen.has(key)) {
const item = { url: v.src || v.poster || '', poster: v.poster || null, tab: group.tabTitle };
if (v.src) item.src = v.src;
// Attach HLS data from content script interception
if (hlsManifests.length > 0) {
item.hlsManifest = hlsManifests[0];
item.mediaType = 'hls';
// When HLS is present, prefer the manifest URL as the primary URL
if (!v.src || v.src.startsWith('blob:')) {
item.url = hlsManifests[0];
item.src = hlsManifests[0];
}
}
// Use poster from content script if available and better
if (contentPoster && !item.poster) {
item.poster = contentPoster;
}
seen.set(key, item);
}
}
}
// If no videos found in DOM but content script found HLS manifests + video sources,
// create entries from the intercepted data
if (!group.frames.length && group.contentData) {
const contentVideos = group.contentData.videoSources || [];
for (const cv of contentVideos) {
if (!cv.src && !hlsManifests.length) continue;
totalFound++;
const effectiveSrc = hlsManifests[0] || cv.src;
const key = effectiveSrc + '||' + (cv.poster || '(none)');
if (!seen.has(key)) {
const item = {
url: effectiveSrc,
src: effectiveSrc,
poster: cv.poster || contentPoster || null,
tab: group.tabTitle,
};
if (hlsManifests.length > 0) {
item.hlsManifest = hlsManifests[0];
item.mediaType = 'hls';
}
seen.set(key, item);
// --- PROCESS HLS MANIFESTS FROM MAIN-WORLD QUERY ---
const hlsManifests = [];
for (const frameResult of (res.hlsQueryResults || [])) {
const q = frameResult.result;
if (q && Array.isArray(q.manifests)) {
for (const m of q.manifests) {
if (m && m.startsWith('http')) {
hlsManifests.push(m);
}
}
}
}
const items = [...seen.values()].filter(v => {
const url = v.src || v.url || '';
return url.startsWith('http://') || url.startsWith('https://');
});
// --- PROCESS INTERCEPTED MANIFESTS FROM CONTENT SCRIPT ---
const interceptedManifests = [];
if (res.contentData && Array.isArray(res.contentData.interceptedManifests)) {
for (const im of res.contentData.interceptedManifests) {
if (im.url && im.url.startsWith('http')) {
interceptedManifests.push(im.url);
}
}
}
console.log('[goon] ===== Found', totalFound, 'video elements across', tabsToScan.length, 'tabs, unique:', items.length);
// Merge all manifest sources
const allManifests = [...new Set([...hlsManifests, ...interceptedManifests])];
// Replace the entire list (fresh scan) — preserve hlsManifest and mediaType
const storedItems = items.map(v => ({
url: v.url,
src: v.src || v.url,
poster: v.poster,
tab: v.tab,
hlsManifest: v.hlsManifest || null,
mediaType: v.mediaType || 'direct',
}));
await chrome.storage.local.set({
[STORAGE_KEY]: storedItems,
[TAGGED_KEY]: items.map(v => v.tab),
});
// --- PROCESS DOM VIDEO ELEMENTS ---
const domVideos = [];
for (const frameResult of (res.scriptResult || [])) {
const r = frameResult.result;
if (r && r.videos) {
for (const v of r.videos) {
if (v) domVideos.push(v);
}
}
}
const skippedTabs = results.filter(r => r === null).length;
const statusMsg = `Found ${items.length} video(s) across ${scanned} tab(s).`;
setStatus(skippedTabs > 0 ? `${statusMsg} ${skippedTabs} tab(s) skipped (idle).` : statusMsg);
btnRow.style.display = 'flex';
} catch (e) {
setStatus(`Scan failed: ${e.message}`);
console.warn('[goon] executeScript failed:', e);
// --- PROCESS CONTENT SCRIPT VIDEO ELEMENTS ---
const csVideos = [];
if (res.contentData && Array.isArray(res.contentData.videoElements)) {
for (const v of res.contentData.videoElements) {
csVideos.push(v);
}
}
// --- BUILD ENTRIES ---
// Priority 1: HLS manifests (per tab, only if present)
for (const manifestUrl of allManifests) {
totalFound++;
const key = 'hls||' + manifestUrl;
if (!seen.has(key)) {
const item = {
url: manifestUrl,
src: manifestUrl,
poster: contentPoster || null,
tab: tabTitle,
hlsManifest: manifestUrl,
mediaType: 'hls',
};
seen.set(key, item);
}
}
// Priority 2: DOM video elements (non-blob direct URLs)
for (const v of domVideos) {
// Skip blob: URLs — they're HLS-internal and can't be played directly
if (!v.src || v.src.startsWith('blob:')) continue;
totalFound++;
const key = (v.src || '(empty)') + '||' + (v.poster || '(none)');
if (!seen.has(key)) {
const item = {
url: v.src,
src: v.src,
poster: v.poster || contentPoster || null,
tab: tabTitle,
};
// If HLS is available for this tab, attach it
if (allManifests.length > 0) {
item.hlsManifest = allManifests[0];
item.mediaType = 'hls';
}
seen.set(key, item);
}
}
// Priority 3: Content script video elements
for (const v of csVideos) {
if (!v.src || v.src.startsWith('blob:')) continue;
totalFound++;
const key = (v.src || '(empty)') + '||' + (v.poster || '(none)');
if (!seen.has(key)) {
const item = {
url: v.src,
src: v.src,
poster: v.poster || contentPoster || null,
tab: tabTitle,
};
if (allManifests.length > 0) {
item.hlsManifest = allManifests[0];
item.mediaType = 'hls';
}
seen.set(key, item);
}
}
}
// Filter: keep only http/https URLs
const items = [...seen.values()].filter(v => {
const url = v.src || v.url || '';
return url.startsWith('http://') || url.startsWith('https://');
});
console.log('[goon] ===== Found', totalFound, 'video elements, unique:', items.length, 'across', tabsToScan.length, 'tabs');
// Store results
const storedItems = items.map(v => ({
url: v.url,
src: v.src || v.url,
poster: v.poster,
tab: v.tab,
hlsManifest: v.hlsManifest || null,
mediaType: v.mediaType || 'direct',
}));
await chrome.storage.local.set({
[STORAGE_KEY]: storedItems,
[TAGGED_KEY]: items.map(v => v.tab),
});
const skippedTabs = results.filter(r => r === null).length;
const statusMsg = `Found ${items.length} video(s) across ${tabsToScan.length} tab(s).`;
setStatus(skippedTabs > 0 ? `${statusMsg} ${skippedTabs} tab(s) skipped.` : statusMsg);
btnRow.style.display = 'flex';
});
/* === "Open Viewer" — launch the 2x2 grid === */
/* === "Open Viewer" — launch the NxN grid === */
openBtn.addEventListener('click', async () => {
const { urls, tags } = await loadVideos();
console.log('[goon] Open Viewer clicked, loaded from storage:', urls, 'tags:', tags);
@@ -280,11 +540,9 @@ openBtn.addEventListener('click', async () => {
return;
}
// Dedup by index to preserve tag alignment
const videoUrls = urls;
const videoTags = tags || [];
// Compute NxN grid
const N = Math.ceil(Math.sqrt(videoUrls.length));
const PANEL_MIN_W = 420;
const PANEL_MIN_H = 340;
@@ -295,7 +553,6 @@ openBtn.addEventListener('click', async () => {
const id = 'goonViewer_' + Date.now();
await chrome.storage.local.set({ [id]: videoUrls });
// Verify write
const verify = await chrome.storage.local.get([id]);
console.log('[goon] viewer storage verified:', verify[id]?.length, 'entries');