Skip to content

Instantly share code, notes, and snippets.

@graememcc
Created June 26, 2012 16: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 graememcc/2996890 to your computer and use it in GitHub Desktop.
Save graememcc/2996890 to your computer and use it in GitHub Desktop.
Basic find functionality for Fx GCLI
Components.utils.import("resource:///modules/devtools/gcli.jsm");
gcli.addCommand({
name: 'find',
description: 'Find text within the page',
params: [
{
name: 'search',
type: 'string',
description: 'The text to search for',
},
{
name: 'ignorecase',
type: 'boolean',
defaultValue: false
}
],
exec: function(args, context) {
let tabbrowser = context.environment.chromeDocument.defaultView.gBrowser;
let browser = tabbrowser.getBrowserForTab(tabbrowser.selectedTab);
// We need to use a seperate nsITypeaheadFind or we will interfere with
// the browser's findbar. If we didn't do this, you could type find foo,
// open the findbar and search for bar, return to the command line and type
// findnext - the findnext would look for the next occurrence of bar
// instead of foo
if (typeof browser.__cliFastFind == 'undefined') {
browser.__cliFastFind = Components.classes["@mozilla.org/typeaheadfind;1"]
.createInstance(Components.interfaces.nsITypeAheadFind);
browser.__cliFastFind.init(browser.docShell);
}
if (args.ignorecase)
browser.__cliFastFind.caseSensitive = false;
else
browser.__cliFastFind.caseSensitive = true;
let res = browser.__cliFastFind.find(args.search, false);
if (res == Components.interfaces.nsITypeAheadFind.FIND_NOTFOUND)
context.exec("echo Phrase not found");
}
});
gcli.addCommand({
name: 'findnext',
description: 'Find next occurrence of the text previously searched for',
exec: function(args, context) {
let tabbrowser = context.environment.chromeDocument.defaultView.gBrowser;
let browser = tabbrowser.getBrowserForTab(tabbrowser.selectedTab);
if (typeof browser.__cliFastFind == 'undefined')
return;
browser.__cliFastFind.findAgain(false, false);
}
});
gcli.addCommand({
name: 'findprev',
description: 'Find previous occurrence of the text previously searched for',
exec: function(args, context) {
let tabbrowser = context.environment.chromeDocument.defaultView.gBrowser;
let browser = tabbrowser.getBrowserForTab(tabbrowser.selectedTab);
if (typeof browser.__cliFastFind == 'undefined')
return;
browser.__cliFastFind.findAgain(true, false);
}
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment