Skip to content

Commit

Permalink
Move Script, ScriptRequire, ScriptResource objects into their own fil…
Browse files Browse the repository at this point in the history
…es (out of config.js).
  • Loading branch information
arantius committed May 8, 2010
1 parent 81f0a05 commit 3991b65
Show file tree
Hide file tree
Showing 5 changed files with 230 additions and 230 deletions.
3 changes: 3 additions & 0 deletions components/greasemonkey.js
Expand Up @@ -138,6 +138,9 @@ var greasemonkeyService = {
loader.loadSubScript("chrome://greasemonkey/content/prefmanager.js");
loader.loadSubScript("chrome://greasemonkey/content/utils.js");
loader.loadSubScript("chrome://greasemonkey/content/config.js");
loader.loadSubScript("chrome://greasemonkey/content/script.js");
loader.loadSubScript("chrome://greasemonkey/content/scriptrequire.js");
loader.loadSubScript("chrome://greasemonkey/content/scriptresource.js");
loader.loadSubScript("chrome://greasemonkey/content/convert2RegExp.js");
loader.loadSubScript("chrome://greasemonkey/content/miscapis.js");
loader.loadSubScript("chrome://greasemonkey/content/xmlhttprequester.js");
Expand Down
230 changes: 0 additions & 230 deletions content/config.js
Expand Up @@ -555,233 +555,3 @@ Config.prototype = {
scriptDir.copyTo(scriptDirBackup.parent, scriptDirBackup.leafName);
}
};

function Script(config) {
this._config = config;
this._observers = [];

this._downloadURL = null; // Only for scripts not installed
this._tempFile = null; // Only for scripts not installed
this._basedir = null;
this._filename = null;
this._modified = null;
this._dependhash = null;

this._name = null;
this._namespace = null;
this._description = null;
this._enabled = true;
this._includes = [];
this._excludes = [];
this._requires = [];
this._resources = [];
this._unwrap = false;
this._dependFail = false
this.delayInjection = false;
this._rawMeta = null;
}

Script.prototype = {
matchesURL: function(url) {
function test(page) {
return convert2RegExp(page).test(url);
}

return this._includes.some(test) && !this._excludes.some(test);
},

_changed: function(event, data) { this._config._changed(this, event, data); },

get name() { return this._name; },
get namespace() { return this._namespace; },
get description() { return this._description; },
get enabled() { return this._enabled; },
set enabled(enabled) { this._enabled = enabled; this._changed("edit-enabled", enabled); },

get includes() { return this._includes.concat(); },
get excludes() { return this._excludes.concat(); },
addInclude: function(url) { this._includes.push(url); this._changed("edit-include-add", url); },
removeIncludeAt: function(index) { this._includes.splice(index, 1); this._changed("edit-include-remove", index); },
addExclude: function(url) { this._excludes.push(url); this._changed("edit-exclude-add", url); },
removeExcludeAt: function(index) { this._excludes.splice(index, 1); this._changed("edit-exclude-remove", index); },

get requires() { return this._requires.concat(); },
get resources() { return this._resources.concat(); },
get unwrap() { return this._unwrap; },

get _file() {
var file = this._basedirFile;
file.append(this._filename);
return file;
},

get editFile() { return this._file; },

get _basedirFile() {
var file = this._config._scriptDir;
file.append(this._basedir);
file.normalize();
return file;
},

get fileURL() { return GM_getUriFromFile(this._file).spec; },
get textContent() { return getContents(this._file); },

_initFileName: function(name, useExt) {
var ext = "";
name = name.toLowerCase();

var dotIndex = name.lastIndexOf(".");
if (dotIndex > 0 && useExt) {
ext = name.substring(dotIndex + 1);
name = name.substring(0, dotIndex);
}

name = name.replace(/\s+/g, "_").replace(/[^-_A-Z0-9]+/gi, "");
ext = ext.replace(/\s+/g, "_").replace(/[^-_A-Z0-9]+/gi, "");

// If no Latin characters found - use default
if (!name) name = "gm_script";

// 24 is a totally arbitrary max length
if (name.length > 24) name = name.substring(0, 24);

if (ext) name += "." + ext;

return name;
},

_initFile: function(tempFile) {
var file = this._config._scriptDir;
var name = this._initFileName(this._name, false);

file.append(name);
file.createUnique(Components.interfaces.nsIFile.DIRECTORY_TYPE, 0755);
this._basedir = file.leafName;

file.append(name + ".user.js");
file.createUnique(Components.interfaces.nsIFile.NORMAL_FILE_TYPE, 0644);
this._filename = file.leafName;

GM_log("Moving script file from " + tempFile.path + " to " + file.path);

file.remove(true);
tempFile.moveTo(file.parent, file.leafName);
},

get urlToDownload() { return this._downloadURL; },
setDownloadedFile: function(file) { this._tempFile = file; },

get previewURL() {
return Components.classes["@mozilla.org/network/io-service;1"]
.getService(Components.interfaces.nsIIOService)
.newFileURI(this._tempFile).spec;
},

isModified: function() {
this.delayInjection = false;
if (this._modified != this._file.lastModifiedTime) {
this._modified = this._file.lastModifiedTime;
return true;
}
return false;
}
};

