| 24 | | |
|---|
| 25 | | Config.prototype.initFilename = function(script) { |
|---|
| 26 | | var index = {}; |
|---|
| 27 | | var base = script.name.replace(/[^A-Z0-9_]/gi, "").toLowerCase(); |
|---|
| 28 | | |
|---|
| 29 | | // If no Latin characters found - use default |
|---|
| 30 | | if (!base) { |
|---|
| 31 | | base = "gm_script"; |
|---|
| 32 | | } |
|---|
| 33 | | |
|---|
| 34 | | // 24 is a totally arbitrary max length |
|---|
| 35 | | if (base.length > 24) { |
|---|
| 36 | | base = base.substring(0, 24); |
|---|
| 37 | | } |
|---|
| 38 | | |
|---|
| 39 | | for (var i = 0; i < this.scripts.length; i++) { |
|---|
| 40 | | index[this.scripts[i].basedir] = this.scripts[i]; |
|---|
| 41 | | } |
|---|
| 42 | | |
|---|
| 43 | | if (!index[base]) { |
|---|
| 44 | | script.filename = base + ".user.js"; |
|---|
| 45 | | script.basedir = base; |
|---|
| 46 | | return; |
|---|
| 47 | | } |
|---|
| 48 | | |
|---|
| 49 | | for (var count = 1; count < Number.MAX_VALUE; count++) { |
|---|
| 50 | | if (!index[base + count]) { |
|---|
| 51 | | script.filename = base + ".user.js"; |
|---|
| 52 | | script.basedir = base + "("+ count + ")"; |
|---|
| 53 | | return; |
|---|
| 54 | | } |
|---|
| 55 | | |
|---|
| 56 | | if (!index[filename]) { |
|---|
| 57 | | // Check to make sure there's no file already in that space. |
|---|
| 58 | | var file = this.scriptDir.clone(); |
|---|
| 59 | | file.append(filename); |
|---|
| 60 | | if (!file.exists()) { |
|---|
| 61 | | script.filename = filename; |
|---|
| 62 | | return; |
|---|
| 63 | | } |
|---|
| 64 | | } |
|---|
| 65 | | } |
|---|
| 66 | | // NOTE: non localised string |
|---|
| 67 | | throw new Error("doooooooode. get some different user script or something."); |
|---|
| 68 | | }; |
|---|
| 69 | | |
|---|
| 70 | | Config.prototype.initDependencyFilename = function(script, req) { |
|---|
| 71 | | var remoteFilename = req.url.substr(req.url.lastIndexOf("/") + 1); |
|---|
| 72 | | |
|---|
| 73 | | if(remoteFilename.indexOf("?")>0){ |
|---|
| 74 | | remoteFilename = remoteFilename.substr(0, remoteFilename.indexOf("?")); |
|---|
| 75 | | } |
|---|
| 76 | | |
|---|
| 77 | | var dotIndex = remoteFilename.lastIndexOf("."); |
|---|
| 78 | | if (dotIndex > 0) { |
|---|
| 79 | | var base = remoteFilename.substring(0, dotIndex); |
|---|
| 80 | | var ext = remoteFilename.substring(dotIndex+1); |
|---|
| 81 | | } else { |
|---|
| 82 | | var base = remoteFilename; |
|---|
| 83 | | var ext = ""; |
|---|
| 84 | | } |
|---|
| 85 | | |
|---|
| 86 | | ext = ext.replace(/[^A-Z0-9_]/gi, ""); |
|---|
| 87 | | base = base.replace(/[^A-Z0-9_]/gi, ""); |
|---|
| 88 | | |
|---|
| 89 | | if (base.length > 24) { |
|---|
| 90 | | base = base.substring(0, 24); |
|---|
| 91 | | } |
|---|
| 92 | | |
|---|
| 93 | | if (ext.length > 0){ |
|---|
| 94 | | ext = "."+ext; |
|---|
| 95 | | } |
|---|
| 96 | | |
|---|
| 97 | | for (var count = 0; count < Number.MAX_VALUE; count++) { |
|---|
| 98 | | var stamp = (count > 0) ? "(" + count + ")" : ""; |
|---|
| 99 | | var filename = base + stamp + ext; |
|---|
| 100 | | var file = script.basedirFile; |
|---|
| 101 | | file.append(filename); |
|---|
| 102 | | |
|---|
| 103 | | if (!file.exists()) { |
|---|
| 104 | | return filename; |
|---|
| 105 | | } |
|---|
| 106 | | } |
|---|
| 107 | | return undefined; |
|---|
| 108 | | } |
|---|
| 227 | | var existingFile = null; |
|---|
| 228 | | var oldScripts = new Array(this.scripts); |
|---|
| 229 | | |
|---|
| 230 | | if (existingIndex > -1) { |
|---|
| 231 | | existingFile = this.scripts[existingIndex].basedirFile; |
|---|
| 232 | | existingFile.normalize(); |
|---|
| 233 | | if (existingFile.equals(this.scriptDir)) { |
|---|
| 234 | | existingFile = this.scripts[existingIndex].file; |
|---|
| 235 | | } |
|---|
| 236 | | if (existingFile.exists()) { |
|---|
| 237 | | existingFile.remove(true); |
|---|
| 238 | | } |
|---|
| 239 | | this.scripts.splice(existingIndex, 1); |
|---|
| 240 | | } |
|---|
| 241 | | |
|---|
| 242 | | this.initFilename(script); |
|---|
| 243 | | newDir.append(script.basedir); |
|---|
| 244 | | script.tempFile.copyTo(newDir, script.filename); |
|---|
| 245 | | |
|---|
| 246 | | for (var i = 0; i < script.requires.length; i++) { |
|---|
| 247 | | this.installDependency(script, script.requires[i]); |
|---|
| 248 | | } |
|---|
| 249 | | |
|---|
| 250 | | for (var i = 0; i < script.resources.length; i++) { |
|---|
| 251 | | this.installDependency(script, script.resources[i]); |
|---|
| 252 | | } |
|---|
| | 141 | if (existingIndex > -1) |
|---|
| | 142 | this.uninstall(this.scripts[existingIndex], false); |
|---|
| | 143 | |
|---|
| | 144 | script._initFile(script.tempFile); |
|---|
| | 145 | script.tempFile = null; |
|---|
| | 146 | |
|---|
| | 147 | for (var i = 0; i < script.requires.length; i++) |
|---|
| | 148 | script.requires[i]._initFile(); |
|---|
| | 149 | |
|---|
| | 150 | for (var i = 0; i < script.resources.length; i++) |
|---|
| | 151 | script.resources[i]._initFile(); |
|---|
| 277 | | |
|---|
| 278 | | Config.prototype.installDependency = function(script, req){ |
|---|
| 279 | | GM_log("Installing dependency: " + req.url + " from " + req.tempFile.path); |
|---|
| 280 | | |
|---|
| 281 | | var scriptDir = this.scriptDir; |
|---|
| 282 | | GM_log("Installing to " + script.basedir); |
|---|
| 283 | | scriptDir.append(script.basedir); |
|---|
| 284 | | |
|---|
| 285 | | req.filename = this.initDependencyFilename(script, req); |
|---|
| 286 | | GM_log("Installing as: " + req.filename); |
|---|
| 287 | | |
|---|
| 288 | | try { |
|---|
| 289 | | req.tempFile.copyTo(scriptDir, req.filename) |
|---|
| 290 | | } catch(e) { |
|---|
| 291 | | throw e; |
|---|
| 292 | | } |
|---|
| 293 | | }; |
|---|
| | 243 | }, |
|---|
| | 244 | |
|---|
| | 245 | _initFileName: function(name, useExt) |
|---|
| | 246 | { |
|---|
| | 247 | var ext = ""; |
|---|
| | 248 | name = name.toLowerCase(); |
|---|
| | 249 | |
|---|
| | 250 | var dotIndex = name.lastIndexOf("."); |
|---|
| | 251 | if (dotIndex > 0 && useExt) { |
|---|
| | 252 | ext = name.substring(dotIndex + 1); |
|---|
| | 253 | name = name.substring(0, dotIndex); |
|---|
| | 254 | } |
|---|
| | 255 | |
|---|
| | 256 | name = name.replace(/[^A-Z0-9_]/gi, ""); |
|---|
| | 257 | ext = ext.replace(/[^A-Z0-9_]/gi, ""); |
|---|
| | 258 | |
|---|
| | 259 | // If no Latin characters found - use default |
|---|
| | 260 | if (!name) |
|---|
| | 261 | name = "gm_script"; |
|---|
| | 262 | |
|---|
| | 263 | // 24 is a totally arbitrary max length |
|---|
| | 264 | if (name.length > 24) |
|---|
| | 265 | name = name.substring(0, 24); |
|---|
| | 266 | |
|---|
| | 267 | if (ext) |
|---|
| | 268 | name += "." + ext; |
|---|
| | 269 | |
|---|
| | 270 | return name; |
|---|
| | 271 | }, |
|---|
| | 272 | |
|---|
| | 273 | _initFile: function(tempFile) |
|---|
| | 274 | { |
|---|
| | 275 | var file = this._config.scriptDir; |
|---|
| | 276 | var name = this._initFileName(this.name, false); |
|---|
| | 277 | |
|---|
| | 278 | file.append(name); |
|---|
| | 279 | file.createUnique(Components.interfaces.nsIFile.DIRECTORY_TYPE, 0755); |
|---|
| | 280 | this.basedir = file.leafName; |
|---|
| | 281 | |
|---|
| | 282 | file.append(name + ".user.js"); |
|---|
| | 283 | file.createUnique(Components.interfaces.nsIFile.NORMAL_FILE_TYPE, 0644); |
|---|
| | 284 | this.filename = file.leafName; |
|---|
| | 285 | |
|---|
| | 286 | GM_log("Moving script file from " + tempFile.path + " to " + file.path); |
|---|
| | 287 | |
|---|
| | 288 | file.remove(true); |
|---|
| | 289 | tempFile.moveTo(file.parent, file.leafName); |
|---|
| | 307 | }, |
|---|
| | 308 | |
|---|
| | 309 | _initFile: function() |
|---|
| | 310 | { |
|---|
| | 311 | var name = this.url.substr(this.url.lastIndexOf("/") + 1); |
|---|
| | 312 | if(name.indexOf("?") > 0) |
|---|
| | 313 | name = name.substr(0, name.indexOf("?")); |
|---|
| | 314 | name = this._script._initFileName(name, true); |
|---|
| | 315 | |
|---|
| | 316 | var file = this._script.basedirFile; |
|---|
| | 317 | file.append(name); |
|---|
| | 318 | file.createUnique(Components.interfaces.nsIFile.NORMAL_FILE_TYPE, 0644); |
|---|
| | 319 | this.filename = file.leafName; |
|---|
| | 320 | |
|---|
| | 321 | GM_log("Moving dependency file from " + this.tempFile.path + " to " + file.path); |
|---|
| | 322 | |
|---|
| | 323 | file.remove(true); |
|---|
| | 324 | this.tempFile.moveTo(file.parent, file.leafName); |
|---|
| | 325 | this.tempFile = null; |
|---|