pastebin - collaborative debugging tool
kpaste.net RSS


Untitled
Posted by Anonymous on Thu 13th Aug 2026 03:29
raw | new post

  1. #!/usr/bin/env python3
  2.  
  3. import sys
  4.  
  5. from invisible_playwright import InvisiblePlaywright
  6.  
  7. INIT_SCRIPT = """
  8. () => {
  9.     window.__m3u8FetchState = { count: 0, secondUrl: null };
  10.  
  11.     const originalFetch = window.fetch.bind(window);
  12.  
  13.     window.fetch = function (input, init) {
  14.         try {
  15.             const rawUrl =
  16.                 typeof input === 'string'
  17.                     ? input
  18.                     : input instanceof URL
  19.                     ? input.toString()
  20.                     : input && input.url
  21.                     ? input.url
  22.                     : String(input);
  23.  
  24.             const resolved = new URL(rawUrl, window.location.href);
  25.             const pathOnly = resolved.pathname; // ignore query string / hash
  26.  
  27.             if (pathOnly.toLowerCase().endsWith('.m3u8')) {
  28.                 window.__m3u8FetchState.count += 1;
  29.                 if (window.__m3u8FetchState.count === 2 && !window.__m3u8FetchState.secondUrl) {
  30.                     window.__m3u8FetchState.secondUrl = resolved.href;
  31.                 }
  32.             }
  33.         } catch (e) {
  34.             // If URL parsing fails for some exotic input, just let the
  35.             // fetch proceed untouched rather than breaking the page.
  36.         }
  37.  
  38.         return originalFetch(input, init);
  39.     };
  40. }
  41. """
  42.  
  43.  
  44. def main() -> int:
  45.     if len(sys.argv) < 2:
  46.         print("Usage: python second_m3u8_fetch.py <url>", file=sys.stderr)
  47.         return 1
  48.  
  49.     url = sys.argv[1]
  50.  
  51.     with InvisiblePlaywright() as browser:
  52.         page = browser.new_page()
  53.  
  54.         # Installed before any page JS runs.
  55.         page.add_init_script(INIT_SCRIPT)
  56.  
  57.         try:
  58.             page.goto(url, wait_until="domcontentloaded")
  59.         except Exception as err:
  60.             print(f"Navigation failed: {err}", file=sys.stderr)
  61.             return 3
  62.  
  63.         print("Waiting for the second fetch()-ed .m3u8 URL (no timeout)...", file=sys.stderr)
  64.  
  65.         # timeout=0 means wait indefinitely. If the page only ever fetches
  66.         # one .m3u8 file, this call simply never returns — by design.
  67.         handle = page.wait_for_function(
  68.             "() => window.__m3u8FetchState.secondUrl", timeout=0
  69.         )
  70.         second_m3u8_url = handle.json_value()
  71.  
  72.     print(second_m3u8_url)
  73.     return 0
  74.  
  75.  
  76. if __name__ == "__main__":
  77.     sys.exit(main())

Submit a correction or amendment below (click here to make a fresh posting)
After submitting an amendment, you'll be able to view the differences between the old and new posts easily.

Syntax highlighting:

To highlight particular lines, prefix each line with {%HIGHLIGHT}




All content is user-submitted.
The administrators of this site (kpaste.net) are not responsible for their content.
Abuse reports should be emailed to us at