Changeset 634
- Timestamp:
- 01/20/08 05:17:25 (11 months ago)
- Files:
Legend:
- Unmodified
- Added
- Removed
- Modified
- Copied
- Moved
branches/jhs1/chrome/chromeFiles/content/config.js
r625 r634 287 287 this.file = null; 288 288 this.filename = null; 289 this.charset = null; 289 290 }; 290 291 … … 295 296 this.filename = null; 296 297 this.mimetype = null; 297 }; 298 this.charset = null; 299 }; branches/jhs1/chrome/chromeFiles/content/scriptdownloader.js
r625 r634 44 44 // If loading from file, status might be zero on success 45 45 if (this.req_.status != 200 && this.req_.status != 0) { 46 // NOTE: Unlocalized string47 alert( 'Error loading user script:\n'+48 this.req_.status + ": " +49 this.req_.statusText);46 // NOTE: i18n 47 alert("Error loading user script:\n" + 48 this.req_.status + ": " + 49 this.req_.statusText); 50 50 return; 51 51 } … … 76 76 window.setTimeout(GM_hitch(this, "fetchDependencies"), 0); 77 77 78 if (this.installing_){78 if (this.installing_) { 79 79 this.showInstallDialog(); 80 } else{80 } else { 81 81 this.showScriptView(); 82 82 } 83 83 } catch (e) { 84 // NOTE: unlocalized string84 // NOTE: i18n 85 85 alert("Script could not be installed " + e); 86 86 throw e; … … 104 104 }; 105 105 106 ScriptDownloader.prototype.downloadNextDependency = function() {106 ScriptDownloader.prototype.downloadNextDependency = function() { 107 107 if (this.depQueue_.length > 0) { 108 108 var dep = this.depQueue_.pop(); 109 try { 110 var persist = Components.classes[ 111 "@mozilla.org/embedding/browser/nsWebBrowserPersist;1"] 112 .createInstance(Components.interfaces.nsIWebBrowserPersist); 113 persist.persistFlags = 114 persist.PERSIST_FLAGS_BYPASS_CACHE | 115 persist.PERSIST_FLAGS_REPLACE_EXISTING_FILES; //doesn't work? 116 var ioservice = 117 Components.classes["@mozilla.org/network/io-service;1"] 118 .getService(); 119 var sourceUri = ioservice.newURI(dep.url, null, null); 120 var sourceChannel = ioservice.newChannelFromURI(sourceUri); 121 sourceChannel.notificationCallbacks = new NotificationCallbacks(); 122 123 var file = getTempFile(); 124 125 var progressListener = new PersistProgressListener(persist); 126 progressListener.onFinish = GM_hitch(this, 127 "handleDependencyDownloadComplete", dep, file, sourceChannel); 128 persist.progressListener = progressListener; 129 130 persist.saveChannel(sourceChannel, file); 131 } catch(e) { 132 GM_log("Download exception " + e); 133 this.errorInstallDependency(this.script, dep, e); 134 } 109 this.downloadFile(dep.url, "handleDependencyDownloadComplete", 110 "errorInstallDependency", dep); 135 111 } else { 136 112 this.dependenciesLoaded_ = true; … … 140 116 141 117 ScriptDownloader.prototype.handleDependencyDownloadComplete = 142 function( dep, file, channel) {118 function(file, channel, dep) { 143 119 GM_log("Dependency Download complete " + dep.url); 144 120 try { … … 152 128 if (httpChannel.requestSucceeded) { 153 129 dep.file = file; 154 dep.mimetype= channel.contentType; 130 if (dep.hasOwnProperty("mimetype")) 131 dep.mimetype = channel.contentType; 155 132 if (channel.contentCharset) { 156 133 dep.charset = channel.contentCharset; … … 158 135 this.downloadNextDependency(); 159 136 } else { 160 this.errorInstallDependency(this.script, dep, 161 "Error! Server Returned : " + httpChannel.responseStatus + ": " + 162 httpChannel.responseStatusText); 137 var error = new Error("Error! Server returned: " + 138 httpChannel.responseStatus + ": " + 139 httpChannel.responseStatusText); 140 this.errorInstallDependency(error, dep); 163 141 } 164 142 } else { … … 180 158 case "file": 181 159 var scriptScheme = ioService.extractScheme(this.uri_.spec); 182 return (scriptScheme == "file") 160 return (scriptScheme == "file"); 183 161 default: 184 162 return false; … … 192 170 }; 193 171 194 ScriptDownloader.prototype.errorInstallDependency = function(script, dep, msg){ 172 ScriptDownloader.prototype.errorInstallDependency = function(e, dep) { 173 var msg = e.message; 195 174 GM_log("Error loading dependency " + dep.url + "\n" + msg) 196 175 if (this.installOnCompletion_) { … … 226 205 }; 227 206 228 ScriptDownloader.prototype.parseScript = function(source, uri ) {207 ScriptDownloader.prototype.parseScript = function(source, uri, script, headers) { 229 208 function resolveURL(url, baseurl) { 230 209 url = ioservice.newURI(url, null, baseurl); … … 234 213 .getService(); 235 214 236 var script = new Script(); 237 script.uri = uri; 238 script.enabled = true; 239 var headers = GM_parseScriptHeaders(source, { 215 headers = GM_parseScriptHeaders(source, { 240 216 // verbatim string, last value only: 241 217 name:1, … … 276 252 return new ScriptResource(name, resolveURL(url, uri)); 277 253 } 278 }); 279 280 script.name = headers.name || parseScriptName(uri); 281 script.namespace = headers.namespace || uri.host; 282 script.description = headers.description || ""; 254 }, headers); 255 256 if (!script) { 257 script = this.script = new Script(); 258 script.uri = uri; 259 script.enabled = true; 260 script.name = headers.name || parseScriptName(uri); 261 script.namespace = headers.namespace || uri.host; 262 script.description = headers.description || ""; 263 } 283 264 script.includes = headers.include || ["*"]; 284 265 script.excludes = headers.exclude || []; … … 286 267 script.resources = headers.resource || []; 287 268 288 this.script = script; 289 }; 290 269 return headers; 270 }; 271 272 /** 273 * Download and save url into a temporary file. On successful completion, call 274 * onOK(file, channel), otherwise onFail(exception). Additional arguments get 275 * passed on to either callback, and the this object is retained. 276 */ 277 ScriptDownloader.prototype.downloadFile = function(url, onOK, onFail) { 278 var args = [].slice.call(arguments, 3); // trailing args for the callbacks 279 try { 280 var ioservice = 281 Components.classes["@mozilla.org/network/io-service;1"].getService(); 282 var uri = ioservice.newURI(url, null, null); 283 284 var channel = ioservice.newChannelFromURI(uri); 285 channel.notificationCallbacks = new NotificationCallbacks(); 286 287 var file = getTempFile(); 288 289 args.unshift(this, onOK, file, channel); 290 onOK = GM_hitch.apply(args); 291 var progressListener = new PersistProgressListener(onOK); 292 progressListener.persist.saveChannel(channel, file); 293 } catch(e) { 294 GM_log("Download exception " + e); 295 args.unshift(this, onFail, e); 296 onFail = GM_hitch.apply(args); 297 onFail(); 298 } 299 } 291 300 292 301 function NotificationCallbacks() { … … 310 319 311 320 312 function PersistProgressListener(persist){ 321 function PersistProgressListener(doneCallback) { 322 var persist = Components.classes[ 323 "@mozilla.org/embedding/browser/nsWebBrowserPersist;1"] 324 .createInstance(Components.interfaces.nsIWebBrowserPersist); 325 persist.persistFlags = 326 persist.PERSIST_FLAGS_BYPASS_CACHE | 327 persist.PERSIST_FLAGS_REPLACE_EXISTING_FILES; //doesn't work? 328 persist.progressListener = this; 329 330 this.onFinish = doneCallback; 313 331 this.persist = persist; 314 this.onFinish = function(){};315 this.persiststate = "";316 332 }; 317 333 branches/jhs1/chrome/chromeFiles/content/utils.js
r625 r634 27 27 28 28 function GM_hitch(obj, meth) { 29 if (!obj[meth]) { 30 throw "method '" + meth + "' does not exist on object '" + obj + "'"; 29 if (typeof meth != "function") { 30 if (!obj[meth]) { 31 throw "method '" + meth + "' does not exist on object '" + obj + "'"; 32 } 33 meth = obj[meth]; 31 34 } 32 35 … … 45 48 // invoke the original function with the correct this obj and the combined 46 49 // list of static and dynamic arguments. 47 return obj[meth].apply(obj, args);50 return meth.apply(obj, args); 48 51 }; 49 52 }; … … 498 501 * 499 502 * If, instead of 1, a callback function is provided, the return value of that 500 * callback isbecomes appended to the array instead. This callback is invoked503 * callback becomes appended to the array instead. This callback is invoked 501 504 * with two arguments: the raw header, and the array with all prior callback 502 505 * results for this header (or the empty array).
