Skip to content

Commit

Permalink
Apply GM_ prefix to functions missing it.
Browse files Browse the repository at this point in the history
When GM_getContents() can't open a file, log an error about which file it is (and fail silently).
  • Loading branch information
arantius committed May 7, 2010
1 parent 1e2a481 commit e3e09d8
Show file tree
Hide file tree
Showing 9 changed files with 35 additions and 28 deletions.
2 changes: 1 addition & 1 deletion content/addons.js
Expand Up @@ -165,7 +165,7 @@ var greasemonkeyAddons={
var selectedListitem = gExtensionsView.selectedItem;
switch (command) {
case 'cmd_userscript_edit':
openInEditor(script);
GM_openInEditor(script);
break;
case 'cmd_userscript_enable':
selectedListitem.setAttribute('isDisabled', 'false');
Expand Down
2 changes: 1 addition & 1 deletion content/browser.js
Expand Up @@ -524,7 +524,7 @@ function GM_popupClicked(aEvent) {
if (aEvent.button == 0) // left-click: toggle enabled state
script.enabled =! script.enabled;
else // right-click: open in editor
openInEditor(script);
GM_openInEditor(script);

closeMenus(aEvent.target);
}
Expand Down
12 changes: 6 additions & 6 deletions content/config.js
Expand Up @@ -60,7 +60,7 @@ Config.prototype = {
var domParser = Components.classes["@mozilla.org/xmlextras/domparser;1"]
.createInstance(Components.interfaces.nsIDOMParser);

var configContents = getContents(this._configFile);
var configContents = GM_getContents(this._configFile);
var doc = domParser.parseFromString(configContents, "text/xml");
var nodes = doc.evaluate("/UserScriptConfig/Script", doc, null, 0, null);
var fileModified = false;
Expand All @@ -77,7 +77,7 @@ Config.prototype = {
if (!node.getAttribute("modified") || !node.getAttribute("dependhash")) {
script._modified = script._file.lastModifiedTime;
var rawMeta = this.parse(
getContents(script._file), script._downloadURL, true)._rawMeta;
GM_getContents(script._file), script._downloadURL, true)._rawMeta;
script._dependhash = GM_sha1(rawMeta);
fileModified = true;
} else {
Expand Down Expand Up @@ -210,7 +210,7 @@ Config.prototype = {

doc.firstChild.appendChild(doc.createTextNode("\n"));

var configStream = getWriteStream(this._configFile);
var configStream = GM_getWriteStream(this._configFile);
Components.classes["@mozilla.org/xmlextras/xmlserializer;1"]
.createInstance(Components.interfaces.nsIDOMSerializer)
.serializeToStream(doc, configStream, "utf-8");
Expand Down Expand Up @@ -331,7 +331,7 @@ Config.prototype = {
}

// if no meta info, default to reasonable values
if (!script._name && uri) script._name = parseScriptName(uri);
if (!script._name && uri) script._name = GM_parseScriptName(uri);
if (!script._namespace && uri) script._namespace = uri.host;
if (!script._description) script._description = "";
if (script._includes.length == 0) script._includes.push("*");
Expand Down Expand Up @@ -434,7 +434,7 @@ Config.prototype = {
if (!dir.exists()) {
dir.create(Components.interfaces.nsIFile.DIRECTORY_TYPE, 0755);

var configStream = getWriteStream(this._configFile);
var configStream = GM_getWriteStream(this._configFile);
var xml = "<UserScriptConfig/>";
configStream.write(xml, xml.length);
configStream.close();
Expand All @@ -461,7 +461,7 @@ Config.prototype = {

for (var i = 0, script; script = scripts[i]; i++) {
var parsedScript = this.parse(
getContents(script._file), script._downloadURL, true);
GM_getContents(script._file), script._downloadURL, true);
script.updateFromNewScript(parsedScript);
this._changed(script, "modified", null, true);
}
Expand Down
6 changes: 3 additions & 3 deletions content/newscript.js
Expand Up @@ -22,8 +22,8 @@ function doInstall() {
if (!script) return false;

// put this created script into a file -- only way to install it
var tempFile = getTempFile();
var foStream = getWriteStream(tempFile);
var tempFile = GM_getTempFile();
var foStream = GM_getWriteStream(tempFile);
foStream.write(script, script.length);
foStream.close();

Expand All @@ -45,7 +45,7 @@ function doInstall() {
config.install(script);

// and fire up the editor!
openInEditor(script);
GM_openInEditor(script);

// persist namespace value
GM_prefRoot.setValue("newscript_namespace", script.namespace);
Expand Down
2 changes: 1 addition & 1 deletion content/script.js
Expand Up @@ -67,7 +67,7 @@ Script.prototype = {
},

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

_initFileName: function(name, useExt) {
var ext = "";
Expand Down
4 changes: 2 additions & 2 deletions content/scriptdownloader.js
Expand Up @@ -79,7 +79,7 @@ GM_ScriptDownloader.prototype.handleScriptDownloadComplete = function() {
converter.charset = "UTF-8";
source = converter.ConvertFromUnicode(source);

var ws = getWriteStream(file);
var ws = GM_getWriteStream(file);
ws.write(source, source.length);
ws.close();

Expand Down Expand Up @@ -132,7 +132,7 @@ GM_ScriptDownloader.prototype.downloadNextDependency = function(){
var sourceChannel = ioservice.newChannelFromURI(sourceUri);
sourceChannel.notificationCallbacks = new NotificationCallbacks();

var file = getTempFile();
var file = GM_getTempFile();
this.tempFiles_.push(file);

var progressListener = new PersistProgressListener(persist);
Expand Down
2 changes: 1 addition & 1 deletion content/scriptrequire.js
Expand Up @@ -15,7 +15,7 @@ ScriptRequire.prototype = {
},

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

_initFile: function() {
var name = this._downloadURL.substr(this._downloadURL.lastIndexOf("/") + 1);
Expand Down
4 changes: 2 additions & 2 deletions content/scriptresource.js
Expand Up @@ -20,14 +20,14 @@ ScriptResource.prototype = {
return file;
},

get textContent() { return getContents(this._file); },
get textContent() { return GM_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 binaryContents = GM_getBinaryContents(this._file);

var mimetype = this._mimetype;
if (this._charset && this._charset.length > 0) {
Expand Down
29 changes: 18 additions & 11 deletions content/utils.js
Expand Up @@ -77,20 +77,20 @@ function GM_log(message, force) {
// TODO: this stuff was copied wholesale and not refactored at all. Lots of
// the UI and Config rely on it. Needs rethinking.

function openInEditor(script) {
function GM_openInEditor(script) {
var file = script.editFile;
var stringBundle = Components
.classes["@mozilla.org/intl/stringbundle;1"]
.getService(Components.interfaces.nsIStringBundleService)
.createBundle("chrome://greasemonkey/locale/gm-browser.properties");
var editor = getEditor(stringBundle);
var editor = GM_getEditor(stringBundle);
if (!editor) {
// The user did not choose an editor.
return;
}

try {
launchApplicationWithDoc(editor, file);
GM_launchApplicationWithDoc(editor, file);
} catch (e) {
// Something may be wrong with the editor the user selected. Remove so that
// next time they can pick a different one.
Expand All @@ -100,7 +100,7 @@ function openInEditor(script) {
}
}

function getEditor(stringBundle) {
function GM_getEditor(stringBundle) {
var editorPath = GM_prefRoot.getValue("editor");

if (editorPath) {
Expand Down Expand Up @@ -151,7 +151,7 @@ function getEditor(stringBundle) {
}
}

function launchApplicationWithDoc(appFile, docFile) {
function GM_launchApplicationWithDoc(appFile, docFile) {
var args=[docFile.path];

// For the mac, wrap with a call to "open".
Expand All @@ -172,14 +172,14 @@ function launchApplicationWithDoc(appFile, docFile) {
process.run(false, args, args.length);
}

function parseScriptName(sourceUri) {
function GM_parseScriptName(sourceUri) {
var name = sourceUri.spec;
name = name.substring(0, name.indexOf(".user.js"));
name = name.substring(name.lastIndexOf("/") + 1);
return name;
}

function getTempFile() {
function GM_getTempFile() {
var file = Components.classes["@mozilla.org/file/directory_service;1"]
.getService(Components.interfaces.nsIProperties)
.get("TmpD", Components.interfaces.nsILocalFile);
Expand All @@ -193,7 +193,7 @@ function getTempFile() {
return file;
}

function getBinaryContents(file) {
function GM_getBinaryContents(file) {
var ioService = Components.classes["@mozilla.org/network/io-service;1"]
.getService(Components.interfaces.nsIIOService);

Expand All @@ -209,7 +209,7 @@ function getBinaryContents(file) {
return bytes;
}

function getContents(file, charset) {
function GM_getContents(file, charset) {
if( !charset ) {
charset = "UTF-8"
}
Expand All @@ -225,7 +225,13 @@ function getContents(file, charset) {
unicodeConverter.charset = charset;

var channel = ioService.newChannelFromURI(GM_getUriFromFile(file));
var input=channel.open();
try {
var input=channel.open();
} catch (e) {
GM_logError(new Error("Could not open file: " + file.path));
return "";
}

scriptableStream.init(input);
var str=scriptableStream.read(input.available());
scriptableStream.close();
Expand All @@ -238,7 +244,7 @@ function getContents(file, charset) {
}
}

function getWriteStream(file) {
function GM_getWriteStream(file) {
var stream = Components.classes["@mozilla.org/network/file-output-stream;1"]
.createInstance(Components.interfaces.nsIFileOutputStream);

Expand All @@ -253,6 +259,7 @@ function GM_getUriFromFile(file) {
.newFileURI(file);
}

// Todo: replace with nsIVersionComparator?
/**
* Compares two version numbers
*
Expand Down

0 comments on commit e3e09d8

Please sign in to comment.