Changeset 661

Show
Ignore:
Timestamp:
02/15/08 12:36:11 (10 months ago)
Author:
ma..@jesperkristensen.dk
Message:

Make Config an XPCOM service.

Files:

Legend:

Unmodified
Added
Removed
Modified
Copied
Moved
  • branches/config-service/src/chrome/chromeFiles/content/browser.js

    r657 r661  
    2828  this.menuCommanders = []; 
    2929  this.currentMenuCommander = null; 
    30  
    31   (new Config()).updateVersion(); 
    3230 
    3331  GM_listen(window, "load", GM_hitch(this, "chromeLoad")); 
     
    233231  this.scriptDownloader_ = scriptDownloader; 
    234232 
    235   var ioSvc = Components.classes["@mozilla.org/network/io-service;1"] 
    236                         .getService(Components.interfaces.nsIIOService); 
    237   var uri = ioSvc.newFileURI(scriptDownloader.script.tempFile); 
    238  
    239   var tab = this.tabBrowser.addTab(uri.spec); 
     233  var tab = this.tabBrowser.addTab(scriptDownloader.script.urlToDownload); 
    240234  var browser = this.tabBrowser.getBrowserForTab(tab); 
    241235 
     
    265259 
    266260GM_BrowserUI.installScript = function(script){ 
    267   var config = new Config(); 
    268   config.load(); 
    269   config.install(script); 
     261  GM_getConfig().install(script); 
    270262  this.showHorrayMessage(script.name); 
    271263}; 
     
    429421 
    430422function GM_showPopup(aEvent) { 
    431   var config = new Config(); 
    432   config.load(); 
    433423  var popup = aEvent.target; 
    434424  var url = getBrowser().contentWindow.document.location.href; 
     
    444434  } 
    445435 
    446   var foundInjectedScript = false; 
    447  
    448436  // build the new list of scripts 
    449   for (var i = 0, script = null; script = config.scripts[i]; i++) { 
    450     incloop: for (var j = 0; j < script.includes.length; j++) { 
    451       var pattern = convert2RegExp(script.includes[j]); 
    452       if (pattern.test(url)) { 
    453         for (var k = 0; k < script.excludes.length; k++) { 
    454           pattern = convert2RegExp(script.excludes[k]); 
    455           if (pattern.test(url)) { 
    456             break incloop; 
    457           } 
    458         } 
    459  
    460         foundInjectedScript = true; 
    461  
     437  var scripts = GM_getConfig().getScriptsForUrl(url, true); 
     438  for (var i = 0, script = null; script = scripts[i]; i++) { 
    462439        var mi = document.createElement('menuitem'); 
    463440        mi.setAttribute('label', script.name); 
     
    467444 
    468445        popup.insertBefore(mi, document.getElementById("gm-status-no-scripts-sep")); 
    469  
    470         break incloop; 
    471       } 
    472     } 
    473   } 
    474  
    475   document.getElementById("gm-status-no-scripts").collapsed = foundInjectedScript; 
     446  } 
     447 
     448  document.getElementById("gm-status-no-scripts").collapsed = scripts.length; 
    476449}; 
    477450 
     
    482455function GM_popupClicked(aEvent) { 
    483456  if (aEvent.button == 0 || aEvent.button == 2) { 
    484     var config = new Config(); 
    485     config.load(); 
    486457    var scriptNum=aEvent.target.value; 
    487     if (!config.scripts[scriptNum]) return; 
    488  
    489     if (aEvent.button == 0) { 
    490       // left-click: toggle enabled state 
    491       config.scripts[scriptNum].enabled=!config.scripts[scriptNum].enabled; 
    492       config.save(); 
    493     } else { 
    494       // right-click: open in editor 
    495       openInEditor(config.scripts[scriptNum]); 
    496     } 
     458    var script = GM_getConfig().scripts[scriptNum]; 
     459    if (!script) return; 
     460 
     461    if (aEvent.button == 0) // left-click: toggle enabled state 
     462      script.enabled=!script.enabled; 
     463    else // right-click: open in editor 
     464      openInEditor(script); 
    497465 
    498466    closeMenus(aEvent.target); 
     
    630598 
    631599GM_BrowserUI.manageMenuItemClicked = function() { 
    632    window.openDialog("chrome://greasemonkey/content/manage.xul", "manager", 
    633     "resizable,centerscreen,modal"); 
     600   GM_openUserScriptManager(); 
    634601}; 
    635602 
  • branches/config-service/src/chrome/chromeFiles/content/browser.xul

    r657 r661  
    1616  <script type="application/x-javascript" src="chrome://greasemonkey/content/dochandler.js" /> 
    1717  <script type="application/x-javascript" src="chrome://greasemonkey/content/scriptdownloader.js" /> 
    18   <script type="application/x-javascript" src="chrome://greasemonkey/content/config.js" /> 
    1918  <script type="application/x-javascript" src="chrome://greasemonkey/content/browser.js" /> 
    2019  <script type="application/x-javascript" src="chrome://greasemonkey/content/accelimation.js" /> 
  • branches/config-service/src/chrome/chromeFiles/content/config.js

    r660 r661  
    33 
    44function Config() { 
    5   this.onload = null; 
    6   this.scripts = null; 
    7   this.configFile = this.scriptDir; 
    8   this.configFile.append("config.xml"); 
     5  this._scripts = null; 
     6  this._configFile = this._scriptDir; 
     7  this._configFile.append("config.xml"); 
     8 
     9  this._observers = []; 
     10 
     11  this._updateVersion(); 
     12  this._load(); 
    913}; 
    1014 
     
    1317  .loadSubScript("chrome://greasemonkey/content/versioning.js"); 
    1418 
    15 Config.prototype.find = function(namespace, name) { 
    16   namespace = namespace.toLowerCase(); 
    17   name = name.toLowerCase(); 
    18  
    19   for (var i = 0, script = null; (script = this.scripts[i]); i++) { 
    20     if (script.namespace.toLowerCase() == namespace && script.name.toLowerCase() == name) { 
     19Config.prototype.addObserver = function(observer, script) { 
     20  var observers = script ? script._observers : this._observers; 
     21  observers.push(observer); 
     22}; 
     23 
     24Config.prototype.removeObserver = function(observer, script) { 
     25  var observers = script ? script._observers : this._observers; 
     26  var index = observers.indexOf(observer); 
     27  if (index == -1) throw new Error("Observer not found"); 
     28  observers.splice(index, 1); 
     29}; 
     30 
     31Config.prototype._notifyObservers = function(script, event, data) { 
     32  var observers = this._observers.concat(script._observers); 
     33  for (var i = 0, observer; observer = observers[i]; i++) 
     34    observer.notifyEvent(script, event, data); 
     35}; 
     36 
     37Config.prototype._changed = function(script, event, data) { 
     38  this._save(); 
     39  this._notifyObservers(script, event, data); 
     40}; 
     41 
     42Config.prototype.installIsUpdate = function(script) 
     43
     44  return this._find(script) > -1; 
     45}; 
     46 
     47Config.prototype._find = function(aScript) { 
     48  namespace = aScript._namespace.toLowerCase(); 
     49  name = aScript._name.toLowerCase(); 
     50 
     51  for (var i = 0, script = null; (script = this._scripts[i]); i++) { 
     52    if (script._namespace.toLowerCase() == namespace && script._name.toLowerCase() == name) { 
    2153      return i; 
    2254    } 
     
    2658}; 
    2759 
    28 Config.prototype.load = function() { 
     60Config.prototype._load = function() { 
    2961  var domParser = Components.classes["@mozilla.org/xmlextras/domparser;1"] 
    3062                            .createInstance(Components.interfaces.nsIDOMParser); 
    3163 
    32   var configContents = getContents(this.configFile); 
     64  var configContents = getContents(this._configFile); 
    3365  var doc = domParser.parseFromString(configContents, "text/xml"); 
    3466  var nodes = doc.evaluate("/UserScriptConfig/Script", doc, null, 0, null); 
    3567 
    36   this.scripts = []; 
     68  this._scripts = []; 
    3769 
    3870  for (var node = null; (node = nodes.iterateNext()); ) { 
     
    4173    for (var i = 0, childNode = null; (childNode = node.childNodes[i]); i++) { 
    4274      if (childNode.nodeName == "Include") { 
    43         script.includes.push(childNode.firstChild.nodeValue); 
     75        script._includes.push(childNode.firstChild.nodeValue); 
    4476      } else if (childNode.nodeName == "Exclude") { 
    45         script.excludes.push(childNode.firstChild.nodeValue); 
     77        script._excludes.push(childNode.firstChild.nodeValue); 
    4678      } else if (childNode.nodeName == "Require") { 
    4779        var scriptRequire = new ScriptRequire(script); 
    48         scriptRequire.filename = childNode.getAttribute("filename"); 
    49         script.requires.push(scriptRequire); 
     80        scriptRequire._filename = childNode.getAttribute("filename"); 
     81        script._requires.push(scriptRequire); 
    5082      } else if (childNode.nodeName == "Resource") { 
    5183        var scriptResource = new ScriptResource(script); 
    52         scriptResource.name = childNode.getAttribute("name"); 
    53         scriptResource.filename = childNode.getAttribute("filename"); 
    54         scriptResource.mimetype = childNode.getAttribute("mimetype"); 
    55         scriptResource.charset  = childNode.getAttribute("charset"); 
    56         script.resources.push(scriptResource); 
     84        scriptResource._name = childNode.getAttribute("name"); 
     85        scriptResource._filename = childNode.getAttribute("filename"); 
     86        scriptResource._mimetype = childNode.getAttribute("mimetype"); 
     87        scriptResource._charset  = childNode.getAttribute("charset"); 
     88        script._resources.push(scriptResource); 
    5789      } 
    5890    } 
    5991 
    60     script.filename = node.getAttribute("filename"); 
    61     script.name = node.getAttribute("name"); 
    62     script.namespace = node.getAttribute("namespace"); 
    63     script.description = node.getAttribute("description"); 
    64     script.enabled = node.getAttribute("enabled") == true.toString(); 
    65     script.basedir = node.getAttribute("basedir") || "."; 
    66  
    67     this.scripts.push(script); 
    68   } 
    69 }; 
    70  
    71 Config.prototype.save = function() { 
    72   var doc = document.implementation.createDocument("", "UserScriptConfig", null); 
    73  
    74   for (var i = 0, scriptObj = null; (scriptObj = this.scripts[i]); i++) { 
     92    script._filename = node.getAttribute("filename"); 
     93    script._name = node.getAttribute("name"); 
     94    script._namespace = node.getAttribute("namespace"); 
     95    script._description = node.getAttribute("description"); 
     96    script._enabled = node.getAttribute("enabled") == true.toString(); 
     97    script._basedir = node.getAttribute("basedir") || "."; 
     98 
     99    this._scripts.push(script); 
     100  } 
     101}; 
     102 
     103Config.prototype._save = function() { 
     104  var doc = Components.classes["@mozilla.org/xmlextras/domparser;1"] 
     105    .createInstance(Components.interfaces.nsIDOMParser) 
     106    .parseFromString("<UserScriptConfig></UserScriptConfig>", "text/xml"); 
     107 
     108  for (var i = 0, scriptObj = null; (scriptObj = this._scripts[i]); i++) { 
    75109    var scriptNode = doc.createElement("Script"); 
    76110 
    77     for (var j = 0; j < scriptObj.includes.length; j++) { 
     111    for (var j = 0; j < scriptObj._includes.length; j++) { 
    78112      var includeNode = doc.createElement("Include"); 
    79       includeNode.appendChild(doc.createTextNode(scriptObj.includes[j])); 
     113      includeNode.appendChild(doc.createTextNode(scriptObj._includes[j])); 
    80114      scriptNode.appendChild(doc.createTextNode("\n\t\t")); 
    81115      scriptNode.appendChild(includeNode); 
    82116    } 
    83117 
    84     for (var j = 0; j < scriptObj.excludes.length; j++) { 
     118    for (var j = 0; j < scriptObj._excludes.length; j++) { 
    85119      var excludeNode = doc.createElement("Exclude"); 
    86       excludeNode.appendChild(doc.createTextNode(scriptObj.excludes[j])); 
     120      excludeNode.appendChild(doc.createTextNode(scriptObj._excludes[j])); 
    87121      scriptNode.appendChild(doc.createTextNode("\n\t\t")); 
    88122      scriptNode.appendChild(excludeNode); 
    89123    } 
    90124 
    91     for (var j = 0; j < scriptObj.requires.length; j++) { 
    92       var req = scriptObj.requires[j]; 
     125    for (var j = 0; j < scriptObj._requires.length; j++) { 
     126      var req = scriptObj._requires[j]; 
    93127      var resourceNode = doc.createElement("Require"); 
    94128 
    95       resourceNode.setAttribute("filename", req.filename); 
     129      resourceNode.setAttribute("filename", req._filename); 
    96130 
    97131      scriptNode.appendChild(doc.createTextNode("\n\t\t")); 
     
    99133    } 
    100134 
    101     for (var j = 0; j< scriptObj.resources.length; j++) { 
    102       var imp = scriptObj.resources[j]; 
     135    for (var j = 0; j< scriptObj._resources.length; j++) { 
     136      var imp = scriptObj._resources[j]; 
    103137      var resourceNode = doc.createElement("Resource"); 
    104138 
    105       resourceNode.setAttribute("name", imp.name); 
    106       resourceNode.setAttribute("filename", imp.filename); 
    107       resourceNode.setAttribute("mimetype", imp.mimetype); 
    108       if (imp.charset) { 
    109         resourceNode.setAttribute("charset", imp.charset); 
     139      resourceNode.setAttribute("name", imp._name); 
     140      resourceNode.setAttribute("filename", imp._filename); 
     141      resourceNode.setAttribute("mimetype", imp._mimetype); 
     142      if (imp._charset) { 
     143        resourceNode.setAttribute("charset", imp._charset); 
    110144      } 
    111145 
     
    116150    scriptNode.appendChild(doc.createTextNode("\n\t")); 
    117151 
    118     scriptNode.setAttribute("filename", scriptObj.filename); 
    119     scriptNode.setAttribute("name", scriptObj.name); 
    120     scriptNode.setAttribute("namespace", scriptObj.namespace); 
    121     scriptNode.setAttribute("description", scriptObj.description); 
    122     scriptNode.setAttribute("enabled", scriptObj.enabled); 
    123     scriptNode.setAttribute("basedir", scriptObj.basedir); 
     152    scriptNode.setAttribute("filename", scriptObj._filename); 
     153    scriptNode.setAttribute("name", scriptObj._name); 
     154    scriptNode.setAttribute("namespace", scriptObj._namespace); 
     155    scriptNode.setAttribute("description", scriptObj._description); 
     156    scriptNode.setAttribute("enabled", scriptObj._enabled); 
     157    scriptNode.setAttribute("basedir", scriptObj._basedir); 
    124158 
    125159    doc.firstChild.appendChild(doc.createTextNode("\n\t")); 
     
    129163  doc.firstChild.appendChild(doc.createTextNode("\n")); 
    130164 
    131   var configStream = getWriteStream(this.configFile); 
    132   new XMLSerializer().serializeToStream(doc, configStream, "utf-8"); 
     165  var configStream = getWriteStream(this._configFile); 
     166  Components.classes["@mozilla.org/xmlextras/xmlserializer;1"] 
     167    .createInstance(Components.interfaces.nsIDOMSerializer) 
     168    .serializeToStream(doc, configStream, "utf-8"); 
    133169  configStream.close(); 
    134170}; 
     
    140176 
    141177  var script = new Script(this); 
    142   script.enabled = true; 
    143   script.includes = []; 
    144   script.excludes = []; 
     178  script._downloadUrl = uri.spec; 
     179  script._enabled = true; 
    145180 
    146181  // read one line at a time looking for start meta delimiter or EOF 
     
    173208          case "namespace": 
    174209          case "description": 
    175             script[match[1]] = match[2]; 
     210            script["_" + match[1]] = match[2]; 
    176211            break; 
    177212          case "include": 
     213            script._includes.push(match[2]); 
     214            break; 
    178215          case "exclude": 
    179             script[match[1]+"s"].push(match[2]); 
     216            script._excludes.push(match[2]); 
    180217            break; 
    181218          case "require": 
    182219            var reqUri = ioservice.newURI(match[2], null, uri); 
    183220            var scriptRequire = new ScriptRequire(script); 
    184             scriptRequire.url = reqUri.spec; 
    185             script.requires.push(scriptRequire); 
     221            scriptRequire._downloadUrl = reqUri.spec; 
     222            script._requires.push(scriptRequire); 
    186223            break; 
    187224          case "resource": 
     
    205242            var resUri = ioservice.newURI(res[2], null, uri); 
    206243            var scriptResource = new ScriptResource(script); 
    207             scriptResource.name = resName; 
    208             scriptResource.url = resUri.spec; 
    209             script.resources.push(scriptResource); 
     244            scriptResource._name = resName; 
     245            scriptResource._downloadUrl = resUri.spec; 
     246            script._resources.push(scriptResource); 
    210247            break; 
    211248        } 
     
    215252 
    216253  // if no meta info, default to reasonable values 
    217   if (script.name == null) { 
    218     script.name = parseScriptName(uri); 
    219   } 
    220  
    221   if (script.namespace == null) { 
    222     script.namespace = uri.host; 
    223   } 
    224  
    225   if (script.includes.length == 0) { 
    226     script.includes.push("*"); 
     254  if (script._name == null) { 
     255    script._name = parseScriptName(uri); 
     256  } 
     257 
     258  if (script._namespace == null) { 
     259    script._namespace = uri.host; 
     260  } 
     261 
     262  if (!script._description) 
     263    script._description = ""; 
     264 
     265  if (script._includes.length == 0) { 
     266    script._includes.push("*"); 
    227267  } 
    228268 
     
    234274 
    235275  try { 
    236     var existingIndex = this.find(script.namespace, script.name); 
     276    var existingIndex = this._find(script); 
    237277    if (existingIndex > -1) 
    238       this.uninstall(this.scripts[existingIndex], false); 
    239  
    240     script._initFile(script.tempFile); 
    241     script.tempFile = null; 
    242  
    243     for (var i = 0; i < script.requires.length; i++) 
    244       script.requires[i]._initFile(); 
    245  
    246     for (var i = 0; i < script.resources.length; i++) 
    247       script.resources[i]._initFile(); 
    248  
    249     this.scripts.push(script); 
    250     this.save(); 
     278      this.uninstall(this._scripts[existingIndex], false); 
     279 
     280    script._initFile(script._tempFile); 
     281    script._tempFile = null; 
     282 
     283    for (var i = 0; i < script._requires.length; i++) 
     284      script._requires[i]._initFile(); 
     285 
     286    for (var i = 0; i < script._resources.length; i++) 
     287      script._resources[i]._initFile(); 
     288 
     289    this._scripts.push(script); 
     290    this._changed(script, "install", null); 
    251291 
    252292    GM_log("< Config.install"); 
     
    260300Config.prototype.uninstall = function(script, uninstallPrefs) 
    261301{ 
    262   var idx = this.find(script.namespace, script.name); 
    263   this.scripts.splice(idx, 1); 
    264  
    265   if (script.basedir) // if script has its own dir, remove the dir + contents 
    266     script.basedirFile.remove(true); 
     302  var idx = this._find(script); 
     303  this._scripts.splice(idx, 1); // TODO 
     304  this._changed(script, "uninstall", null); 
     305 
     306  if (script._basedir) // if script has its own dir, remove the dir + contents 
     307    script._basedirFile.remove(true); 
    267308  else // if script is in the root, just remove the file 
    268     script.file.remove(false); 
     309    script._file.remove(false); 
    269310 
    270311  if (uninstallPrefs) // Remove saved preferences 
    271      GM_prefRoot.remove("scriptvals." + script.namespace + "/" + script.name + "."); 
     312     GM_prefRoot.remove("scriptvals." + script._namespace + "/" + script._name + "."); 
    272313} 
    273314 
     
    282323Config.prototype.move = function(script, destination) 
    283324{ 
    284   var from = this.scripts.indexOf(script); 
     325  var from = this._scripts.indexOf(script); 
    285326  var to = -1; 
    286327 
     
    292333    to = from + destination; 
    293334    to = Math.max(0, to); 
    294     to = Math.min(this.scripts.length - 1, to); 
     335    to = Math.min(this._scripts.length - 1, to); 
    295336  } else { // if destination is a script object 
    296     to = this.scripts.indexOf(destination); 
     337    to = this._scripts.indexOf(destination); 
    297338  } 
    298339 
     
    300341    return null; 
    301342 
    302   var tmp = this.scripts.splice(from, 1)[0]; 
    303   this.scripts.splice(to, 0, tmp); 
    304  
    305   return {from: from, to: to}; 
    306 }; 
    307  
    308 Config.prototype.__defineGetter__("scriptDir", function() { 
    309   var newDir = this.newScriptDir; 
     343  var tmp = this._scripts.splice(from, 1)[0]; 
     344  this._scripts.splice(to, 0, tmp); 
     345  this._changed(script, 'move', to); 
     346}; 
     347 
     348Config.prototype.__defineGetter__("_scriptDir", function() { 
     349  var newDir = this._newScriptDir; 
    310350  if (newDir.exists()) 
    311351    return newDir; 
    312352 
    313   var oldDir = this.oldScriptDir; 
     353  var oldDir = this._oldScriptDir; 
    314354  if (oldDir.exists()) 
    315355    return oldDir; 
     
    328368}); 
    329369 
    330 Config.prototype.__defineGetter__("newScriptDir", function() { 
     370Config.prototype.__defineGetter__("_newScriptDir", function() { 
    331371  var file = Components.classes["@mozilla.org/file/directory_service;1"] 
    332372                       .getService(Components.interfaces.nsIProperties) 
     
    336376}); 
    337377 
    338 Config.prototype.__defineGetter__("oldScriptDir", function() { 
     378Config.prototype.__defineGetter__("_oldScriptDir", function() { 
    339379  var file = getContentDir(); 
    340380  file.append("scripts"); 
     
    342382}); 
    343383 
     384Config.prototype.__defineGetter__("scripts", function() { 
     385  return this._scripts.concat(); 
     386}); 
     387 
     388Config.prototype.getScriptsForUrl = function(url, onlyEnabled) 
     389{ 
     390  var scripts = []; 
     391 
     392  scriptLoop: 
     393  for (var i = 0, script; script = this._scripts[i]; i++) { 
     394    if (script.enabled || !onlyEnabled) { 
     395      for (var j = 0, include; include = script._includes[j]; j++) { 
     396        if (convert2RegExp(include).test(url)) { 
     397          for (var k = 0, exclude; exclude = script._excludes[k]; k++) { 
     398            if (convert2RegExp(exclude).test(url)) { 
     399              continue scriptLoop; 
     400            } 
     401          } 
     402          scripts.push(script); 
     403          continue scriptLoop; 
     404        } 
     405      } 
     406    } 
     407  } 
     408 
     409  return scripts; 
     410}; 
     411 
    344412function Script(config) 
    345413{ 
    346414  this._config = config; 
    347   this.tempFile = null; // Only for scripts not installed 
    348   this.filename = null; 
    349   this.name = null; 
    350   this.namespace = null; 
    351   this.description = null; 
    352   this.enabled = true; 
    353   this.includes = []; 
    354   this.excludes = []; 
    355   this.basedir = null; 
    356   this.requires = []; 
    357   this.resources = []; 
     415  this._observers = []; 
     416 
     417  this._downloadUrl = null; // Only for scripts not installed 
     418  this._tempFile = null; // Only for scripts not installed 
     419  this._basedir = null; 
     420  this._filename = null; 
     421 
     422  this._name = null; 
     423  this._namespace = null; 
     424  this._description = null; 
     425  this._enabled = true; 
     426  this._includes = []; 
     427  this._excludes = []; 
     428  this._requires = []; 
     429  this._resources = []; 
    358430} 
    359431 
    360432Script.prototype = { 
    361   get file() 
    362   { 
    363     var file = this.basedirFile; 
    364     file.append(this.filename); 
     433  _changed: function(event, data) { this._config._changed(this, event, data); }, 
     434 
     435  get name() { return this._name; }, 
     436  get namespace() { return this._namespace; }, 
     437  get description() { return this._description; }, 
     438  get enabled() { return this._enabled; }, 
     439  set enabled(enabled) { this._enabled = enabled; this._changed('edit-enabled', enabled); }, 
     440 
     441  get includes() { return this._includes.concat(); }, 
     442  get excludes() { return this._excludes.concat(); }, 
     443  addInclude: function(url) { this._includes.push(url); this._changed('edit-include-add', url); }, 
     444  removeIncludeAt: function(index) { this._includes.splice(index, 1); this._changed('edit-include-remove', index); }, 
     445  addExclude: function(url) { this._excludes.push(url); this._changed('edit-exclude-add', url); }, 
     446  removeExcludeAt: function(index) { this._excludes.splice(index, 1); this._changed('edit-exclude-remove', index); }, 
     447 
     448  get requires() { return this._requires.concat(); }, 
     449  get resources() { return this._resources.concat(); }, 
     450 
     451  get _file() 
     452  { 
     453    var file = this._basedirFile; 
     454    file.append(this._filename); 
    365455    return file; 
    366456  }, 
    367457 
    368   get basedirFile() 
    369   { 
    370     var file = this._config.scriptDir; 
    371     file.append(this.basedir); 
     458  get editFile() { return this._file; }, 
     459 
     460  get _basedirFile() 
     461  { 
     462    var file = this._config._scriptDir; 
     463    file.append(this._basedir); 
    372464    return file; 
    373465  }, 
     466 
     467  get fileUrl() { return GM_getUriFromFile(this._file).spec; }, 
     468  get textContent() { return getContents(this._file); }, 
    374469 
    375470  _initFileName: function(name, useExt) 
     
    403498  _initFile: function(tempFile) 
    404499  { 
    405     var file = this._config.scriptDir; 
    406     var name = this._initFileName(this.name, false); 
     500    var file = this._config._scriptDir; 
     501    var name = this._initFileName(this._name, false); 
    407502 
    408503    file.append(name); 
    409504    file.createUnique(Components.interfaces.nsIFile.DIRECTORY_TYPE, 0755); 
    410     this.basedir = file.leafName; 
     505    this._basedir = file.leafName; 
    411506 
    412507    file.append(name + ".user.js"); 
    413508    file.createUnique(Components.interfaces.nsIFile.NORMAL_FILE_TYPE, 0644); 
    414     this.filename = file.leafName; 
     509    this._filename = file.leafName; 
    415510 
    416511    GM_log("Moving script file from " + tempFile.path + " to " + file.path); 
     
    418513    file.remove(true); 
    419514    tempFile.moveTo(file.parent, file.leafName); 
    420   } 
     515  }, 
     516 
     517  get urlToDownload() { return this._downloadUrl; }, 
     518  setDownloadedFile: function(file) { this._tempFile = file; } 
    421519} 
    422520 
     
    424522{ 
    425523  this._script = script; 
    426   this.url = null; // Only for scripts not installed 
    427   this.tempFile = null; // Only for scripts not installed 
    428   this.filename = null; 
     524 
     525  this._downloadUrl = null; // Only for scripts not installed 
     526  this._tempFile = null; // Only for scripts not installed 
     527  this._filename = null; 
    429528} 
    430529 
    431530ScriptRequire.prototype = { 
    432   get file() 
    433   { 
    434     var file = this._script.basedirFile; 
    435     file.append(this.filename); 
     531  get _file() 
     532  { 
     533    var file = this._script._basedirFile; 
     534    file.append(this._filename); 
    436535    return file; 
    437536  }, 
    438537 
     538  get fileUrl() { return GM_getUriFromFile(this._file).spec; }, 
     539  get textContent() { return getContents(this._file); }, 
     540 
    439541  _initFile: function() 
    440542  { 
    441     var name = this.url.substr(this.url.lastIndexOf("/") + 1); 
     543    var name = this.url.substr(this._downloadUrl.lastIndexOf("/") + 1); 
    442544    if(name.indexOf("?") > 0) 
    443545      name = name.substr(0, name.indexOf("?")); 
    444546    name = this._script._initFileName(name, true); 
    445547 
    446     var file = this._script.basedirFile; 
     548    var file = this._script._basedirFile; 
    447549    file.append(name); 
    448550    file.createUnique(Components.interfaces.nsIFile.NORMAL_FILE_TYPE, 0644); 
    449     this.filename = file.leafName; 
    450  
    451     GM_log("Moving dependency file from " + this.tempFile.path + " to " + file.path); 
     551    this._filename = file.leafName; 
     552 
     553    GM_log("Moving dependency file from " + this._tempFile.path + " to " + file.path); 
    452554 
    453555    file.remove(true); 
    454     this.tempFile.moveTo(file.parent, file.leafName); 
    455     this.tempFile = null; 
    456   } 
     556    this._tempFile.moveTo(file.parent, file.leafName); 
     557    this._tempFile = null; 
     558  }, 
     559 
     560  get urlToDownload() { return this._downloadUrl; }, 
     561  setDownloadedFile: function(file) { this._tempFile = file; } 
    457562} 
    458563 
     
    460565{ 
    461566  this._script = script; 
    462   this.url = null; // Only for scripts not installed 
    463   this.tempFile = null; // Only for scripts not installed 
    464   this.name = null; 
    465   this.filename = null; 
    466   this.mimetype = null; 
    467   this.charset = null; 
     567 
     568  this._downloadUrl = null; // Only for scripts not installed 
     569  this._tempFile = null; // Only for scripts not installed 
     570  this._filename = null; 
     571  this._mimetype = null; 
     572  this._charset = null; 
     573 
     574  this._name = null; 
    468575} 
    469576 
    470577ScriptResource.prototype = { 
    471   get file() 
    472   { 
    473     var file = this._script.basedirFile; 
    474     file.append(this.filename); 
     578  get name() { return this._name; }, 
     579 
     580  get _file() 
     581  { 
     582    var file = this._script._basedirFile; 
     583    file.append(this._filename); 
    475584    return file; 
    476585  }, 
    477    
    478   _initFile: ScriptRequire.prototype._initFile 
    479 
     586 
     587  get textContent() { return getContents(this._file); }, 
     588 
     589  get dataContent() 
     590  { 
     591    var appSvc = Components.classes["@mozilla.org/appshell/appShellService;1"] 
     592                           .getService(Components.interfaces.nsIAppShellService); 
     593 
     594    var window = appSvc.hiddenDOMWindow; 
     595    var binaryContents = getBinaryContents(this._file); 
     596 
     597    var mimetype = this._mimetype; 
     598    if(this._charset && this._charset.length > 0){ 
     599      mimetype += ";charset=" + this._charset; 
     600    } 
     601 
     602    return "data:" + mimetype + ";base64," + 
     603      window.encodeURIComponent(window.btoa(binaryContents)); 
     604  }, 
     605 
     606  _initFile: ScriptRequire.prototype._initFile, 
     607 
     608  get urlToDownload() { return this._downloadUrl; }, 
     609  setDownloadedFile: function(tempFile, mimetype, charset) 
     610  { 
     611    this._tempFile = tempFile; 
     612    this._mimetype = mimetype; 
     613    this._charset = charset; 
     614  } 
     615
  • branches/config-service/src/chrome/chromeFiles/content/install.xul

    r547 r661  
    2626 
    2727  <script type="application/x-javascript" src="chrome://greasemonkey/content/utils.js" /> 
    28   <script type="application/x-javascript" src="chrome://greasemonkey/content/config.js" /> 
    2928  <script type="application/x-javascript" src="chrome://greasemonkey/content/scriptdownloader.js" /> 
    3029  <script type="application/x-javascript" src="chrome://greasemonkey/content/prefmanager.js" /> 
  • branches/config-service/src/chrome/chromeFiles/content/manage.css

    r546 r661  
    44the original starting size of the elements before flexing. 
    55*/ 
    6 dialog
     6window
    77  width: 600px; 
    8   height: 500px 
     8  height: 500px; 
     9  margin: 10px; 
    910} 
    1011#col_left { 
  • branches/config-service/src/chrome/chromeFiles/content/manage.js

    r660 r661  
    1 var config = new Config(); 
    2 var uninstallList = []; 
     1var config = GM_getConfig(); 
    32 
    43window.addEventListener("load", function(ev) { 
    5   config.load(); 
    64  loadControls(); 
    75 
     
    108    chooseScript(0); 
    119  } 
     10 
     11  config.addObserver(observer); 
    1212}, false); 
    1313 
    14 function handleOkButton() { 
    15   var uninstallPrefs = document.getElementById('chkUninstallPrefs').checked; 
    16  
    17   for (var i = 0, script = null; (script = uninstallList[i]); i++) { 
    18     config.uninstall(script, uninstallPrefs); 
    19   } 
    20   config.save(); 
    21   return true; 
     14window.addEventListener("unload", function(ev) { 
     15  pagesControl.clear(); 
     16  config.removeObserver(observer); 
     17}, false); 
     18 
     19var observer = { 
     20  notifyEvent: function(script, event, data) 
     21  { 
     22    var node = null; 
     23    for (var i = 0; node = listbox.childNodes[i]; i++) 
     24      if (node.script == script) 
     25        break; 
     26 
     27    switch (event) { 
     28    case "edit-enabled": 
     29      node.style.color = data ? '' : 'gray'; 
     30      if (script == selectedScript) 
     31        chkEnabled.checked = data; 
     32      break; 
     33    case "install": 
     34      addListitem(script, -1); 
     35      break; 
     36    case "uninstall": 
     37      var selected = listbox.selectedItem == node; 
     38      listbox.removeChild(node); 
     39 
     40      if (selected && listbox.childNodes.length > 0) { 
     41        chooseScript(Math.max(Math.min(listbox.selectedIndex, listbox.childNodes.length - 1), 0)); 
     42      } 
     43      break; 
     44    case "move": 
     45      listbox.removeChild(node); 
     46      listbox.insertBefore(node, listbox.childNodes[data]); 
     47      // then re-select the dropped script 
     48      listbox.selectedIndex = data; 
     49      break; 
     50    } 
     51 
     52    // fix the listbox indexes 
     53    for (var i=0, n=null; n=listbox.childNodes[i]; i++) n.index=i; 
     54  } 
    2255}; 
    2356 
     
    3972  btnUninstall.addEventListener("command", function() { handleUninstallButton(); }, false); 
    4073  chkEnabled.addEventListener("command", function() { 
    41      if (selectedScript) { 
     74     if (selectedScript) 
    4275       selectedScript.enabled = chkEnabled.checked; 
    43        if (selectedScript.enabled) { 
    44          listbox.selectedItem.style.color = ''; 
    45        } else { 
    46          listbox.selectedItem.style.color = 'gray'; 
    47        } 
    48      } 
    4976  }, false); 
    5077}; 
     
    5784    chkEnabled.checked = true; 
    5885    pagesControl.clear(); 
    59     document.documentElement.getButton("accept").disabled = false; 
    6086  } else { 
    6187    selectedScript = listbox.getSelectedItem(0).script; 
     
    86112 
    87113function handleUninstallButton() { 
    88   var index=listbox.selectedIndex; 
    89  
    90   // mark script to be uninstalled on "OK" 
    91   uninstallList.push(selectedScript); 
    92  
    93   // remove it from the display 
    94   listbox.removeChild(listbox.childNodes[index]); 
    95  
    96   if (listbox.childNodes.length > 0) { 
    97     chooseScript(Math.max(Math.min(listbox.selectedIndex, listbox.childNodes.length - 1), 0)); 
    98   } 
     114  var uninstallPrefs = document.getElementById('chkUninstallPrefs').checked; 
     115  config.uninstall(selectedScript, uninstallPrefs); 
    99116}; 
    100117 
    101118function populateChooser() { 
    102   for (var i = 0, script = null; (script = config.scripts[i]); i++) { 
    103     var listitem = document.createElement("listitem"); 
    104  
    105     listitem.setAttribute("label", script.name); 
    106     listitem.setAttribute("crop", "end"); 
    107     listitem.script = script; 
    108     listitem.index = i; 
    109  
    110     if (!script.enabled) { 
    111       listitem.style.color = 'gray'; 
    112     } 
     119  var scripts = config.scripts; 
     120  for (var i = 0, script = null; (script = scripts[i]); i++) 
     121    addListitem(script, i); 
     122}; 
     123 
     124function addListitem(script, i) { 
     125  var listitem = document.createElement("listitem"); 
     126 
     127  listitem.setAttribute("label", script.name); 
     128  listitem.setAttribute("crop", "end"); 
     129  listitem.script = script; 
     130  listitem.index = i; 
     131 
     132  if (!script.enabled) { 
     133    listitem.style.color = 'gray'; 
     134  } 
    113135 
    114136  listbox.appendChild(listitem); 
    115   } 
    116137}; 
    117138 
     
    119140  listbox.selectedIndex = index; 
    120141  listbox.focus(); 
    121 }; 
    122  
    123 function toggleScript(index, enableFlag) { 
    124   var listitem = listbox.childNodes[index]; 
    125   if (enableFlag) { 
    126     listitem.script.enabled = true; 
    127     listitem.style.color = ''; 
    128   } else { 
    129     listitem.script.enabled = false; 
    130     listitem.style.color = 'gray'; 
    131   } 
    132 }; 
    133  
    134 function reorderScript(from, to) { 
    135   // REORDER DISPLAY: 
    136   var tmp = listbox.childNodes[from]; 
    137   listbox.removeChild(tmp); 
    138   listbox.insertBefore(tmp, listbox.childNodes[to]); 
    139   // fix the listbox indexes 
    140   for (var i=0, node=null; node=listbox.childNodes[i]; i++) { 
    141     node.index=i; 
    142   } 
    143  
    144   // then re-select the dropped script 
    145   listbox.selectedIndex = to; 
    146142}; 
    147143 
     
    158154  else if (KeyEvent.DOM_VK_DOWN == event.keyCode) 
    159155    move = config.move(listbox.selectedItem.script, 1); 
    160  
    161   if (move) 
    162     reorderScript(move.from, move.to); 
    163156}; 
    164157 
     
    204197    // do the move 
    205198    var move = config.move(config.scripts[index], config.scripts[newIndex]); 
    206     if (move) 
    207       reorderScript(move.from, move.to); 
    208199  }, 
    209200 
  • branches/config-service/src/chrome/chromeFiles/content/manage.xul

    r592 r661  
    66<!DOCTYPE dialog SYSTEM "chrome://greasemonkey/locale/greasemonkey.dtd"> 
    77 
    8 <dialog id="greasemonkey" 
     8<window id="greasemonkey" 
     9  windowtype="Greasemonkey:Manage" 
    910  xmlns="http://www.mozilla.org/keymaster/gatekeeper/there.is.only.xul" 
    10   buttons="accept,cancel" 
    11   ondialogaccept="return handleOkButton()" 
    1211  title="&manage.title;" 
    1312  orient="vertical"> 
     
    1716  <script type="application/x-javascript" src="chrome://greasemonkey/content/utils.js" /> 
    1817  <script type="application/x-javascript" src="chrome://greasemonkey/content/prefmanager.js" /> 
    19   <script type="application/x-javascript" src="chrome://greasemonkey/content/config.js" /> 
    2018  <script type="application/x-javascript" src="chrome://greasemonkey/content/manage.js" /> 
    2119 
     
    7068  </grid> 
    7169 
    72 </dialog
     70</window
  • branches/config-service/src/chrome/chromeFiles/content/miscapis.js

    r655 r661  
    3232  } 
    3333 
    34   var dep = this.getDep_(name); 
    35  
    36   var ioService = Components.classes["@mozilla.org/network/io-service;1"] 
    37                             .getService(Components.interfaces.nsIIOService); 
    38   var appSvc = Components.classes["@mozilla.org/appshell/appShellService;1"] 
    39                          .getService(Components.interfaces.nsIAppShellService); 
    40  
    41   var window = appSvc.hiddenDOMWindow; 
    42   var binaryContents = getBinaryContents(dep.file); 
    43  
    44   var mimetype = dep.mimetype; 
    45   if(dep.charset && dep.charset.length > 0){ 
    46     mimetype += ";charset=" + dep.charset; 
    47   } 
    48  
    49   return "data:" + mimetype + ";base64," + 
    50     window.encodeURIComponent(window.btoa(binaryContents));