拦截社交媒体视频流:40 行控制台脚本

发布: (2026年2月2日 GMT+8 04:35)
2 分钟阅读
原文: Dev.to

Source: Dev.to

工作原理

它会重写 fetch()XMLHttpRequest.open() 来拦截外发的网络请求。当检测到 .mp4 URL 时,会去除字节范围参数并记录下干净的 URL。getVideoUrls() 辅助函数可以让你获取所有收集到的链接,方便在控制台信息过多时使用。

// Instagram Story Video URL Extractor
(() => {
    const videoUrls = new Set();

    // Strip start and end time params
    const cleanUrl = (url) => {
        const u = new URL(url);
        u.searchParams.delete('bytestart');
        u.searchParams.delete('byteend');
        return u.toString();
    };

    // Intercept fetch
    const origFetch = window.fetch;
    window.fetch = async function(url, ...args) {
        const urlStr = url?.toString?.() || url;

        // Domain and extension matcher, you can drop the domain to use this elsewhere
        if (urlStr.includes('fbcdn.net') && urlStr.includes('.mp4')) {
            const clean = cleanUrl(urlStr);
            if (!videoUrls.has(clean)) {
                videoUrls.add(clean);
                console.log('> Video found:', clean);
            }
        }
        return origFetch.apply(this, [url, ...args]);
    };

    // Intercept XHR
    const origOpen = XMLHttpRequest.prototype.open;
    XMLHttpRequest.prototype.open = function(method, url) {

        // Domain and extension matcher, you can drop the domain to use this elsewhere
        if (url?.includes?.('fbcdn.net') && url?.includes?.('.mp4')) {
            const clean = cleanUrl(url);
            if (!videoUrls.has(clean)) {
                videoUrls.add(clean);
                console.log('> Video found:', clean);
            }
        }
        return origOpen.apply(this, arguments);
    };

    // Helper to get all collected URLs
    window.getVideoUrls = () => {
        const urls = [...videoUrls];
        console.log(`\nFound ${urls.length} video(s):\n`);
        urls.forEach((url, i) => console.log(`${i + 1}. ${url}\n`));
        return urls;
    };

    console.log('Extractor active - play stories/reels, then run getVideoUrls()');
})();

注意: 此脚本仅用于研究目的。请勿下载您不拥有的内容。

使用方法(针对完全新手)

  1. 打开开发者工具(按 F12)。
  2. 将脚本粘贴到控制台并按 Enter
  3. 播放想要的视频;URL 会出现在控制台中。
  4. 可选地运行 getVideoUrls() 列出所有收集到的 URL。
  5. 在新标签页打开某个 URL 观看视频,或右键 → 另存为 下载。
Back to Blog

相关文章

阅读更多 »