iyaozhen
V2EX  ›  问与答

Chrome 扩展如何屏蔽(绕过)页面的弹窗( alert)

  •  1
     
  •   iyaozhen · Mar 26, 2015 · 6032 views
    This topic created in 4150 days ago, the information mentioned may be changed or developed.

    我有一个扩展实现的很简单功能,就是点击页面上的一个选择按钮,然后再点击提交按钮。

    但原本的页面在提交时偶尔会报错弹窗(alert),然后就会阻塞js的执行(我需要刷新页面再执行上述相同的操作)。

    核心代码:

    var button = $("button")[0];
    var submit = $("button")[1];
    if ($(button).data('bind') == "hooked_btn:fast_remove") {
    button.click();
    }
    else{
    console.log("筛选按钮选择出错");
    }
    var list = $(".glyphicon-chevron-left").length;
    if(list > 0){
    if ($(submit).data('bind') == "click:confirm_remove") {
    submit.click();
    setTimeout(function(){
    location.reload();
    }, 200);
    }
    else{
    console.log("提交按钮选择出错");
    }
    }

    10 replies    2015-03-28 11:39:47 +08:00
    bigdude
        1
    bigdude  
       Mar 26, 2015
    window.alert = function(){};

    覆盖掉原方法
    lmaq
        2
    lmaq  
       Mar 26, 2015
    顶楼上~
    iyaozhen
        3
    iyaozhen  
    OP
       Mar 27, 2015
    @bigdude 这个方法我知道,但不行,chrome 扩展的 js 环境和页面 js 环境是隔离的。
    @lmaq
    lmaq
        4
    lmaq  
       Mar 27, 2015
    @iyaozhen 不要把脚本写到background,chrome扩展使用content_scripts
    iyaozhen
        5
    iyaozhen  
    OP
       Mar 27, 2015
    @lmaq 是 content_scripts,不行。难道是我姿势不对,我再试试
    lmaq
        6
    lmaq  
       Mar 27, 2015
    @iyaozhen 我这测试没有问题啊
    iyaozhen
        7
    iyaozhen  
    OP
       Mar 27, 2015
    iyaozhen
        8
    iyaozhen  
    OP
       Mar 27, 2015
    lmaq
        9
    lmaq  
       Mar 28, 2015
    两个 content_scripts
    控制content script注入的时机。可以是document_start, document_end或者document_idle

    第一个 content_scripts 先载入 alert
    第二个再载入你要的代码
    iyaozhen
        10
    iyaozhen  
    OP
       Mar 28, 2015
    @lmaq 还是不行。找到一些说明:
    当然,content scripts也有一些限制,它们不能做的事情包括 :
    不能访问web页面或其它content script中定义的函数和变量

    与嵌入的页面通信
    尽管content script的执行环境与所在的页面是隔离的,但它们还是共享了页面的DOM。 如果页面需要与content script通信(或者通过content script与扩展通信), 就必须通过这个共享的DOM。
    About   ·   Help   ·   Advertise   ·   Blog   ·   API   ·   FAQ   ·   Solana   ·   5746 Online   Highest 6679   ·     Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 · 27ms · UTC 01:58 · PVG 09:58 · LAX 18:58 · JFK 21:58
    ♥ Do have faith in what you're doing.