iframe CSS注入加载优先级

发布于 2025-02-12 11:11:00 字数 1328 浏览 1 评论 0原文

我试图将一些CSS注入一个iframe,而且效果很好,除了在新的CSS生效之前有一个延迟,因此您将获得一个加载一个主题然后更改颜色的页面。它只能看到一秒钟,但很烦人。有什么方法可以将iFrame渲染延迟到应用CSS?我在CSS之后添加了一个脚本,但似乎由于我将其注入DOM,因此会渲染然后重新租赁。我已经尝试使用ReadyState Interactive完成在加载前将其获取,但没有运气,也不确定如何延迟它。还尝试了addeventListener(“ load”),但在加载射击之前似乎会发生变化。

const frame_doc = document.getElementById("iframe").contentDocument;
function setFrameClass(light) {
    if (light) {
        frame_doc.body.classList.remove("dark")
    } else {
        frame_doc.body.classList.add("dark")
    }
}
themeSwitch.addEventListener('change', function () {
    setFrameClass(this.checked, frame_doc.body)
});
frame_doc.onreadystatechange = function () {
    if (frame_doc.readyState === 'interactive') {
        let link = frame_doc.createElement('link');
        link.rel = 'stylesheet';
        link.type = 'text/css';
        link.href = new URL('rootstatic/css/dark.css', window.location.origin).href;
        link.media = 'all';
        frame_doc.head.appendChild(link);
        let s = frame_doc.createElement("script");
        s.type = "text/javascript";
        s.src = new URL('rootstatic/js/dark-mode.js', window.location.origin).href;
        frame_doc.head.appendChild(s);
        setFrameClass(themeSwitch.checked)
    }
}

I'm trying to inject some CSS into an iFrame and it works fine except there is a delay before the new CSS takes effect, so you get a page that loads one theme and then changes color. It's only visible for a second, but it's annoying. Is there any way to delay the iFrame render until that CSS is applied? I add a script after the CSS, but seems that since I'm injecting this into the DOM, it renders then re-renders. I've tried using readyState interactive and complete to get it before load, but no luck and not sure how to delay it. Also tried addEventListener("load"), but it appears to change before the load fires.

const frame_doc = document.getElementById("iframe").contentDocument;
function setFrameClass(light) {
    if (light) {
        frame_doc.body.classList.remove("dark")
    } else {
        frame_doc.body.classList.add("dark")
    }
}
themeSwitch.addEventListener('change', function () {
    setFrameClass(this.checked, frame_doc.body)
});
frame_doc.onreadystatechange = function () {
    if (frame_doc.readyState === 'interactive') {
        let link = frame_doc.createElement('link');
        link.rel = 'stylesheet';
        link.type = 'text/css';
        link.href = new URL('rootstatic/css/dark.css', window.location.origin).href;
        link.media = 'all';
        frame_doc.head.appendChild(link);
        let s = frame_doc.createElement("script");
        s.type = "text/javascript";
        s.src = new URL('rootstatic/js/dark-mode.js', window.location.origin).href;
        frame_doc.head.appendChild(s);
        setFrameClass(themeSwitch.checked)
    }
}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。
列表为空,暂无数据
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文