简体   繁体   English

当注入DOM时如何防止iframe加载?

[英]How to prevent iframe from loading when injected into the DOM?

How to prevent iframe from loading when injected into the DOM? 当注入DOM时如何防止iframe加载?

For example, this code creates an iframe with a src that begins a download. 例如,此代码使用开始下载的src创建一个iframe。

f = B.Node.create('<iframe class="offscreen" role="presentation" tabindex="-1" id="' + d + '" src="' + Z + Y + '">');
F("body").appendChild(f);

Without any libraries, what are ways to prevent the iframe from loading or to stop the download? 没有任何库,有什么方法可以阻止iframe加载或停止下载?

Preventing the iframe injection is also acceptable. 阻止iframe注入也是可以接受的。

Is it a good idea to modify the behavior of "appendChild()"? 修改“ appendChild()”的行为是一个好主意吗?

I'm using Opera 11.50 Build 1074. 我正在使用Opera 11.50 Build 1074。

You cannot overwrite functions like appendChild , in all (if any) browsers. 您不能在所有(如果有)浏览器中覆盖诸如appendChild功能。 The only way to prevent iframes from being injected is to not include any JavaScript libraries that do arbitrary DOM injection. 防止注入iframe的唯一方法是不包括任何执行任意DOM注入的JavaScript库。

If it's your own code you want to prevent from inserting iframes, simple add some HTML "sanitizing" functionality. 如果要防止插入iframe是您自己的代码,只需添加一些HTML“清除”功能即可。

append empty iframe 附加空的iframe

document.body.appendChild(document.createElement('iframe').setAttribute('id', 'myiFrame'));

when you want to load the content: 当您要加载内容时:

document.getElementById('myiFrame').setAttribute('src', 'http://blah.com/blah.htm');

https://gist.github.com/1126767/ https://gist.github.com/1126767/

// ==UserScript==
// @name Enhance Yahoo! Mail
// @author XP1 (https://github.com/XP1/)
// @namespace https://gist.github.com/1126767/
// @version 1.0
// @description In Yahoo! Mail, opens the download iframe in a new window so that the attachment can be opened if the file type is associated with the Opera browser.
// @include http*://mail.yahoo.*/*
// @include http*://*.mail.yahoo.*/*
// @include http*://mail.yimg.*/*
// @include http*://*.mail.yimg.*/*
// @include http*://yahooapis.*/*
// @include http*://*.yahooapis.*/*
// ==/UserScript==

/*jslint browser: true, vars: true, white: true, maxerr: 50, indent: 4 */
(function (topWindow)
{
    "use strict";

    if (window.self === topWindow)
    {
        var disableDownloadIframe = function ()
        {
            topWindow.addEventListener("DOMNodeInserted", function (event)
            {
                var sourceElement = event.srcElement;
                if (sourceElement.tagName.toLowerCase() === "iframe" && sourceElement.hasAttribute("id") && sourceElement.getAttribute("id").indexOf("#dlFrame") !== -1)
                {
                    var downloadLink = sourceElement.getAttribute("src");
                    sourceElement.parentNode.removeChild(sourceElement);

                    window.open(downloadLink);
                }
            }, false);
        };

        disableDownloadIframe.call(this);
    }
}(window.top));

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

相关问题 如何在DOM中移动iframe时阻止iframe重新加载 - How to prevent an iframe from reloading when moving it in the DOM 阻止iframe在手机上加载 - Prevent iframe from loading on mobile 从扩展中动态注入的iframe访问当前标签页的DOM,并更新iframe DOM - Access current tab's DOM from dynamically injected Iframe from extension & update iframe DOM 将iframe添加到文档中时,如何防止其重新加载? - How do I prevent an Iframe from re-loading when adding it to the document? 如何防止在对象完全加载到$ scope中之前加载DOM? - How to prevent DOM from loading until object is fully loaded into $scope? 如何在加载DOM之前开始加载iframe - How to start loading an iframe before the DOM is loaded 在尝试选择注入appendChild()的节点时,如何防止Range.selectNode()选择过多的DOM? - How can I prevent Range.selectNode() selecting too much of the DOM when attempting to select a node injected with appendChild()? 电子:防止<style> tags in DOM from loading - Electron: Prevent <style> tags in DOM from loading 阻止模式视频iframe加载先前的视频iframe - Prevent modal video iframe from loading previous video iframe 如何知道 iframe 何时完成加载 DOM,但尚未加载所有图像? - how to know when an iframe has finished loading the DOM, but not yet all images?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM