简体   繁体   English

在iframe的文档和/或shadow-root中注入html dom

[英]Injecting html dom inside iframe's document and/or shadow-root

I am injecting iframe onto the webpage using javascript (using chrome extension). 我正在使用javascript(使用chrome扩展名)将iframe注入网页。 Using javascript, I am creating iframe and I can access my html's url, so I though I can fetch the DOM using $.get 使用javascript,我正在创建iframe,我可以访问我的html的url,所以我虽然可以使用$.get获取DOM

var elt = document.createElement('iframe');
elt.id = 'my_iframe';

$.get(chrome.extension.getURL('views/test.html'), function(doc) {

    var container = document.querySelector('#my_iframe');

    console.log(container.documentContent);  // can access
    console.log(container.shadowRoot); // can't access

    // ideally:
    container.shadowRoot($(doc));
    container.contentDocument($(doc));

});

document.getElementsByTagName('body')[0].appendChild(elt);

在此输入图像描述

How can I inject my 'text.html' inside the #shadow-root ? 如何在#shadow-root注入我的'text.html'?

The reason is that, when I use elt.src = url , it doesn't wrap the html content inside #document or #shadow-root , and this results in a bug - (blurry content in iframe when not wrapped in #document or #shadow-root - bug report ) 原因是,当我使用elt.src = url ,它不会将html内容包装在#document#shadow-root ,这会导致错误 - (iframe中未包含在#document内容模糊或#shadow-root - bug报告

If you are just changing the contents of an iframe, you can do it by: 如果您只是更改iframe的内容,可以通过以下方式执行:

const iframe = document.querySelector('#myIframe')
iframe.outerHTML = '<iframe></iframe>' // clears the iframe
iframe.contentDocument.open()
iframe.contentDocument.write(yourHTMLText)
iframe.contentDocument.close()

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

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