导航到列入黑名单的URL并取消它们

时间:2013-07-22 01:01:09

标签: javascript browser firefox-addon message content-script

我需要编写一个Firefox扩展程序来创建URL的黑名单和白名单,并检查以确保用户想要在用户尝试这样做时导航到它们。我正在使用我附加到每个页面的主脚本和内容脚本(使用PageMod);我使用jQuery将一个监听器附加到使用window.onbeforeunload执行函数的每个链接(带有标记“a”)。我有两个问题:

  1. 如何提示/询问用户他们是否确实想要访问该网站?
  2. 如果用户决定不停止浏览器导航到网站?
  3. 现在我的代码在两个脚本之间传递消息以实现我的目标;据我所知,我只能在内容脚本中使用“文档”,并将黑名单/白名单保存在主脚本中。我使用简单存储来保存我的列表,并使用端口模块在脚本之间传递消息。

    对于问题1,我尝试使用确认(消息)来获得用户的肯定/否定回复,但弹出窗口没有显示或显示一瞬间然后自动回答负面响应。当我查看控制台的错误消息时,我看到“用户提示中止”错误。

    对于问题2,我已经尝试通过将click事件传递给函数来使用event.preventDefault()(我认为这很有用)。有一个更好的方法吗?我见过人们使用window.location =“”等等来做这件事。

    无论如何,代码如下:

    MAIN.JS

    var ss = require("sdk/simple-storage");
    
    exports.main = function() {
      if (!ss.storage.blacklist) {
        ss.storage.blacklist = [];}
      if (!ss.storage.whitelist) {
        ss.storage.whitelist = [];}
      var data = require("sdk/self").data;
      var pageMod = require("sdk/page-mod");
        pageMod.PageMod({
          include: "*",          
          contentScriptFile: [data.url("jquery-1.10.2.min.js"),data.url("secChk.js")],
          onAttach: function(worker) {
    
            function whiteCNTD(str) {
              for (var index = 0; index < ss.storage.whitelist.length; index++) {
                if (ss.storage.whitelist[index] == str) {
                  return index;
                }
              } 
              return -1;
            }
    
            function blackCNTD(str) {
              for (var index = 0; index < ss.storage.blacklist.length; index++) {
                if (ss.storage.blacklist[index] == str) {
                  return index;
                }
              } 
              return -1;
            }
    
            function checkLists(URL) {
              if (whiteCNTD(URL) == -1) {
                if (blackCNTD(URL) != -1) {
                  var bool = false;
                  worker.port.emit("navq", "Do you want to go to this link and add it to the whitelist?");
                  worker.port.on("yes", function() {
                    bool = true;
                  });
                  worker.port.on("no", function() {
                    bool = false;
                  });
                if (bool == true) {
                  ss.storage.blacklist.splice(index, 1);
                  ss.storage.whitelist.push(URL);
                  return true;
                }
                else {
                  return false;
                }
              }
              else {
                var bool = false;
                worker.port.emit("safeq", "Is this a safe site?");
                worker.port.on("yes", function() {
                  bool = true;
                });
                worker.port.on("no", function() {
                  bool = false;
                });
                if (bool == true) {
                  ss.storage.whitelist.push(URL);
                  return true;
                }
              else {
                ss.storage.blacklist.push(URL);
                return false;
              }
            }
          }
          return true;
        }
    
        worker.port.on("newURL", function(URL) {
          var s = "";
          s = URL;
          if (checkLists(s)) {
            worker.port.emit("good", s);
          } else if (!checkLists(s)) {
            worker.port.emit("bad", s);
            }
          });
        }
      });
    }
    

    SECCHK.JS

    //Check if the site is a bad site whenever a link is clicked
    $("a").click(function(event) {
      window.onbeforeunload = function() {
        self.port.on("navq", function(message) {
          var r = confirm("Do you want to go to this link and add it to the whitelist?");
          if (r == true) {
            self.port.emit("yes", message);
          } else if (r == false) {
            self.port.emit("no", message);
          }
        });
        self.port.on("safeq", function(message) {
          var r = confirm("Is this a safe site?");
          if (r == true) {
            self.port.emit("yes", temp);
          } else if (r == false) {
            self.port.emit("no", temp);
          }
        });
        link = document.activeElement.href;
        self.port.emit("newURL", link);
        self.port.on("good", function(message) {
          return true;
        });
        self.port.on("bad", function(message) {
          return false;
        });
      }   
    });
    

0 个答案:

没有答案