简体   繁体   English

如何使用Firefox扩展将iframe添加到内容/ DOM?

[英]How to add an iframe to the content / DOM using a firefox extension?

I'm trying to add an iframe to the content / DOM of a webpage using a firefox extension. 我正在尝试使用Firefox扩展将iframe添加到网页的内容/ DOM。

Here is my code: 这是我的代码:

const XUL = Namespace("xul", "http://www.mozilla.org/keymaster/gatekeeper/there.is.only.xul");

function addIframe(src) {
    // Create the script node
    let iframe = document.createElementNS(XUL, "iframe");
    iframe.setAttribute("src", src);

        window.content.appendChild(iframe);
}

It's a modified version from this answer: Execute JS from Firefox extension 这是此答案的修改版本: 从Firefox扩展中执行JS

I replaced this line: 我替换了这一行:

// Inject it into the top-level element of the document
document.documentElement.appendChild(script);

With this line: 用这行:

window.content.appendChild(iframe);

Because it was inserting the iframe into the chrome of the browser and not into the content area DOM. 因为它是将iframe插入浏览器的镶边中,而不是内容区域DOM中。 What is the right command to insert it into the page? 将其插入页面的正确命令是什么? (My command is not working) (我的命令不起作用)

And the correct answers goes to... me! 正确的答案是……我!

const XUL = Namespace("xul", "http://www.mozilla.org/keymaster/gatekeeper/there.is.only.xul");

function addIframe(src) {
    // Create the script node
    let iframe = document.createElementNS(XUL, "iframe");
    iframe.setAttribute("src", src);

        window._content.document.body.appendChild(iframe);
        return;
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM