[Greasemonkey] Google Suggest

affzedeluis kraxxi affzedelius at hotmail.com
Wed Apr 19 21:47:24 EDT 2006


Hello, thanks for helping. I'm planning to learn more about JS and maybe 
read some of the books you links too.


you told me to use this:

>GM_xmlhttpRequest({
>  method:'GET',
>  url:'http://www.google.com/ac.js',
>  onload:function(details) {
>    if (details.status == '200') {
>      //create and append script element here.
>    }
>  }
>}


instead of this:


> > s.src = 'http://www.google.com/ac.js';

So now s.src 0 is replaced with what you said. But "create and append script 
element here.", I don't know what that element is thought that i might be 
the rest of the code so i pasted there.

So now the whole script looks like this:


// Google Suggest Greasemonkey script
// version jgd.0.4
// 2005-04-01
// Released under the GPL license: http://www.gnu.org/copyleft/gpl.html
// See http://www.holovaty.com/blog/archive/2005/03/19/1826 for original.
// Possibly-forked if Adrian doesn't ack new revision.
//
// Changelog:
// 0.1 (2005-03-19) -- Original release.
// 0.2 (2005-03-22) -- Now using ev.handleEvent instead of setTimeout
//                     hack. (Thanks, Follower!)
// 0.3 (2005-04-01) -- Now including on google.* instead of just on
//                     google.com. (Thanks, Etienne!)
// jgd.0.4 (2006-04-16) -- Fixing to work w/ sandboxed scope.
// --------------------------------------------------------------------
//
// This is a Greasemonkey user script.
//
// To install, you need Greasemonkey: http://greasemonkey.mozdev.org/
// Then restart Firefox and revisit this script.
// Under Tools, there will be a new menu item to "Install User Script".
// Accept the default configuration and install.
//
// To uninstall, go to Tools/Manage User Scripts, select this script,
// and click Uninstall.
//
// --------------------------------------------------------------------
//
// ==UserScript==
// @name            Google Suggest
// @namespace       http://dunck.us/code/greasemonkey/
// namespace used to be http://www.holovaty.com/code/firefox/greasemonkey/ 
prior to jgd.0.4
// @description     Adds Google Suggest dropdown to normal Google searches
// @include         http://*.google.*/*
// ==/UserScript==

(function() {
    get_search_form = function() {
        return document.evaluate("//form[@action='/search']", document, 
null, XPathResult.FIRST_ORDERED_NODE_TYPE, null).singleNodeValue;
    }
    // Find the search form. If it doesn't exist on this page, don't bother.
    if (!get_search_form()) return;
    // "Import" Google Suggest JavaScript by dynamically appending it to the
    // current page as a   <script> element.
    var s = document.createElement('script');
    GM_xmlhttpRequest({
method:'GET',
url:'http://www.google.com/ac.js',
onload:function(details) {
   if (details.status == '200') {
     //
    var ev = new Object();
    ev.handleEvent = function (e) {
        var f = get_search_form();
        unsafeWindow.location.href="javascript:"+"(function(){InstallAC(f, 
f.q, f.btnG, 'search', 'en');})()"
    }
    s.addEventListener('load', ev, true);

    document.getElementsByTagName('head')[0].appendChild(s);
})();

   }
}
}




Thanks again for helping.// Google Suggest Greasemonkey script
// version jgd.0.4
// 2005-04-01
// Released under the GPL license: http://www.gnu.org/copyleft/gpl.html
// See http://www.holovaty.com/blog/archive/2005/03/19/1826 for original.
// Possibly-forked if Adrian doesn't ack new revision.
//
// Changelog:
// 0.1 (2005-03-19) -- Original release.
// 0.2 (2005-03-22) -- Now using ev.handleEvent instead of setTimeout
//                     hack. (Thanks, Follower!)
// 0.3 (2005-04-01) -- Now including on google.* instead of just on
//                     google.com. (Thanks, Etienne!)
// jgd.0.4 (2006-04-16) -- Fixing to work w/ sandboxed scope.
// --------------------------------------------------------------------
//
// This is a Greasemonkey user script.
//
// To install, you need Greasemonkey: http://greasemonkey.mozdev.org/
// Then restart Firefox and revisit this script.
// Under Tools, there will be a new menu item to "Install User Script".
// Accept the default configuration and install.
//
// To uninstall, go to Tools/Manage User Scripts, select this script,
// and click Uninstall.
//
// --------------------------------------------------------------------
//
// ==UserScript==
// @name            Google Suggest
// @namespace       http://dunck.us/code/greasemonkey/
// namespace used to be http://www.holovaty.com/code/firefox/greasemonkey/ 
prior to jgd.0.4
// @description     Adds Google Suggest dropdown to normal Google searches
// @include         http://*.google.*/*
// ==/UserScript==

