-
-
Save esquifit/101722 to your computer and use it in GitHub Desktop.
Issue Greasemonkey userscript commands from the Ubiquity command line
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
var noun_type_gmCommand = { | |
_name: "GM command name", | |
// Returns all GM commands available for the current page. | |
getCommands: function(){ | |
var commands= {}; | |
var menuitems = jQuery(context.chromeWindow.document) | |
.find('\x23userscript-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; | |
} | |
} | |
if (CmdUtils.parserVersion == 2) { | |
CmdUtils.CreateCommand({ | |
names: ["gm"], | |
author: {name: "esquifit"}, | |
arguments: [ {label: "GM command", | |
nountype: noun_type_gmCommand, | |
role: "object"} | |
], | |
description: "Issue Greasemonkey userscript commands from the Ubiquity command line", | |
icon: "chrome://greasemonkey/content/icon_small.png", | |
execute: function( {object} ) { | |
object.data._commandFunc(); | |
}, | |
preview: function( pblock, {object} ) { | |
var text = object.text; | |
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(); | |
}); | |
} | |
}); | |
} else { | |
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} ) { | |
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