Skip to content

Instantly share code, notes, and snippets.

@esquifit
Created January 15, 2010 22:29
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save esquifit/278474 to your computer and use it in GitHub Desktop.
Save esquifit/278474 to your computer and use it in GitHub Desktop.
Open "file:///" url in editor
/*
* Open file:/// url in editor
*
* This jetpack adds an option 'Open in editor' to the context menu of the location bar
* when a local file is loaded into Firefox. The path to the editor can be maintained
* as a jetpack setting. To set it to your editor of choice proceed as follows:
* 1) in about:jetpack select 'installed features'
* 2) click on 'settings' next to the feature name
* 3) restart Firefox for the setting to take effect
*
* Tested only in WinXP with Firefox 3.5.7
*
* esquifit - 15.01.2010
*/
var manifest = {
settings: [{
name: "editor",
type: "text",
label: "Editor",
default: "Please set the path to your editor here." }]
};
jetpack.future.import("menu");
jetpack.future.import("storage.settings");
jetpack.menu.context.browser.beforeShow = function (menu, context) {
// avoid duplicated entries
menu.reset();
// only in the location bar
if (context.node.parentNode &&
context.node.parentNode.parentNode &&
context.node.parentNode.parentNode.parentNode &&
context.node.parentNode.parentNode.parentNode.id != 'urlbar')
return;
var fileURL = context.node.value.toString();
if (! fileURL.match(/^file:/i) )
return;
menu.add({
label: "Open in editor",
command: function () {
var editor = getEditor();
if(!editor) {
jetpack.notifications.show("Editor preference not set or not executable.");
return;
}
var service = Components.classes['@mozilla.org/network/io-service;1']
.getService(Components.interfaces.nsIIOService);
var fileHandler = service.getProtocolHandler("file")
.QueryInterface(Components.interfaces.nsIFileProtocolHandler);
var file = fileHandler.getFileFromURLSpec( fileURL );
launchApplicationWithDoc(editor,file.path);
}
})
};
// stolen from Greasemonkey source
function launchApplicationWithDoc(appFile, filePath) {
var args=[filePath];
// For the mac, wrap with a call to "open".
var xulRuntime = Components.classes["@mozilla.org/xre/app-info;1"]
.getService(Components.interfaces.nsIXULRuntime);
if ("Darwin"==xulRuntime.OS) {
args=["-a", appFile.path, filePath]
appFile = Components.classes["@mozilla.org/file/local;1"]
.createInstance(Components.interfaces.nsILocalFile);
appFile.followLinks = true;
appFile.initWithPath("/usr/bin/open");
}
var process = Components.classes["@mozilla.org/process/util;1"]
.createInstance(Components.interfaces.nsIProcess);
process.init(appFile);
process.run(false, args, args.length);
}
// stolen from Greasemonkey source
function getEditor() {
var editorPath = jetpack.storage.settings.editor;
var editor = Components.classes["@mozilla.org/file/local;1"]
.createInstance(Components.interfaces.nsILocalFile);
editor.followLinks = true;
try {
editor.initWithPath(editorPath);
} catch (ex) {
console.error(ex.toString());
return null;
}
// make sure the editor preference is still valid
if (editor.exists() && editor.isExecutable()) {
return editor;
} else {
console.error('Editor does not exist or is not executable');
return null;
}
}
// vim:se sw=2:
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment