Skip to content

Instantly share code, notes, and snippets.

@esquifit
Created April 20, 2009 12:37
Show Gist options
  • Save esquifit/98514 to your computer and use it in GitHub Desktop.
Save esquifit/98514 to your computer and use it in GitHub Desktop.
Issue Greasemonkey userscript commands from the Ubiquity [0.1.9] command line
var noun_type_gmCommand = {
_name: "GM command name",
// Returns all GM commands available for the current page.
getCommands: function(){
var commands= {};
var menuitems = jQuery(window.document)
.find('#userscript-commands-sb > menupopup > menuitem')
.get();
for (var i=0; i<menuitems.length; i++){
var cmd = menuitems[i];
commands[cmd.getAttribute('label')] = cmd;
}
return commands;
},
suggest: function( text, html ) {
var suggestions = [];
var commands = this.getCommands();
for ( var cmd in commands ) {
if ( cmd.match(text, "i") ){
suggestions.push( CmdUtils.makeSugg(cmd, cmd, commands[cmd]) );
}
}
// Return the list of input objects
return suggestions;
}
}
CmdUtils.CreateCommand({
name: "gm",
author: "esquifit",
takes: {"GM command": noun_type_gmCommand },
description: "Issue Greasemonkey userscript commands from the Ubiquity command line",
icon: "chrome://greasemonkey/content/icon_small.png",
execute: function( {data} ) {
data._commandFunc();
},
preview: function( pblock, {text} ) {
pblock.innerHTML = 'Testing...';
var commands = noun_type_gmCommand.getCommands();
// Combine e4x with trimpath templates.
// In trimpath templates, "{...}" is replaced with "[%...%]"
// to avoid conflict with e4x variables
var list = <div>
{text}
<br/>
<hr/>
<h4><i>Commands available in this page:</i></h4>
<hr/>
[%if commands != null %]
<ul>
[%for c in commands %]
<li>
<a name="$[%c_index%]">$[%c_index%]</a>
</li>
[%/for%]
</ul>
[%/if%]
</div>.toXMLString()
.replace(/\[%(.*?)%\]/g,"{$1}");
pblock.innerHTML = CmdUtils.renderTemplate(list, {commands: commands} ) ;
// add click listener to links
var ul = jQuery(pblock).find('li>a')
.bind('click', function(ev){
// clicking closes the preview and executes the action
context.chromeWindow.gUbiquity.closeWindow();
commands[jQuery(this).attr('name')]._commandFunc();
});
}
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment