home *** CD-ROM | disk | FTP | other *** search
/ PC Professionell 2006 June / PCpro_2006_06.ISO / files / firefox / noscript-1.1.3.9-fxflmz.xpi / chrome / noscript.jar / content / noscript / noscriptOptions.js < prev    next >
Encoding:
JavaScript  |  2006-01-26  |  8.0 KB  |  275 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.   var pingCbx =  document.getElementById("mozopt-browser.send_pings");
  47.   if(pingCbx.getAttribute("label").indexOf("Allow ") == 0) { 
  48.     pingCbx.setAttribute("label", noscriptUtil.getString("allowLocal", ["<a ping...>"]));
  49.     document.getElementById("opt-noping")
  50.             .setAttribute("label", noscriptUtil.getString("forbidLocal", ["<a ping...>"]));
  51.   }
  52.   
  53.   visitCheckboxes(
  54.     function(prefName, inverse, checkbox, mozilla) {
  55.       try {
  56.         val = mozilla ? g_serv.prefService.getBoolPref(prefName) : g_serv.getPref(prefName);
  57.         checkbox.setAttribute("checked",inverse ? !val: val);
  58.       } catch(ex) {}
  59.     }
  60.   );
  61.  
  62.   document.getElementById("opt-showTemp").setAttribute("label", noscriptUtil.getString("allowTemp",["[...]"]));
  63.   
  64.   document.getElementById("opt-notify.hide").setAttribute("label",
  65.            noscriptUtil.getString("notifyHide",[g_serv.getPref("notify.hideDelay",3)]));
  66.    
  67.   nso_setSample(g_serv.getPref("sound.block"));
  68. }
  69.  
  70. function nso_urlListChanged() {
  71.   const selectedItems=g_urlList.selectedItems;
  72.   var removeDisabled=true;
  73.   for(var j=selectedItems.length; j-->0;) {
  74.     if(selectedItems[j].getAttribute("disabled")!="true") {
  75.       removeDisabled=false;
  76.       break;
  77.     }
  78.   } 
  79.   g_removeButton.setAttribute("disabled", removeDisabled);
  80.   nso_urlChanged();
  81. }
  82.  
  83. function nso_urlChanged() {
  84.   var url=g_urlText.value;
  85.   if(url.match(/\s/)) url=g_urlText.value=url.replace(/\s/g,'');
  86.   var addEnabled=url.length>0 && (url=g_serv.getSite(url)) ;
  87.   if(addEnabled) {
  88.     var match=url.match(g_dom2);
  89.     if(match) url=match[1];
  90.     url=g_policySites.matches(url);
  91.     if( !(addEnabled = !url) ) {
  92.       nso_ensureVisible(url);
  93.     }
  94.   }
  95.   g_addButton.setAttribute("disabled",!addEnabled);
  96. }
  97.  
  98. function nso_populateUrlList() {
  99.   const sites=g_policySites.sitesList;
  100.   for(var j=g_urlList.getRowCount(); j-->0; g_urlList.removeItemAt(j));
  101.   var site,item;
  102.   
  103.   var match,k,len;
  104.   for(j=0, len=sites.length; j<len; j++) {
  105.     site=sites[j];
  106.     // skip protocol+2ndlevel domain URLs
  107.     if(match=site.match(g_dom2)) {
  108.       item=match[1];
  109.       for(k=sites.length; k-->0;) {
  110.         if(sites[k]==item) {
  111.           item=null;
  112.           break;
  113.         }
  114.       }
  115.       if(!item) continue;
  116.     }
  117.     item=g_urlList.appendItem(site,site);
  118.     if(g_serv.isPermanent(site)) { 
  119.       item.setAttribute("disabled","true");
  120.     } 
  121.     item.style.fontStyle=g_serv.isTemp(site)?"italic":"normal";
  122.   }
  123.   nso_urlListChanged();
  124. }
  125.  
  126. function nso_ensureVisible(site) {
  127.   var item;
  128.   for(var j=g_urlList.getRowCount(); j-->0;) {
  129.     if((item=g_urlList.getItemAtIndex(j)).getAttribute("value")==site) {
  130.       g_urlList.ensureElementIsVisible(item);
  131.     }
  132.   }
  133. }
  134.  
  135. function nso_allow() {
  136.   const site=g_serv.getSite(g_urlText.value);
  137.   g_policySites.add(site);
  138.   nso_populateUrlList();
  139.   nso_ensureVisible(site);
  140.   g_addButton.setAttribute("disabled","true");
  141. }
  142.  
  143.  
  144.  
  145. function nso_remove() {
  146.   const selectedItems=g_urlList.selectedItems;
  147.   var site;
  148.   for(var j=selectedItems.length; j-->0;) {
  149.     if(!g_serv.isPermanent(site=selectedItems[j].getAttribute("value"))) {
  150.       g_urlList.removeItemAt(g_urlList.getIndexOfItem(selectedItems[j]));
  151.       g_policySites.remove(site);
  152.     }
  153.   }
  154. }
  155.  
  156. function nso_save() {
  157.   visitCheckboxes(
  158.     function(prefName, inverse, checkbox, mozilla) {
  159.       if(checkbox.getAttribute("collapsed")!="true") {
  160.         const checked=checkbox.getAttribute("checked")=="true";
  161.         const requestedVal=inverse? !checked : checked;
  162.         
  163.         if(mozilla) {
  164.           g_serv.prefService.setBoolPref(prefName, requestedVal);
  165.           return;
  166.         }
  167.         
  168.         const prevVal=g_serv.getPref(prefName);
  169.         if(requestedVal!=prevVal) {
  170.           g_serv.setPref(prefName, requestedVal);
  171.         }
  172.       }
  173.     }
  174.   );
  175.   const serv=g_serv;
  176.   const global=g_jsglobal.getAttribute("checked")=="true";
  177.   serv.safeCapsOp(function() {
  178.     serv.setJSEnabled(g_policySites.sitesList,true,true);
  179.     serv.jsEnabled=global;
  180.   });
  181.   
  182.   g_serv.setPref("sound.block",nso_getSample());
  183. }
  184.  
  185. function nso_chooseSample() {
  186.    const title="NoScript - "+document.getElementById("sampleChooseButton").getAttribute("label");
  187.    try {
  188.     const cc=Components.classes;
  189.     const ci=Components.interfaces;
  190.     const fp = cc["@mozilla.org/filepicker;1"].createInstance(ci.nsIFilePicker);
  191.     
  192.     fp.init(window,title, ci.nsIFilePicker.modeOpen);
  193.     fp.appendFilter(noscriptUtil.getString("audio.samples"),"*.wav");
  194.     fp.filterIndex=0;
  195.     const ret=fp.show();
  196.     if (ret==ci.nsIFilePicker.returnOK || ret==ci.nsIFilePicker.returnReplace) {
  197.       nso_setSample(fp.fileURL.spec);
  198.       nso_play();
  199.     }
  200.   } catch(ex) {
  201.     g_serv.prompter.alert(window,title,ex.message);
  202.   }
  203. }
  204.  
  205. function nso_setSample(url) {
  206.   if(!url) {
  207.     url="chrome://noscript/skin/block.wav";
  208.   }
  209.   document.getElementById("sampleURL").value=url;
  210. }
  211. function nso_getSample() {
  212.   return document.getElementById("sampleURL").value;
  213. }
  214. function nso_play() {
  215.   g_serv.playSound(nso_getSample(),true);
  216. }
  217.  
  218.  
  219. function nso_buttonToTitle(op) {
  220.   return 
  221. }
  222.  
  223. function nso_impexp(callback) {
  224.   const op=callback.name.replace(/nso_/,'');
  225.   const title="NoScript - "+document.getElementById(op+"Button").getAttribute("label");
  226.   try {
  227.     const cc=Components.classes;
  228.     const ci=Components.interfaces;
  229.     const fp = cc["@mozilla.org/filepicker;1"].createInstance(ci.nsIFilePicker);
  230.     
  231.     fp.init(window,title, op=="import"?ci.nsIFilePicker.modeOpen:ci.nsIFilePicker.modeSave);
  232.     fp.appendFilters(ci.nsIFilePicker.filterText);
  233.     fp.appendFilters(ci.nsIFilePicker.filterAll);
  234.     fp.filterIndex=0;
  235.     fp.defaultExtension=".txt";
  236.     const ret=fp.show();
  237.     if (ret==ci.nsIFilePicker.returnOK || ret==ci.nsIFilePicker.returnReplace) {
  238.       callback(fp.file);
  239.     }
  240.     
  241.   } catch(ex) {
  242.     g_serv.prompter.alert(window,title,ex.message);
  243.   }
  244. }
  245.  
  246.  
  247. function nso_import(file) {
  248.   if(typeof(file)=="undefined") return nso_impexp(nso_import);
  249.   g_policySites.sitesString += g_serv.readFile(file);
  250.   nso_populateUrlList();
  251.   return null;
  252. }
  253.  
  254. function nso_export(file) {
  255.   if(typeof(file)=="undefined") return nso_impexp(nso_export);
  256.   g_serv.writeFile(file,g_policySites.sitesList.join("\n"));
  257.   return null;
  258. }
  259.  
  260.  
  261. function visitCheckboxes(callback) {
  262.   const rxOpt=/^(inv|moz|)opt-(.*)/;
  263.   var j,checkbox,match;
  264.   const opts=document.getElementsByTagName("checkbox");
  265.   for(j=opts.length; j-->0;) {
  266.     checkbox=opts[j];
  267.     if(match=checkbox.id.match(rxOpt)) {
  268.       callback(match[2],match[1]=="inv",checkbox,match[1]=="moz");
  269.     }
  270.   }
  271. }
  272.  
  273.  
  274.  
  275.