Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/363.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript 关闭已注入的iframe_Javascript_Iframe_Google Chrome Extension - Fatal编程技术网

Javascript 关闭已注入的iframe

Javascript 关闭已注入的iframe,javascript,iframe,google-chrome-extension,Javascript,Iframe,Google Chrome Extension,我有一个从chrome扩展运行的内容脚本。 此脚本将iframe注入当前页面的主体。 我希望能够从iframe中关闭iframe。 我该怎么做? 当我在网上搜索这个问题时,几乎每个解决方案都使用window.parent.document属性,由于某种原因,在我的案例中没有定义该属性。有什么想法吗 编辑-代码示例: 在iframe的HTML中: <script type="text/javascript"> function frameClose() {

我有一个从chrome扩展运行的内容脚本。 此脚本将
iframe
注入当前页面的主体。 我希望能够从
iframe
中关闭
iframe
。 我该怎么做? 当我在网上搜索这个问题时,几乎每个解决方案都使用
window.parent.document
属性,由于某种原因,在我的案例中没有定义该属性。有什么想法吗

编辑-代码示例:

iframe
的HTML中:

<script type="text/javascript">
        function frameClose() {
            var windowFrames = window.parent.frames;
            for (var i = 0; i < windowFrames.length; i++) {
                var aFrame = windowFrames[i];
                if (aFrame.name == 'myFrame') {
                    alert('in frame');
                    // WHAT TO DO HERE?
                    // window.parent.document is undefined
                    // aFrame.parentNode.removeChild(aFrame); - THIS DOES NOT WORK ALSO
                    break;
                }
            }
        }
    </script>
在PushIFrame.js中,我有:

让内容脚本(比如
PushIframe.js
)将
消息
事件绑定到主框架。然后,每当您想要隐藏iframe时,调用通知主帧。此消息由内容脚本接收,从中可以隐藏帧(如函数
pushIframe
中所定义)

//PushIframe.js:
addEventListener(“消息”,功能(ev){
如果(ev.data==='closeIframe'){
pushIframe(文档);//您的代码
}
});
//Iframe:
函数frameClose(){
parent.postMessage('closeIframe','*');
}

window.parent.document未定义很奇怪。如何注入Iframe?请为您的问题添加一些上下文:如果window.parent.document未定义,我们需要知道原因。请给我们看一些代码。在我的扩展中,我有一个chrome.browserAction.onClicked.addListener。单击扩展名时,我有chrome.tabs.executeScript,它创建iframe并设置其src属性。我需要从iframe的html中关闭它,这样我就有了一个覆盖所有框架(window.parent.frames)的函数。当我到达框架时,我试图将其从window.parent.document中删除,但未定义。请使用iframe中的
parent.postMessage
通知内容脚本,内容脚本将关闭框架。@RobW-请将代码示例发布到解决方案中好吗?
chrome.browserAction.onClicked.addListener(function(tab) {

    chrome.tabs.executeScript(null, {
        file : "/js/PushIFrame.js"
    }, function() {
        if (chrome.extension.lastError) {
        }
    });
});
chrome.extension.sendMessage({
    action: "pushFrame",
    source: pushIframe(document)
});

function pushIframe(document) {
    var existingFrame = document.getElementById('bmarkFrame');
    if (existingFrame == null) {
        var temp = document.createElement('iframe');
        temp.id = 'myFrame';
        temp.name = 'myFrame';
        temp.setAttribute('scrolling', 'no');
        temp.setAttribute('allowtransparency', 'true');
        temp.style.border = 'none';
        temp.style.height = '100%';
        temp.style.width = '100%';
        temp.style.position = 'fixed';
        temp.style.zIndex = 99999999;
        temp.style.top = 0;
        temp.style.left = 0;
        temp.style.display = 'block';
        temp.src = 'https://www.mysite.com/';

        document.body.appendChild(temp);
    }
    else {
        existingFrame.style.display = 'block';
    }
}
// PushIframe.js:
addEventListener('message', function(ev) {
    if (ev.data === 'closeIframe') {
        pushIframe(document); // Your code
    }
});

// Iframe:
<script>
function frameClose() {
    parent.postMessage('closeIframe', '*');
}
</script>