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 / scriptdownloader.js < prev    next >
Encoding:
Text File  |  2005-11-30  |  3.5 KB  |  132 lines

  1.  
  2.  
  3. function ScriptDownloader() {}
  4.  
  5. ScriptDownloader.prototype.installFromURL = function(url) {
  6.   GM_BrowserUI.showStatus("Downloading user script...");
  7.  
  8.   this.xhr = new XMLHttpRequest();
  9.   this.url = url;
  10.  
  11.   try {
  12.     this.xhr.open("GET", url);
  13.     this.xhr.onload = GM_hitch(this, "installFromURLSuccess");
  14.     this.xhr.onerror = GM_hitch(this, "installFromURLFailure");
  15.     this.xhr.send(null);
  16.   }
  17.   catch (e) {
  18.     handleErrors(e);
  19.   }
  20. }
  21.  
  22. ScriptDownloader.prototype.installFromURLFailure = function(e) {
  23.   alert("Could not download user script\n\n" + e.toString());
  24.   GM_BrowserUI.hideStatus();
  25. }
  26.   
  27. ScriptDownloader.prototype.installFromURLSuccess = function() {
  28.   this.installFromSource(this.xhr.responseText, this.url);
  29. }
  30.  
  31. ScriptDownloader.prototype.installFromSource = function(source, url) {
  32.   var ioservice = Components.classes["@mozilla.org/network/io-service;1"]
  33.                             .getService();
  34.   var sourceUri = ioservice.newURI(url, null, null);
  35.  
  36.   try {
  37.     var targetFile = getTempFile();
  38.     var writeStream = getWriteStream(targetFile);
  39.  
  40.     writeStream.write(source, source.length);
  41.     writeStream.close();
  42.  
  43.     // initialize a new script object
  44.     var script = new Script();
  45.     script.filename = targetFile.leafName;
  46.     script.enabled = true;
  47.     script.includes = [];
  48.     script.excludes = [];
  49.     
  50.     // read one line at a time looking for start meta delimiter or EOF
  51.     var lines = source.match(/.+/g);
  52.     var lnIdx = 0;
  53.     var result = {};
  54.     var foundMeta = false;
  55.  
  56.     while (result = lines[lnIdx++]) {
  57.       if (result.indexOf("// ==UserScript==") == 0) {
  58.         GM_log("* found metadata");
  59.         foundMeta = true;
  60.         break;
  61.       }
  62.     }
  63.  
  64.     // gather up meta lines
  65.     if (foundMeta) {
  66.       while (result = lines[lnIdx++]) {
  67.         if (result.indexOf("// ==/UserScript==") == 0) {
  68.           break;
  69.         }
  70.  
  71.         var match = result.match(/\/\/ \@(\S+)\s+([^\n]+)/);
  72.         if (match != null) {
  73.           switch (match[1]) {
  74.             case "name":
  75.             case "namespace":
  76.             case "description":
  77.               script[match[1]] = match[2];
  78.               break;
  79.             case "include":
  80.             case "exclude":
  81.               script[match[1]+"s"].push(match[2]);
  82.               break;
  83.           }
  84.         }
  85.       }
  86.     }
  87.  
  88.     // if no meta info, default to reasonable values
  89.     if (script.name == null) {
  90.       script.name = parseScriptName(sourceUri);
  91.     }
  92.  
  93.     if (script.namespace == null) {
  94.       script.namespace = sourceUri.host;
  95.     }
  96.  
  97.     if (script.includes.length == 0) {
  98.       script.includes.push("*");
  99.     }
  100.  
  101.     var config = new Config(getScriptFile("config.xml"));
  102.  
  103.     config.load();
  104.  
  105.     var newDir = getScriptDir();
  106.     var existingIndex = config.find(script.namespace, script.name);
  107.     var existingFile = null;
  108.     var oldScripts = new Array(config.scripts);
  109.  
  110.     if (existingIndex > -1) {
  111.       existingFile = getScriptFile(config.scripts[existingIndex].filename);
  112.       existingFile.remove(false);
  113.       config.scripts.splice(existingIndex, 1);
  114.     }
  115.  
  116.     try {
  117.       config.initFilename(script);
  118.       targetFile.moveTo(newDir, script.filename)
  119.       config.scripts.push(script);
  120.       config.save();
  121.       GM_BrowserUI.hideStatus();
  122.       alert(script.filename + " installed successfully.");
  123.     }
  124.     catch (e) {
  125.       config.scripts = oldScripts;
  126.       throw e;
  127.     }
  128.   } catch (e2) {
  129.     alert("Error installing user script:\n\n" + (e2 ? e2 : ""));
  130.     GM_BrowserUI.hideStatus();
  131.   }
  132. }