function ScriptRequire(script) {
this._script = script;

this._downloadURL = null; // Only for scripts not installed
this._tempFile = null; // Only for scripts not installed
this._filename = null;
this.updateScript = false;
}

ScriptRequire.prototype = {
get _file() {
var file = this._script._basedirFile;
file.append(this._filename);
return file;
},

get fileURL() { return GM_getUriFromFile(this._file).spec; },
get textContent() { return getContents(this._file); },

_initFile: function() {
var name = this._downloadURL.substr(this._downloadURL.lastIndexOf("/") + 1);
if(name.indexOf("?") > 0) {
name = name.substr(0, name.indexOf("?"));
}
name = this._script._initFileName(name, true);

var file = this._script._basedirFile;
file.append(name);
file.createUnique(Components.interfaces.nsIFile.NORMAL_FILE_TYPE, 0644);
this._filename = file.leafName;

GM_log("Moving dependency file from " + this._tempFile.path + " to " + file.path);

file.remove(true);
this._tempFile.moveTo(file.parent, file.leafName);
this._tempFile = null;
},

get urlToDownload() { return this._downloadURL; },
setDownloadedFile: function(file) {
this._tempFile = file;
if (this.updateScript)
this._initFile();
}
};

function ScriptResource(script) {
this._script = script;

this._downloadURL = null; // Only for scripts not installed
this._tempFile = null; // Only for scripts not installed
this._filename = null;
this._mimetype = null;
this._charset = null;
this.updateScript = false;

this._name = null;
}

ScriptResource.prototype = {
get name() { return this._name; },

get _file() {
var file = this._script._basedirFile;
file.append(this._filename);
return file;
},

get textContent() { return getContents(this._file); },

get dataContent() {
var appSvc = Components.classes["@mozilla.org/appshell/appShellService;1"]
.getService(Components.interfaces.nsIAppShellService);

var window = appSvc.hiddenDOMWindow;
var binaryContents = getBinaryContents(this._file);

var mimetype = this._mimetype;
if (this._charset && this._charset.length > 0) {
mimetype += ";charset=" + this._charset;
}

return "data:" + mimetype + ";base64," +
window.encodeURIComponent(window.btoa(binaryContents));
},

_initFile: ScriptRequire.prototype._initFile,

get urlToDownload() { return this._downloadURL; },
setDownloadedFile: function(tempFile, mimetype, charset) {
this._tempFile = tempFile;
this._mimetype = mimetype;
this._charset = charset;
if (this.updateScript)
this._initFile();
}
};
131 changes: 131 additions & 0 deletions content/script.js
@@ -0,0 +1,131 @@
function Script(config) {
this._config = config;
this._observers = [];

this._downloadURL = null; // Only for scripts not installed
this._tempFile = null; // Only for scripts not installed
this._basedir = null;
this._filename = null;
this._modified = null;
this._dependhash = null;

this._name = null;
this._namespace = null;
this._description = null;
this._enabled = true;
this._includes = [];
this._excludes = [];
this._requires = [];
this._resources = [];
this._unwrap = false;
this._dependFail = false
this.delayInjection = false;
this._rawMeta = null;
}

