Skip to content

Instantly share code, notes, and snippets.

@autonome
Created November 9, 2010 15:49
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 autonome/669262 to your computer and use it in GitHub Desktop.
Save autonome/669262 to your computer and use it in GitHub Desktop.
Jetpack module for querying the datasource that powers the Firefox awesomebar.
const { Ci, Cc, Cu } = require("chrome");
Cu.import("resource://gre/modules/Services.jsm", this);
Cu.import("resource://gre/modules/XPCOMUtils.jsm", this);
function AutoCompleteInput(aSearches) {
this.searches = aSearches;
}
AutoCompleteInput.prototype = {
timeout: 10,
textValue: "",
searches: null,
searchParam: "",
popupOpen: false,
minResultsForPopup: 0,
invalidate: function() {},
disableAutoComplete: false,
completeDefaultIndex: false,
get popup() { return this; },
onSearchBegin: function() {},
onSearchComplete: function() {},
setSelectedIndex: function() {},
get searchCount() { return this.searches.length; },
getSearchAt: function(aIndex) this.searches[aIndex],
QueryInterface: XPCOMUtils.generateQI([
Ci.nsIAutoCompleteInput,
Ci.nsIAutoCompletePopup,
])
};
exports.search = function search(options) {
let controller = Cc["@mozilla.org/autocomplete/controller;1"].
getService(Ci.nsIAutoCompleteController);
// Make an AutoCompleteInput that uses our searches
// and confirms results on search complete
let input = new AutoCompleteInput(["history"]);
controller.input = input;
input.searchParam = options.search;
input.onSearchBegin = function() {};
input.onSearchComplete = function onSearchComplete() {
let results = [];
for (let i = 0; i < controller.matchCount; i++) {
let result = {
url: controller.getValueAt(i),
title: controller.getCommentAt(i),
//label: controller.getLabelAt(i),
style: controller.getStyleAt(i),
image: controller.getImageAt(i)
};
if (options.onResult)
options.onResult(result);
results.push(result);
}
if (options.onComplete)
options.onComplete(results);
};
controller.startSearch(options.search);
};
@vitaly-zdanevich
Copy link

Can you please explain what is this and how I can use it?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment