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

发布于 2024-11-27 20:25:55 字数 392 浏览 2 评论 0原文

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

例如,此代码创建一个带有开始下载的 src 的 iframe。

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

如果没有任何库,有什么方法可以阻止 iframe 加载或停止下载?

防止 iframe 注入也是可以接受的。

修改“appendChild()”的行为是个好主意吗?

我正在使用 Opera 11.50 Build 1074。

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

For example, this code creates an iframe with a src that begins a download.

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?

Preventing the iframe injection is also acceptable.

Is it a good idea to modify the behavior of "appendChild()"?

I'm using Opera 11.50 Build 1074.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

入画浅相思 2024-12-04 20:25:55

在所有(如果有)浏览器中,您无法覆盖诸如 appendChild 之类的函数。防止 iframe 被注入的唯一方法是不包含任何进行任意 DOM 注入的 JavaScript 库。

如果您想要阻止插入 iframe 的是您自己的代码,只需添加一些 HTML“清理”功能即可。

You cannot overwrite functions like appendChild, in all (if any) browsers. The only way to prevent iframes from being injected is to not include any JavaScript libraries that do arbitrary DOM injection.

If it's your own code you want to prevent from inserting iframes, simple add some HTML "sanitizing" functionality.

爱,才寂寞 2024-12-04 20:25:55

附加空 iframe :

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

当您想要加载内容时

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

append empty 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');
半透明的墙 2024-12-04 20:25:55

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));

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));
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文