Script.prototype = {
matchesURL: function(url) {
function test(page) {
return convert2RegExp(page).test(url);
}

return this._includes.some(test) && !this._excludes.some(test);
},

_changed: function(event, data) { this._config._changed(this, event, data); },

get name() { return this._name; },
get namespace() { return this._namespace; },
get description() { return this._description; },
get enabled() { return this._enabled; },
set enabled(enabled) { this._enabled = enabled; this._changed("edit-enabled", enabled); },

get includes() { return this._includes.concat(); },
get excludes() { return this._excludes.concat(); },
addInclude: function(url) { this._includes.push(url); this._changed("edit-include-add", url); },
removeIncludeAt: function(index) { this._includes.splice(index, 1); this._changed("edit-include-remove", index); },
addExclude: function(url) { this._excludes.push(url); this._changed("edit-exclude-add", url); },
removeExcludeAt: function(index) { this._excludes.splice(index, 1); this._changed("edit-exclude-remove", index); },

get requires() { return this._requires.concat(); },
get resources() { return this._resources.concat(); },
get unwrap() { return this._unwrap; },

get _file() {
var file = this._basedirFile;
file.append(this._filename);
return file;
},

get editFile() { return this._file; },

get _basedirFile() {
var file = this._config._scriptDir;
file.append(this._basedir);
file.normalize();
return file;
},

get fileURL() { return GM_getUriFromFile(this._file).spec; },
get textContent() { return getContents(this._file); },

_initFileName: function(name, useExt) {
var ext = "";
name = name.toLowerCase();

var dotIndex = name.lastIndexOf(".");
if (dotIndex > 0 && useExt) {
ext = name.substring(dotIndex + 1);
name = name.substring(0, dotIndex);
}

name = name.replace(/\s+/g, "_").replace(/[^-_A-Z0-9]+/gi, "");
ext = ext.replace(/\s+/g, "_").replace(/[^-_A-Z0-9]+/gi, "");

// If no Latin characters found - use default
if (!name) name = "gm_script";

// 24 is a totally arbitrary max length
if (name.length > 24) name = name.substring(0, 24);

if (ext) name += "." + ext;

return name;
},

_initFile: function(tempFile) {
var file = this._config._scriptDir;
var name = this._initFileName(this._name, false);

file.append(name);
file.createUnique(Components.interfaces.nsIFile.DIRECTORY_TYPE, 0755);
this._basedir = file.leafName;

file.append(name + ".user.js");
file.createUnique(Components.interfaces.nsIFile.NORMAL_FILE_TYPE, 0644);
this._filename = file.leafName;

GM_log("Moving script file from " + tempFile.path + " to " + file.path);

file.remove(true);
tempFile.moveTo(file.parent, file.leafName);
},

get urlToDownload() { return this._downloadURL; },
setDownloadedFile: function(file) { this._tempFile = file; },

get previewURL() {
return Components.classes["@mozilla.org/network/io-service;1"]
.getService(Components.interfaces.nsIIOService)
.newFileURI(this._tempFile).spec;
},

isModified: function() {
this.delayInjection = false;
if (this._modified != this._file.lastModifiedTime) {
this._modified = this._file.lastModifiedTime;
return true;
}
return false;
}
};

0 comments on commit 3991b65

Please sign in to comment.