禁用警报消息

Edw*_*mas 6 javascript

如何禁用页面上的某些警报并允许其他人?

我尝试使用此代码:

window.alert = function ( text ) {
   console.log(text); 
   if(!text.includes("rambo"))
      alert("rambo"); 
};
Run Code Online (Sandbox Code Playgroud)

这不起作用,因为它再次调用警报而不是警报.

我需要使用javascript警报(不是任何其他库)

Cer*_*nce 15

保存旧的window.alert第一个引用.

const oldAlert = window.alert;
window.alert = function ( text ) {
  console.log(text); 
  if(!text.includes("rambo"))
    oldAlert(text); 
  return true;
};

window.alert('ram');
window.alert('rambo');
Run Code Online (Sandbox Code Playgroud)