[英]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.