javascript - Navigating to Blacklisted URL's and Canceling Them -
i need write firefox extension creates blacklist , whitelist of url's, , checks make sure user wants navigate them whenever user attempts so. i'm doing using main script , content script attach every page (using pagemod); attached listener using jquery every link (with tag "a") executes function using window.onbeforeunload. have 2 questions:
- how prompt/ask user if did want go site?
- how stop browser navigating site if user decided not to?
right code passes messages between 2 scripts in order accomplish goal; far can tell, can use "document" in content script, , save blacklist/whitelist in main script. i'm using simple-storage save lists, , port module pass messages between scripts.
for question 1, i've attempted using confirm(message) positive/negative response user, popup either doesn't show or shows split second gets automatically answered negative response. when in console's error messages, see "prompt aborted user" error.
for question 2, i've tried using event.preventdefault() passing click event function (this worked, think). there better way this? i've seen people using window.location = "", et cetera this.
anyways, code below:
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) { (var index = 0; index < ss.storage.whitelist.length; index++) { if (ss.storage.whitelist[index] == str) { return index; } } return -1; } function blackcntd(str) { (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 want go link , add 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 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 site bad site whenever link clicked $("a").click(function(event) { window.onbeforeunload = function() { self.port.on("navq", function(message) { var r = confirm("do want go link , add 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 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; }); } });
Comments
Post a Comment