Edits may only be made by snippet owner.

 Tagged as javascript

edit delete favorite
Posted by jimbojw on Oct 09, 2007
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');
 
edit delete favorite
Posted by eric on Oct 23, 2007
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;
}
 
edit delete favorite
Posted by boyan on Oct 31, 2007
function newWindow(windowName, URL, width, height, scrolling) {
	width = width || 400;
	height = height || 360;
	scrolling = scrolling || 0;
	
	var topX = (window.screen.width / 2) - ( width / 2);
	var topY = (window.screen.height / 2) - ( height / 2);
	
	window.open(URL, windowName, 'width=' + width + ',height=' + height +
 ',location=no,resizable=yes,scrollbars=' + scrolling + 
',screenX=' + topX + ',screenY=' + topY);
}
 
edit delete favorite
Posted by eric on Nov 13, 2007
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();
 
edit delete favorite
Posted by eric on Nov 13, 2007
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);
 
Please submit any bugs/features and report abuse. Thanks!
Spacer
Spacer
Spacer
Top Tags
Spacer