本篇介紹的是如何使用userscript來實現關閉網頁上的alert()函式,將:

 unsafeWindow.alert=function() {}; 

寫入userscript即可實現,但如果alert是寫在body中的直接執行原始碼(非條件產生),就不能關閉,這是由於這種型態的alert()會在執行Greasemonkey前執行alert函式。

可以連結這個網站做測試:http://jsbin.com/ajeqe4/6 .

原始碼 (http://jsbin.com/ajeqe4/6/edit), 將會看到三個alert(). Greasemonkey只能擋掉寫成function的alert(),對於直接執行原始碼是無法擋掉的。

 


 

要完全解決這個問題,可以改裝firefox的 Scriptish 這個套件,如此

unsafeWindow.alert=function() {}; 就可以擋掉所有的alert()了

裝完 Scriptish 後,try it:

// ==UserScript==
// @id alert-killer-test@erikvold.com
// @name Overwrite Alert
// @description Overwrites alert()
// @include *
// @run-at document-start
// ==/UserScript==

unsafeWindow.alert=function() {};

 

來源:http://stackoverflow.com/questions/4656702/how-to-override-alert-function

arrow
arrow
    全站熱搜

    bshadow 發表在 痞客邦 留言(0) 人氣()