(function() {
    get_search_form = function() {
        return document.evaluate("//form[@action='/search']", document, 
null, XPathResult.FIRST_ORDERED_NODE_TYPE, null).singleNodeValue;
    }
    // Find the search form. If it doesn't exist on this page, don't bother.
    if (!get_search_form()) return;
    // "Import" Google Suggest JavaScript by dynamically appending it to the
    // current page as a <script> element.
    var s = document.createElement('script');
    GM_xmlhttpRequest({
method:'GET',
url:'http://www.google.com/ac.js',
onload:function(details) {
   if (details.status == '200') {
     //
    var ev = new Object();
    ev.handleEvent = function (e) {
        var f = get_search_form();
        unsafeWindow.location.href="javascript:"+"(function(){InstallAC(f, 
f.q, f.btnG, 'search', 'en');})()"
    }
    s.addEventListener('load', ev, true);

    document.getElementsByTagName('head')[0].appendChild(s);
})();

   }
}
}


>From: "Jeremy Dunck" <jdunck at gmail.com>
>Reply-To: greasemonkey at mozdev.org
>To: greasemonkey at mozdev.org
>Subject: Re: [Greasemonkey] Problems with greasemonkey suggest
>Date: Mon, 17 Apr 2006 07:59:35 -0500
>
>On 4/17/06, affzedeluis kraxxi <affzedelius at hotmail.com> wrote:
> > Thanks for answering. I'm pretty new to this so I don't understand all 
>what
> > you told me. What is GM_xhr? I have opened the script file and founded 
>this:
>
>GM_xmlhttpRequest allows you to do cross-domain HTTP requests, which
>is what you're trying to do by requesting from google.com when
>visiting g4g.org.  It's an API that Greasemonkey gives to user scripts
>which isn't available to normal page content.  Cross-domain scripting
>is a security issue-- Greasemonkey user scripts have more trust and
>power than page content scripts because of these APIs.
>
>details:
>http://diveintogreasemonkey.org/api/gm_xmlhttprequest.html
>
> > s.src = 'http://www.google.com/ac.js';
> >
> > Am I suppose to change this URL to something else?
>
>Not exactly.  You'd call GM_xhr and create a script element when you
>get the results of that call, placing the response into the script
>element rather than referencing the other domain.
>
>GM_xmlhttpRequest({
>  method:'GET',
>  url:'http://www.google.com/ac.js',
>  onload:function(details) {
>    if (details.status == '200') {
>      //create and append script element here.
>    }
>  }
>}
>
> > You said something about "place the contents of ac.js
> > within the script tag".
>
>Yeah, so instead of creating this:
><script src="http://foo.com/x.js"></script>
>where x.js is "alert('hi')"
>you'd create this:
><script>alert('hi')</script>
>
>Or, scriptily, given:
>     var s = document.createElement('script');
>instead of this:
>     s.src = 'http://www.google.com/ac.js';
>you'd call GM_xhr and do this with the response details:
>     s.appendChild(document.createTextNode(details.responseText));
>
>
> > Excuse my bad  knowledge. I would really appreciate if someone can help 
>me
> > to understand this.
>
>You should know that user scripts are more trusted than page scripts,
>and because of the power they have, can be a security concern.  You
>shouldn't install scripts you don't understand.  This is the same as
>any other arbitrary program or extension.  You shouldn't install
>without some assurance that it is benign.  I could have written the
>script to capture your google account information if I was a bad
>monkey.
>
>If you just need to get familiar with Greasemonkey, Dive into
>Greasemonkey is dandy:
>http://diveintogreasemonkey.org/
>and if you would like ideas for how to use GM, Greasemonkey Hacks is nice:
>http://www.amazon.com/exec/obidos/ASIN/0596101651/ref%3Dnosim/diveintomark20
>
>And if you need basic JS references, there are tons of them, but these are 
>nice:
>JS: The Definitive Guide:
>   http://www.amazon.com/gp/product/0596000480/
>DHTML Utopia:
>   http://www.amazon.com/gp/product/0957921896/
>_______________________________________________
>Greasemonkey mailing list
>Greasemonkey at mozdev.org
>http://mozdev.org/mailman/listinfo/greasemonkey

_________________________________________________________________
Nyhet! Hotmail direkt i din Mobil! http://mobile.msn.com/



More information about the Greasemonkey mailing list