gpt4 book ai didi

javascript - 使用 chrome 扩展程序如何将 html 添加到页面 body 标签的正下方?

转载 作者:塔克拉玛干 更新时间:2023-11-02 21:05:34 27 4
gpt4 key购买 nike

我想要一个 chrome 扩展,在某些网站的顶部添加一个小栏。如果您已登录,则类似于您在本网站顶部看到的栏。

根据我所阅读的内容,我需要使用内容脚本来执行此操作,并且我尝试了各种方法。目前我有一个名为 content.js 的文件,其中包含以下内容

var iframe = document.createElement("iframe");    
iframe.src = chrome.extension.getURL("iframe.html");
document.body.appendChild(iframe);

iframe.html 文件就是这个

<button id="button">Click me!</button>
<script>
document.getElementById("button").addEventListener("click", function() {
top.postMessage("clicked!", "*");
}, false);
</script>

这会在页面底部插入此代码,我的问题是我想将它放在顶部。我该怎么做?

最佳答案

您可以使用 insertBeforefirstchild 将其添加到顶部,如下所示:

document.body.insertBefore(iframe, document.body.firstChild);

关于javascript - 使用 chrome 扩展程序如何将 html 添加到页面 body 标签的正下方?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8986752/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com