代码之家  ›  专栏  ›  技术社区  ›  Kristoffer Bohmann

在弹出警报框之前将其移除[重复]

  •  3
  • Kristoffer Bohmann  · 技术社区  · 15 年前

    HTML:

    <!DOCTYPE html>
    <html>
    <body>
    
    <script>alert("Remove this Alert box with GreaseMonkey.");</script>
    
    <p>Hello world</p>
    
    </body>
    </html>
    

    GreaseMonkey脚本(目前没有jQuery部分):

    // ==UserScript==
    // @name        Remove Alert box
    // @include     http://www.example.com/alert_remove/
    // ==/UserScript==
    
    var node = document.getElementsByTagName('alert');
    node.parentNode.removeChild(node);
    
    if(window.alert) {
        alert("ALERT DETECTED");  // No response.
    }
    

    我不认为使用jQuery可以解决这个问题,因为jQuery代码只有在页面加载且警报可见时才会触发($(document.ready)。


    如果警报出现,我是否可以发送按键(13=回车)来处理警报?

    谢谢

    4 回复  |  直到 15 年前
        1
  •  1
  •   metrobalderas    15 年前

        2
  •  7
  •   Ricket    15 年前

    Greasemonkey在页面加载之前不会运行,因此这在Greasemonkey中是不可能的。如果你真的下定决心了,你可以设置一个代理(比如 squid )使用与本节中使用的方法类似的方法 Upside-Down-Ternet

    如果您可以在其他代码调用之前运行自己的JavaScript alert ,则您将希望执行以下操作:

    window.backupAlert = window.alert;
    window.alert = function () {return true};
    

    (测试工作)

    然后您可以使用 backupAlert 取代 在你自己的脚本中,但是其他脚本 警觉的 “仍然存在”将不会显示弹出窗口。

    功能 窗口对象内部;它不是文档中的标记或DOM对象。

    不,不能用JavaScript发送按键。这在全球范围内是正确的;您可以触发处理按键事件的函数,但不能发送操作系统单击或按键事件。

        3
  •  1
  •   Maxym    15 年前

    var node=document.getElementsByTagName('alert');
    node.parentNode.removeChild(节点);

    • 它根本不起作用,因为“alert”不是节点,而“script”是节点。如果在显示警报之前执行了GreaseMonkey脚本,则必须执行以下操作:

    var node=document.getElementsByTagName('script')[0];

    var result=str.replace(/alert(“[\w\s.]*”);/,“”);
    node.firstChild.textContent=结果;

    希望能有帮助

        4
  •  0
  •   Senica Gonzalez    15 年前