home *** CD-ROM | disk | FTP | other *** search
/ PC Professionell 2006 March / PCpro_2006_03.ISO / files / freeware / greasemonkey-0.6.4-fx.xpi / chrome / chromeFiles / content / config.js < prev    next >
Encoding:
Text File  |  2005-11-30  |  4.3 KB  |  134 lines

  1.  
  2.  
  3. function Config(configFile) {
  4.   this.onload = null;
  5.   this.scripts = null;
  6.   this.configFile = configFile;
  7.  
  8.   this.find = function(namespace, name) {
  9.     namespace = namespace.toLowerCase();
  10.     name = name.toLowerCase();
  11.  
  12.     for (var i = 0, script = null; (script = this.scripts[i]); i++) {
  13.       if (script.namespace.toLowerCase() == namespace && script.name.toLowerCase() == name) {
  14.         return i;
  15.       }
  16.     }
  17.  
  18.     return -1;
  19.   }
  20.   
  21.   this.initFilename = function(script) {
  22.     var index = {};
  23.     var base = script.name.replace(/[^A-Z0-9_]/gi, "").toLowerCase();
  24.  
  25.     // If no Latin characters found - use default
  26.     if (!base) {
  27.       base = "gm_script";
  28.     }
  29.  
  30.     // 24 is a totally arbitrary max length
  31.     if (base.length > 24) {
  32.       base = base.substring(0, 24);
  33.     }
  34.     
  35.     for (var i = 0; i < this.scripts.length; i++) {
  36.       index[this.scripts[i].filename] = this.scripts[i];
  37.     }
  38.     
  39.     if (!index[base + ".user.js"]) {
  40.       script.filename = base + ".user.js";
  41.       return;
  42.     }
  43.     
  44.     for (var count = 1; count < Number.MAX_VALUE; count++) {
  45.       if (!index[base + count + ".user.js"]) {
  46.         script.filename = base + count + ".user.js";
  47.         return;
  48.       }
  49.     }
  50.     
  51.     throw new Error("doooooooode. get some different user script or something.");
  52.   }
  53.   
  54.   this.load = function() {
  55.     var domParser = Components.classes["@mozilla.org/xmlextras/domparser;1"]
  56.                               .createInstance(Components.interfaces.nsIDOMParser);
  57.  
  58.     var configContents = getContents(getScriptFileURI("config.xml"));
  59.     var doc = domParser.parseFromString(configContents, "text/xml");
  60.     var nodes = doc.evaluate("/UserScriptConfig/Script", doc, null, 0, null);
  61.  
  62.     this.scripts = [];
  63.  
  64.     for (var node = null; (node = nodes.iterateNext()); ) {
  65.       var script = new Script();
  66.  
  67.       for (var i = 0, childNode = null; (childNode = node.childNodes[i]); i++) {
  68.         if (childNode.nodeName == "Include") {
  69.           script.includes.push(childNode.firstChild.nodeValue);
  70.         }
  71.         else if (childNode.nodeName == "Exclude") {
  72.           script.excludes.push(childNode.firstChild.nodeValue);
  73.         }
  74.       }
  75.  
  76.       script.filename = node.getAttribute("filename");
  77.       script.name = node.getAttribute("name");
  78.       script.namespace = node.getAttribute("namespace");
  79.       script.description = node.getAttribute("description");
  80.       script.enabled = node.getAttribute("enabled") == true.toString();
  81.  
  82.       this.scripts.push(script);
  83.     }
  84.   }
  85.  
  86.   this.save = function() {
  87.     var doc = document.implementation.createDocument("", "UserScriptConfig", null);
  88.     
  89.     for (var i = 0, scriptObj = null; (scriptObj = this.scripts[i]); i++) {
  90.       var scriptNode = doc.createElement("Script");
  91.  
  92.       for (var j = 0; j < scriptObj.includes.length; j++) {
  93.         var includeNode = doc.createElement("Include");
  94.         includeNode.appendChild(doc.createTextNode(scriptObj.includes[j]));
  95.         scriptNode.appendChild(doc.createTextNode("\n\t\t"));
  96.         scriptNode.appendChild(includeNode);
  97.       }
  98.  
  99.       for (var j = 0; j < scriptObj.excludes.length; j++) {
  100.         var excludeNode = doc.createElement("Exclude");
  101.         excludeNode.appendChild(doc.createTextNode(scriptObj.excludes[j]));
  102.         scriptNode.appendChild(doc.createTextNode("\n\t\t"));
  103.         scriptNode.appendChild(excludeNode);
  104.       }
  105.  
  106.       scriptNode.appendChild(doc.createTextNode("\n\t"));
  107.  
  108.       scriptNode.setAttribute("filename", scriptObj.filename);
  109.       scriptNode.setAttribute("name", scriptObj.name);
  110.       scriptNode.setAttribute("namespace", scriptObj.namespace);
  111.       scriptNode.setAttribute("description", scriptObj.description);
  112.       scriptNode.setAttribute("enabled", scriptObj.enabled);
  113.  
  114.       doc.firstChild.appendChild(doc.createTextNode("\n\t"));
  115.       doc.firstChild.appendChild(scriptNode);
  116.     }
  117.  
  118.     doc.firstChild.appendChild(doc.createTextNode("\n"))
  119.  
  120.     var configStream = getWriteStream(this.configFile);
  121.     new XMLSerializer().serializeToStream(doc, configStream, "utf-8");
  122.     configStream.close();
  123.   }
  124. }
  125.  
  126. function Script() {
  127.   this.filename = null;
  128.   this.name = null;
  129.   this.namespace = null;
  130.   this.description = null;
  131.   this.enabled = true;
  132.   this.includes = [];
  133.   this.excludes = [];
  134. }