Fix LIHKG Embed Iframe

To fix LIHKG Embed Iframe

目前為 2023-09-20 提交的版本,檢視 最新版本

您需要先安裝使用者腳本管理器擴展,如 TampermonkeyGreasemonkeyViolentmonkey 之後才能安裝該腳本。

You will need to install an extension such as Tampermonkey to install this script.

您需要先安裝使用者腳本管理器擴充功能,如 TampermonkeyViolentmonkey 後才能安裝該腳本。

您需要先安裝使用者腳本管理器擴充功能,如 TampermonkeyUserscripts 後才能安裝該腳本。

你需要先安裝一款使用者腳本管理器擴展,比如 Tampermonkey,才能安裝此腳本

您需要先安裝使用者腳本管理器擴充功能後才能安裝該腳本。

(我已經安裝了使用者腳本管理器,讓我安裝!)

你需要先安裝一款使用者樣式管理器擴展,比如 Stylus,才能安裝此樣式

你需要先安裝一款使用者樣式管理器擴展,比如 Stylus,才能安裝此樣式

你需要先安裝一款使用者樣式管理器擴展,比如 Stylus,才能安裝此樣式

你需要先安裝一款使用者樣式管理器擴展後才能安裝此樣式

你需要先安裝一款使用者樣式管理器擴展後才能安裝此樣式

你需要先安裝一款使用者樣式管理器擴展後才能安裝此樣式

(我已經安裝了使用者樣式管理器,讓我安裝!)

// ==UserScript==
// @name        Fix LIHKG Embed Iframe
// @namespace   UserScripts
// @match       https://lihkg.com/*
// @grant       none
// @version     0.1.0
// @author      CY Fung
// @license     MIT
// @description To fix LIHKG Embed Iframe
// ==/UserScript==


(() => {

  let rafPromise = null;

  const getRAFPromise = () => rafPromise || (rafPromise = new Promise(resolve => {
    window.requestAnimationFrame(hRes => {
      rafPromise = null;
      resolve(hRes);
    });
  }));

  setInterval(() => {
    if (!document.querySelector('iframe[src*="https://embed.lih.kg/frame?"]:not([dsjfk])')) return;
    for (const s of document.querySelectorAll('iframe[src*="https://embed.lih.kg/frame?"]:not([dsjfk])')) {
      s.setAttribute('dsjfk', '1');


      (async (s) => {

        // await new Promise(r=>setTimeout(r,1));
        await getRAFPromise().then();

        await new Promise(r => {


          let io = new IntersectionObserver(() => {
            io.disconnect();
            io.takeRecords();
            io = null;
            r();
          });
          io.observe(s);

        });

        // await new Promise(r=>setTimeout(r,1));
        await getRAFPromise().then();


        let src = s.getAttribute('src');
        if (!src) return;
        if (!src.startsWith('https://embed.lih.kg/frame?')) return;
        let u = new URL(src);
        let p = u.searchParams;
        if (!p) return;
        let href = p.get('u') || '';
        if (!href.startsWith('https://www.instagram.com/')) return;
        let v = new URL(href);

        let q = `${v.origin}${v.pathname}`;

        // https://lihkg.com/thread/3222489/page/12
        // https://lihkg.com/thread/3485508/page/1
        // https://jsfiddle.net/lunarlogic489/g9a32ws1/
        if (/^https\:\/\/www\.instagram\.com\/(reel|tv|p)\/[-\w]+\/?$/.test(q)) {



          const eUrl = q.replace(/\/$/, '') + '/embed';


          try {
            s.contentWindow.location.replace(eUrl);
          } catch (e) { }




        }


      })(s);

      // https://embed.lih.kg/frame?u=https%3A%2F%2Fwww.instagram.com%2Fp%2FClO8ScgD5uD%2F%3Figshid%3DMDM4ZDc5MmU%3D&h=0b047d4a&theme=dark
      // <iframe src="https://www.instagram.com/p/ClO8ScgD5uD/embed" width="400" height="500" frameborder="0" scrolling="no" allowtransparency="true"></iframe>

    }

  }, 100);
})();