forked from greasemonkey/greasemonkey
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Move Script, ScriptRequire, ScriptResource objects into their own fil…
…es (out of config.js).
- Loading branch information
Showing
5 changed files
with
230 additions
and
230 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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; | ||
} | ||
}; |
Oops, something went wrong.