Built on Scriptaculous Autocompleter.local object, this class allows you to inject Google Suggest suggestions into any text field (input with type="text"). In the example, the input has an id of "searchBox" and the auto-populated dropdown div has an id of "searchBoxSuggestions"
GoogleSuggestAutocompleter = Class.create();
Object.extend(Object.extend(GoogleSuggestAutocompleter.prototype, Autocompleter.Local.prototype), {
cache: {},
getUpdatedChoices: function() {
var tok = this.getToken().toLowerCase();
if (this.cache[tok]) return this.sendRPCDone( null, tok, this.cache[tok] );
var script = document.createElement('script');
script.type = 'text/javascript';
script.src = 'http://www.google.com/complete/search?hl=en&client=suggest&js=true&q=' + encodeURIComponent(tok);
document.body.appendChild(script);
},
sendRPCDone: function( meh, tok, list ) {
if (!list) return this.updateChoices( '<ul></ul>' );
if (!this.cache[tok]) this.cache[tok] = list;
var values = list.collect( function(v) {
return '<b>'+v.slice(0, tok.length)+'</b>'+v.slice(tok.length);
});
this.updateChoices( '<ul><li>' + values.join('</li><li>') + '</li><ul>' );
}
});
var frameElement, google = {};
Event.observe(window, 'load', function(event) {
google.ac = new GoogleSuggestAutocompleter( "searchBox", "searchBoxSuggestions", null, { minChars: 1 } );
}, 'false');
The Javascript hook is in the processUserCommand method. This one I created out of frustration with repeating myself - answering questions with the answer "RTFM", then posting a link to respective manuals:
/rtfm air
function viewMessage( body, connection, view ) {
var msg = new JVMutableChatMessage('', connection.localUser());
msg.setBodyAsHTML(body);
view.sendMessage(msg);
view.echoSentMessageToDisplay(msg);
return true;
}
function processUserCommand( command, arguments, connection, view ) {
if( command == "rtfm" ) {
switch( arguments[1] ) {
case "air":
return viewMessage("http://labs.adobe.com/wiki/index.php/AIR:Documentation", connection, view);
case "maven":
return viewMessage("http://maven.apache.org/guides/index.html", connection, view);
case "google":
return viewMessage("Google it! http://www.google.com", connection, view);
default:
return viewMessage("RTFM!", connection, view);
}
}
return false;
}
To open a new browser window, you can simply use window.open(). However, window.open() returns a Window object for content, not for the browser window itself, so you should get the chrome Window first. The simplest way to do that is to use nsIWindowMediator [developer.mozilla.org].
window.open();
var wm = Components.classes["@mozilla.org/appshell/window-mediator;1"]
.getService(Components.interfaces.nsIWindowMediator);
var newWindow = wm.getMostRecentWindow("navigator:browser");
var b = newWindow.getBrowser();
To make a window draggable by clicking on the window's contents, you can use the mousedown and mousemove events. The following code does not care which element is clicked on, simply responding to all mousedown events equally. You could improve this code by checking the event.target element and only setting the startPos if the element matches some criteria.
var startPos=0;
var mouseDown = function(event) {
startPos = [ event.clientX, event.clientY];
}
var mouseMove = function(event) {
if (startPos != 0) {
var newX = event.screenX-startPos[0];
var newY = event.screenY-startPos[1];
window.moveTo(newX,newY);
}
}
var mouseUp = function(event) {
startPos=0;
}
window.addEventListener("mousedown",mouseDown, false);
window.addEventListener("mouseup",mouseUp, false);
window.addEventListener("mousemove",mouseMove, false);