home *** CD-ROM | disk | FTP | other *** search
/ Personal Computer World 2006 March / PCWMAR06.iso / Software / FromTheMag / Noscript / noscript-1.1.3.5-fx+mz.xpi / chrome / noscript.jar / content / noscript / noscriptOptions.js < prev    next >
Encoding:
JavaScript  |  2005-12-08  |  7.4 KB  |  260 lines

  1. /***** BEGIN LICENSE BLOCK *****
  2.  
  3. NoScript - a Firefox extension for whitelist driven safe JavaScript execution
  4. Copyright (C) 2004-2005 Giorgio Maone - g.maone@informaction.com
  5.  
  6. This program is free software; you can redistribute it and/or modify
  7. it under the terms of the GNU General Public License as published by
  8. the Free Software Foundation; either version 2 of the License, or
  9. (at your option) any later version.
  10.  
  11. This program is distributed in the hope that it will be useful,
  12. but WITHOUT ANY WARRANTY; without even the implied warranty of
  13. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  14. GNU General Public License for more details.
  15.  
  16. You should have received a copy of the GNU General Public License
  17. along with this program; if not, write to the Free Software
  18. Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
  19.  
  20. ***** END LICENSE BLOCK *****/
  21.  
  22. const g_serv=noscriptUtil.service;
  23. var g_urlList=null;
  24. var g_jsglobal=null;
  25. var g_urlText=null;
  26. var g_addButton=null;
  27. var g_removeButton=null;
  28. var g_dom2=/^(?:http[s]?|file):\/\/([^\.\?\/#,;:\\\@]+(:?\.[^\.\?\/#,;:\\\@]+$|$))/; // 2nd level domain hack
  29. var g_policySites=null;
  30.  
  31. function nso_init() {
  32.   if(g_serv.uninstalling) { // this should never happen! 
  33.     window.close();
  34.     return;
  35.   }
  36.   g_urlText=document.getElementById("urlText");
  37.   g_urlList=document.getElementById("urlList");
  38.   g_jsglobal=document.getElementById("jsglobal");
  39.   g_addButton=document.getElementById("addButton");
  40.   g_removeButton=document.getElementById("removeButton");
  41.   
  42.   g_policySites=g_serv.jsPolicySites.clone();
  43.   nso_populateUrlList();
  44.   g_jsglobal.setAttribute("checked",g_serv.jsEnabled);
  45.  
  46.   visitCheckboxes(
  47.     function(prefName,inverse,checkbox) {
  48.       var val=g_serv.getPref(prefName);
  49.       checkbox.setAttribute("checked",inverse?!val:val);
  50.     }
  51.   );
  52.  
  53.   document.getElementById("opt-showTemp").setAttribute("label",noscriptUtil.getString("allowTemp",["[...]"]));
  54.   
  55.   document.getElementById("opt-notify.hide").setAttribute("label",
  56.            noscriptUtil.getString("notifyHide",[g_serv.getPref("notify.hideDelay",3)]));
  57.    
  58.   nso_setSample(g_serv.getPref("sound.block"));
  59. }
  60.  
  61. function nso_urlListChanged() {
  62.   const selectedItems=g_urlList.selectedItems;
  63.   var removeDisabled=true;
  64.   for(var j=selectedItems.length; j-->0;) {
  65.     if(selectedItems[j].getAttribute("disabled")!="true") {
  66.       removeDisabled=false;
  67.       break;
  68.     }
  69.   } 
  70.   g_removeButton.setAttribute("disabled", removeDisabled);
  71.   nso_urlChanged();
  72. }
  73.  
  74. function nso_urlChanged() {
  75.   var url=g_urlText.value;
  76.   if(url.match(/\s/)) url=g_urlText.value=url.replace(/\s/g,'');
  77.   var addEnabled=url.length>0 && (url=g_serv.getSite(url)) ;
  78.   if(addEnabled) {
  79.     var match=url.match(g_dom2);
  80.     if(match) url=match[1];
  81.     url=g_policySites.matches(url);
  82.     if( !(addEnabled = !url) ) {
  83.       nso_ensureVisible(url);
  84.     }
  85.   }
  86.   g_addButton.setAttribute("disabled",!addEnabled);
  87. }
  88.  
  89. function nso_populateUrlList() {
  90.   const sites=g_policySites.sitesList;
  91.   for(var j=g_urlList.getRowCount(); j-->0; g_urlList.removeItemAt(j));
  92.   var site,item;
  93.   
  94.   var match,k,len;
  95.   for(j=0, len=sites.length; j<len; j++) {
  96.     site=sites[j];
  97.     // skip protocol+2ndlevel domain URLs
  98.     if(match=site.match(g_dom2)) {
  99.       item=match[1];
  100.       for(k=sites.length; k-->0;) {
  101.         if(sites[k]==item) {
  102.           item=null;
  103.           break;
  104.         }
  105.       }
  106.       if(!item) continue;
  107.     }
  108.     item=g_urlList.appendItem(site,site);
  109.     if(g_serv.isPermanent(site)) { 
  110.       item.setAttribute("disabled","true");
  111.     } 
  112.     item.style.fontStyle=g_serv.isTemp(site)?"italic":"normal";
  113.   }
  114.   nso_urlListChanged();
  115. }
  116.  
  117. function nso_ensureVisible(site) {
  118.   var item;
  119.   for(var j=g_urlList.getRowCount(); j-->0;) {
  120.     if((item=g_urlList.getItemAtIndex(j)).getAttribute("value")==site) {
  121.       g_urlList.ensureElementIsVisible(item);
  122.     }
  123.   }
  124. }
  125.  
  126. function nso_allow() {
  127.   const site=g_serv.getSite(g_urlText.value);
  128.   g_policySites.add(site);
  129.   nso_populateUrlList();
  130.   nso_ensureVisible(site);
  131.   g_addButton.setAttribute("disabled","true");
  132. }
  133.  
  134.  
  135.  
  136. function nso_remove() {
  137.   const selectedItems=g_urlList.selectedItems;
  138.   var site;
  139.   for(var j=selectedItems.length; j-->0;) {
  140.     if(!g_serv.isPermanent(site=selectedItems[j].getAttribute("value"))) {
  141.       g_urlList.removeItemAt(g_urlList.getIndexOfItem(selectedItems[j]));
  142.       g_policySites.remove(site);
  143.     }
  144.   }
  145. }
  146.  
  147. function nso_save() {
  148.   visitCheckboxes(
  149.     function(prefName,inverse,checkbox) {
  150.       if(checkbox.getAttribute("collapsed")!="true") {
  151.         const checked=checkbox.getAttribute("checked")=="true";
  152.         const requestedVal=inverse?!checked:checked;
  153.         const prevVal=g_serv.getPref(prefName);
  154.         if(requestedVal!=prevVal) {
  155.           g_serv.setPref(prefName,requestedVal);
  156.         }
  157.       }
  158.     }
  159.   );
  160.   const serv=g_serv;
  161.   const global=g_jsglobal.getAttribute("checked")=="true";
  162.   serv.safeCapsOp(function() {
  163.     serv.setJSEnabled(g_policySites.sitesList,true,true);
  164.     serv.jsEnabled=global;
  165.   });
  166.   
  167.   g_serv.setPref("sound.block",nso_getSample());
  168. }
  169.  
  170. function nso_chooseSample() {
  171.    const title="NoScript - "+document.getElementById("sampleChooseButton").getAttribute("label");
  172.    try {
  173.     const cc=Components.classes;
  174.     const ci=Components.interfaces;
  175.     const fp = cc["@mozilla.org/filepicker;1"].createInstance(ci.nsIFilePicker);
  176.     
  177.     fp.init(window,title, ci.nsIFilePicker.modeOpen);
  178.     fp.appendFilter(noscriptUtil.getString("audio.samples"),"*.wav");
  179.     fp.filterIndex=0;
  180.     const ret=fp.show();
  181.     if (ret==ci.nsIFilePicker.returnOK || ret==ci.nsIFilePicker.returnReplace) {
  182.       nso_setSample(fp.fileURL.spec);
  183.       nso_play();
  184.     }
  185.   } catch(ex) {
  186.     g_serv.prompter.alert(window,title,ex.message);
  187.   }
  188. }
  189.  
  190. function nso_setSample(url) {
  191.   if(!url) {
  192.     url="chrome://noscript/skin/block.wav";
  193.   }
  194.   document.getElementById("sampleURL").value=url;
  195. }
  196. function nso_getSample() {
  197.   return document.getElementById("sampleURL").value;
  198. }
  199. function nso_play() {
  200.   g_serv.playSound(nso_getSample(),true);
  201. }
  202.  
  203.  
  204. function nso_buttonToTitle(op) {
  205.   return 
  206. }
  207.  
  208. function nso_impexp(callback) {
  209.   const op=callback.name.replace(/nso_/,'');
  210.   const title="NoScript - "+document.getElementById(op+"Button").getAttribute("label");
  211.   try {
  212.     const cc=Components.classes;
  213.     const ci=Components.interfaces;
  214.     const fp = cc["@mozilla.org/filepicker;1"].createInstance(ci.nsIFilePicker);
  215.     
  216.     fp.init(window,title, op=="import"?ci.nsIFilePicker.modeOpen:ci.nsIFilePicker.modeSave);
  217.     fp.appendFilters(ci.nsIFilePicker.filterText);
  218.     fp.appendFilters(ci.nsIFilePicker.filterAll);
  219.     fp.filterIndex=0;
  220.     fp.defaultExtension=".txt";
  221.     const ret=fp.show();
  222.     if (ret==ci.nsIFilePicker.returnOK || ret==ci.nsIFilePicker.returnReplace) {
  223.       callback(fp.file);
  224.     }
  225.     
  226.   } catch(ex) {
  227.     g_serv.prompter.alert(window,title,ex.message);
  228.   }
  229. }
  230.  
  231.  
  232. function nso_import(file) {
  233.   if(typeof(file)=="undefined") return nso_impexp(nso_import);
  234.   g_policySites.sitesString += g_serv.readFile(file);
  235.   nso_populateUrlList();
  236.   return null;
  237. }
  238.  
  239. function nso_export(file) {
  240.   if(typeof(file)=="undefined") return nso_impexp(nso_export);
  241.   g_serv.writeFile(file,g_policySites.sitesList.join("\n"));
  242.   return null;
  243. }
  244.  
  245.  
  246. function visitCheckboxes(callback) {
  247.   const rxOpt=/^(inv|)opt-(.*)/;
  248.   var j,checkbox,match;
  249.   const opts=document.getElementsByTagName("checkbox");
  250.   for(j=opts.length; j-->0;) {
  251.     checkbox=opts[j];
  252.     if(match=checkbox.id.match(rxOpt)) {
  253.       callback(match[2],match[1]=="inv",checkbox);
  254.     }
  255.   }
  256. }
  257.  
  258.  
  259.  
  260.