// ==UserScript==
// @name          弹窗终结者白名单法
// @author        distance0
// @version       1.01
// ==/UserScript==

var popupTMR = {
	  yesprefs : Components.classes["@mozilla.org/preferences-service;1"].getService(Components.interfaces.nsIPrefService).getBranch("capability.policy."),
	  staItem : document.createElement('statusbarpanel'),	
    img1 : "data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAABAAAAAQCAYAAAAf8/9hAAAB4klEQVQ4jY2TTUtbQRiFzw/osj+guzbr7NttLQXTdBdT3CRYtERQDFGKIjeBiNGkpNLUbARTu+imy34s+7EI0vojCv3URpNce2/uvU8XuZmYSsGBB+bMvOfMzAsjSSpu7WBV6qxubl8Iq1KnuLWDBmOlVBuKC44Rz8O1xxcOCIIASVoqVvue9Se7rNca3IwlTMh4IkU8OUUskSaWSBNPTjGeSCFJnucjSbl8ZXjoYqHCq3efCABJmpxeOHejwZrT85CkhdWNYc38ylp/M0yfyVq4AbRPe7RPe7gBzGQtJMl2ekjS3PLa4AkNSk+fMXZnAj8Az4dsvkyu8IisVSZrhfN8Gc+Hju0iSbNLheENMot5Xr//jO14dGwHpxfg+uD64AeYQrfn47je+YZPzy8jSSe2Q6tzanBcD4KAZkQ0IzLGEV2qNdjY3mMsPkHX8Tk6sQ2DYvaBfUzQWS1JSmdyvPlwQKvj8KvVNUhSVQLrSp/QONBVhQGT9+eQpJ/HNt9/dwz2H3cY8uDyCMZcqjXYrD/nVjzJYdvh62Hb0A0DmhHB7UsjnO2JkqkMbz8e8O2ow5cfx4aRHkQE0ZBwzYQkU7P//QtVyZir6jMIMc8Yu3uPa9EbXI1eH2HvxUvTA1P8j/4LykHjEc9FIcYAAAAASUVORK5CYII=",
		img2 : "data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAABAAAAAQCAYAAAAf8/9hAAAByElEQVQ4jZWTS08TURiG338j++5la91Qq6tpERNaENCCVMZCCA1Om7Sh0BolVrohocJfAF16WSq/wWgQpNc5nXo6l9dFWw/EZFJP8iTn9j3n/RYHGIzc7j6NUoWbO3sjYZQqzO3uc1iPdKGsFiOOazXr+VcjCzzPIwCs5V72a7ZeH3CrXOWtkPZXMqHFGI7OMqTFGdLiDEdnOaHFCACO4xIAUpmSenQ1W+Lxhy/0SALA1PzKP4mGe9J2CAArm9vqTjKd7x8O7Au6wZ5Hml2bZtdmzyMXdIMAYEmbALC8kR+2UGXhzVsG70ToeqTjknqmyFT2BXWjSN0YzDNFOi4prB4BYHEtqxIkVjM8+fiVlnQoLElpe+y5vIa0PQpLsiV+EwAe6c+VYD65QQBoW5JN0fWlYVoEgLlkui8olKvc3jtkMBxhR7qsty1faq0OAWBmaV0liCdSfPfplE0hedns+HLREASA6cfPlGDq4TIB4FfL4nlD+HJWNwkAD+aeqhZ2Kke8HY6yZkqe1Uxffly2CQCTM09UgmgswfefT/mzLvj9ouXLt/MmAUCbTlwVLP73Z4rErgiCdyc5FhjnjcDNkRgLjDN47z7/AEIJCHTVyH3UAAAAAElFTkSuQmCC",
		yessite : " \u88AB\u5141\u8BB8\u5F39\u7A97",
		nosite : " \u5DF2\u88AB\u7981\u6B62\u5F39\u7A97",
		 
		init: function() { 
		var yesprefs = popupTMR.yesprefs,
		    staItem = popupTMR.staItem;  
		    
		try {
		    var pname = yesprefs.getCharPref("policynames");
		    if  (pname.indexOf("AllowedjsPopupSites")== -1 ){
		    yesprefs.setCharPref("policynames",pname + " AllowedjsPopupSites");
		     }
		    } catch (ex) {
	      yesprefs.setCharPref("policynames","AllowedjsPopupSites"); 
	       	}
	      yesprefs.setCharPref("default.Window.open","noAccess");
	      yesprefs.setCharPref("AllowedjsPopupSites.Window.open","allAccess");		
          
		    try {
	      yesprefs.getCharPref("AllowedjsPopupSites.sites");
	       } catch (ex) {
		    yesprefs.setCharPref("AllowedjsPopupSites.sites","http://www.google.cn http://mp3.baidu.com");
	      }    	
		staItem.setAttribute("image",popupTMR.img1);
		staItem.setAttribute("id", "popupTMRstatus");
    staItem.setAttribute("class", "statusbarpanel-iconic");
    staItem.setAttribute("onclick", "popupTMR.yesdo(event)");
    
    document.getElementById("status-bar").appendChild(staItem);
    gBrowser.tabContainer.addEventListener("TabSelect", popupTMR.yesupdate, false);
   // document.getElementById("status-bar").addEventListener("mouseover", popupTMR.yesupdate, false); 
    gBrowser.addProgressListener(popupTMR);         
    },
  
   yesdo : function(event){ 
    var staItem = popupTMR.staItem,
    	  yesprefs = popupTMR.yesprefs,
    	  img1 = popupTMR.img1,
  	    img2 = popupTMR.img2,
  	    yessite = popupTMR.yessite,
  	    nosite = popupTMR.nosite,
        site = gBrowser.currentURI.prePath,
        host = gBrowser.currentURI.host,
        siteString = yesprefs.getCharPref("AllowedjsPopupSites.sites");
     if(site.indexOf("http")== 0)
       { if ( event.button == 0 ){
       	if (siteString.indexOf(site)!=-1){
       		yesprefs.setCharPref("AllowedjsPopupSites.sites",siteString.replace(" " + site ,""));
       	  staItem.setAttribute("tooltiptext", site + nosite);
       	  staItem.setAttribute("image",img1);
       	  	}
       	 else	if (siteString.indexOf(" " + site.substring(site.indexOf('.') + 1, site.length))!=-1 || siteString.indexOf(" " + host)!=-1){
       	  	event.preventDefault();
       	  	}
       	 else {   	  	
       	  	yesprefs.setCharPref("AllowedjsPopupSites.sites", siteString + " " + site);
       	  	staItem.setAttribute("tooltiptext", site + yessite);
       	  	staItem.setAttribute("image",img2);
       	  	}
       	 } 	
          if ( event.button == 2 ){
          if (siteString.indexOf(site)!=-1 ){
            event.preventDefault();
       	  	}
       	 else	if (siteString.indexOf(" " + site.substring(site.indexOf('.') + 1, site.length))!=-1){
       	  event.preventDefault();
       	  yesprefs.setCharPref("AllowedjsPopupSites.sites",siteString.replace(" " + site.substring(site.indexOf('.') + 1, site.length) ,""));
       	  staItem.setAttribute("tooltiptext", site + nosite);
       	  staItem.setAttribute("image",img1);
       	  	}
       	 else	if (siteString.indexOf(" " + host)!=-1){
       	  event.preventDefault();
       	  yesprefs.setCharPref("AllowedjsPopupSites.sites",siteString.replace(" " + host ,""));
       	  staItem.setAttribute("tooltiptext", site + nosite);
       	  staItem.setAttribute("image",img1);
       	  	} 	
       	 else	if (host.substring(host.indexOf('.') + 1, host.length).indexOf('.')== -1 ){
          	event.preventDefault();       	  
       	  	yesprefs.setCharPref("AllowedjsPopupSites.sites", siteString + " " + host);
       	  	staItem.setAttribute("tooltiptext", host + yessite);
       	  	staItem.setAttribute("image",img2);
       	  	}  	
   	
       	 else {            	
         	  event.preventDefault();
         	  site = site.substring(site.indexOf('.') + 1, site.length);         	  
       	  	yesprefs.setCharPref("AllowedjsPopupSites.sites", siteString + " " + site);
       	  	staItem.setAttribute("tooltiptext", site + yessite);
       	  	staItem.setAttribute("image",img2);
       	  	 }
           }
         if ( event.button == 1 ){
          newtext =  yesprefs.getCharPref("AllowedjsPopupSites.sites").replace(/ /g, "<br>");
          newtab = gBrowser.addTab("chrome://global/skin/commonDialog.css"); 
          gBrowser.selectedTab = newtab ;
          newtab.addEventListener("load", function () {  
          newtab.setAttribute('label', '\u5F39\u7A97\u767D\u540D\u5355'); 
          gBrowser.userTypedValue = '\u5141\u8BB8\u5F39\u7A97\u7684\u7AD9\u70B9';
          newtab.image = img2;
          document.getElementById("page-proxy-favicon").src = img2;
 
          var newpop = content.document.createElement('div'); 
          sWidth=screen.width, 
          sHeight=screen.height; 
          newpop.style.top="0"; 
          newpop.style.left="0"; 
          newpop.style.height = sHeight;  
          newpop.style.width =  sWidth;  
          newpop.style.position = 'absolute'; 
          newpop.style.background = '#cccccc'; 
          newpop.style.zIndex = "10000";  
      
          content.document.body.appendChild(newpop); 
          var msgObj=content.document.createElement("div")  
          msgObj.setAttribute("align","center");  
          msgObj.style.background="white";  
          msgObj.style.border="1px solid #c51100";  
          msgObj.style.position = "absolute";  
          msgObj.style.left = sWidth/2 -200;  
          msgObj.style.top = sHeight/2 -200;  
          msgObj.style.font="12px/1.6em Verdana,Arial";  
          msgObj.style.width =  "300px";  
          msgObj.style.height ="auto";  
          msgObj.style.textAlign = "left"; 
          msgObj.style.paddingLeft=  "130px";
          msgObj.style.lineHeight ="18px";  
          msgObj.style.zIndex = "10002";  
          msgObj.innerHTML=newtext;  
     
          newpop.appendChild(msgObj);     
         }, true); 
       }
     } 
   },
   
  onLocationChange: function()  
  {  popupTMR.yesupdate(); },    
	onStateChange: function() {},
  onProgressChange: function() {},
	onStatusChange: function() {},
	onSecurityChange: function() {},

  yesupdate : function(){ 
  	 var staItem = popupTMR.staItem, 
  	     img1 = popupTMR.img1,
  	     img2 = popupTMR.img2,
  	     yessite = popupTMR.yessite,
  	     nosite = popupTMR.nosite;
     if (gBrowser.currentURI.spec.indexOf("http")!= 0){
          staItem.setAttribute("tooltiptext", "\u65E0\u6548\u5730\u5740");
          staItem.setAttribute("image",img1);}	
        else {
    	  var yesprefs = popupTMR.yesprefs,
        site = gBrowser.currentURI.prePath,
        host = gBrowser.currentURI.host,
        siteString = yesprefs.getCharPref("AllowedjsPopupSites.sites");       
    if(site)
       { if (siteString.indexOf(site)!=-1){
       	  	staItem.setAttribute("tooltiptext", site + yessite);
       	  	staItem.setAttribute("image",img2);
       	  	}
       	 else	if (siteString.indexOf(" " + site.substring(site.indexOf('.') + 1, site.length))!=-1 ){
       	  	staItem.setAttribute("tooltiptext", site.substring(site.indexOf('.') + 1, site.length) + yessite);
       	  	staItem.setAttribute("image",img2);
       	  	}
       	 else	if (siteString.indexOf(" " + host)!=-1){      	  
       	  	staItem.setAttribute("tooltiptext", host + yessite);
       	  	staItem.setAttribute("image",img2);
       	  	}   	
       	 else {
       	  	staItem.setAttribute("tooltiptext", site + nosite);
       	  	staItem.setAttribute("image",img1);
       	  	}	

        }
      }	
    } 
}
popupTMR.init(); 