To fix LIHKG Embed Iframe
当前为
// ==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);
})();