2011-08-02 23 views
1

如何防止iframe在注入DOM時加載?如何防止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生成1074

+0

你爲什麼要注入的IFrame,如果你不希望它有內容嗎? – GolezTrol

+0

@GolezTrol,我沒有注入iframe。別人是。我無法控制Yahoo! Mail在Yahoo!的服務器上的JavaScript。雅虎郵件注入iframe。 – XP1

+0

您是否可以控制文檔中該腳本的加載,還是加載腳本的第三方內容? – Jacob

回答

1

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

這很酷,但它不會在IE中工作。 – Jacob

1

您不能覆蓋功能,如appendChild,在所有的(如果有的話)的瀏覽器。防止iframe被注入的唯一方法是不包含任何執行任意DOM注入的JavaScript庫。

如果它是您自己的代碼,您想防止插入iframe,只需添加一些HTML「消毒」功能。

1

追加空的iframe

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

當你要加載的內容:

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

這就是我正在做的事情,當我用'src'創建它時,iframe會加載一個空的和一個空的。也許它曾經工作在過去... – nonzaprej