js 屏蔽 alert() confirm() prompt() 後的恢復

alert 彈窗太麻煩,js 一句代碼就屏蔽。函數

屏蔽一時爽,恢復火葬場對象

alert = null;blog

alert = console.log;get

我要恢復使用alert彈窗呢?iframe

沒得恢復。it

alert是window對象的一個方法,將alert賦值爲其餘的後,原alert就沒有引用了,就找不到了。console

恢復思路:保存alert方法的引用!引用

因而就有了網上的代碼:方法

var temp = alert; alert=null; alert(1); alert=temp;

可是,經過閱讀W3School的文章,window對象總會初始化,在新加載一個iframe時。im

靈感來了,新建一個iframe,複製引用iframe中的window對象的alert函數。

image

$("iframe").contentWindow alert(1) window.alert = console.log alert(1) $("iframe").contentWindow.alert window.alert = $("iframe").contentWindow.alert alert(1)

$("iframe").contentWindow,找到iframe的window對象。

alert(1),嘗試alert一下。

window.alert = console.log,禁用 alert。

alert(1),再嘗試alert,已經變成控制檯輸出了。

$("iframe").contentWindow.alert,找到iframe裏面的alert。

window.alert = $("iframe").contentWindow.alert,複製引用,恢復window對象的alert方法引用

alert(1),再alert一次,已經恢復了。

image

如圖

相關文章
相關標籤/搜索