15

I'm making a little script with Autohotkey to quickly go to the first Google result of a search term. My problem is, the only method I've found for doing this though the URL is acting a little inconsistent.

http://www.google.com/search?q=searchterm&btnI=745

This only works when the first hit is deemed to be a very good match. Otherwise Google shows the normal 10 results. However, the actual "I'm Feeling Lucky" button on their front pages always takes you to the first result.

Try these links:

http://www.google.com/search?q=new%20york&btnI=745          <- works
http://www.google.com/search?q=new%20york%20dijon&btnI=745  <- doesn't work

"new york dijon" on the front-page and then hitting "I'm Feeling Lucky" does work though.

Any idea how I can get it to consistently work in URL form?

Edit: Okay, seems this might not be doable in a single URL. I'll mark a greasemonkey-script workaround as correct if posted.

arboreal shark
  • 221
  • 3
  • 11
  • These fail: `google.com/search?q=new%20york%20dijon&btnI=Im+Feeling+Lucky`, `google.com/search?btnI=1&q=new%20york%20dijon`, `google.com/search?btnI=I%27m+Feeling+Lucky&ie=UTF-8&oe=UTF-8&q=new%20york%20dijon` – Ivan Chau Dec 31 '13 at 03:59
  • I guess the safe search functionality hinders the feature. – Ivan Chau Dec 31 '13 at 04:19
  • I'm going to try seeing how the HTML form is set up in Firefox with Noscript enabled. – Just Jake Jan 07 '14 at 11:17
  • [This article](http://cowsthatgomoo.blogspot.nl/2013/05/googles-selfish-move-to-keep-im-feeling.html) might also interest you. According to it, it works with 2 keywords, but not 3. And even then somehow, not always ;( I think `btnI` works with up to 2 keywords **AND if** Google does not decide you might have typed something wrong (like `Did you mean: geeks alive`). Otherwise a bit of javascript is used to redirect you. – Rik Jan 07 '14 at 12:22
  • @JustJake, the lucky button contains a non-standard attribute: `jsaction="sf.lck"`. – Synetech Jan 08 '14 at 01:13
  • The form uses `btnI=submit`, though *any* value is sufficient, so long as `btnI` is not null. @BlueBerry, where did you get `btnI=745`? Also, Rik(’s article) is correct, using `new dijon` works from the URL and you are correct that `new york dijon` works from the homepage. Very odd that they would make such inconsistent behavior. – Synetech Jan 08 '14 at 01:16
  • Just a random reminder: The result for "I'm feeling lucky" does not necessarily match the top result for the same query. – nitro2k01 Jan 14 '14 at 17:55
  • Is there any update on this? I'm trying to create an AutoIt script that goes straight to the first result. Now, I must resort to going to google.com first and press the I'm lucky button. The only condition is that I'd prefer not to use an extension/plugin. I'm using Chrome. – Daan Oct 04 '14 at 19:36

6 Answers6

3

Made a workaround Greasemonkey script:

// ==UserScript==
// @name         Google IFL
// @match        https://*.google.com/*?lucky=*
// @match        http://*.google.com/*?lucky=*
// ==/UserScript==

document.getElementById("gsr").style.display = 'none'; // optional. shows blank screen before forwarding. just looks better imo.
document.getElementById("gbqfq").focus();
var pathname = document.URL;
var start = pathname.indexOf("?lucky=");
var searchterm = pathname.substring(start+7);
document.getElementById("gbqfq").value = decodeURI(searchterm);
var btnLucky = document.getElementsByName('btnI')[0];
btnLucky.click();

This script will always forward you to Google's "I Feel Lucky" choice provided you navigate to www.google.com/?lucky=searchterm_goes_here.

I'm using it in FireFox by having a keyword to a bookmark going to www.google.com/?lucky=%s.

arboreal shark
  • 221
  • 3
  • 11
1

The best solution I've come up with is: Chrome > Preferences > Manage Search Engines... add:

  • Search Engine: I'm Feeling Lucky
  • Keyword: \ (replace with your preferred shortcut)
  • URL: {google:baseURL}search?q=%s&btnI

Then as per this thread, add the following Greasemonkey / Tampermonkey script to reload the page with Google as the referrer.

// ==UserScript==
// @name         I'm feeling lucky fix
// @version      0.0
// @description  Makes Google I'm feeling lucky work reliably from the address bar
// @author       Will Rice
// @match        http://*.google.co.uk/search?q=*&btnI
// @match        https://*.google.co.uk/search?q=*&btnI
// @match        http://*.google.com/search?q=*&btnI
// @match        https://*.google.com/search?q=*&btnI
// ==/UserScript==

document.getElementsByTagName("body")[0].style.display = "none";
window.location.href = location;

Setting the script to "run at body" and adding any additional Google TLDs as you see fit (I couldn't get regex working in Tampermonkey).

Will Rice
  • 11
  • 1
  • This works for simple queries but not more complex ones unfortunately. A script that uses javascript to click the top result would be better. – Kevin Dec 03 '18 at 06:15
1

Some of the otherwise-elegant solutions on this page no longer work, so I'm adding my solution here, which is working for me on tampermonkey chrome in Dec of 2018.

@match vs. @include has changed for tampermonkey (@match can't include query terms) which caused quite a bit of debugging frustration with this in case google changes their URLs.

// ==UserScript==
// @name         I'm feeling lucky fix
// @version      0.1
// @description  Makes Google I'm feeling lucky work reliably from the address bar
// @author       Kevin Watt
// @include      https://www.google.*/*btnI*
// ==/UserScript==
// // @match      https://*/*
if (location.href.indexOf('btnI')) document.querySelector('#search a').click()
Kevin
  • 111
  • 2
1

I've found that ?gfns=1&sourceid=navclient&q=your+query+string works much better than ?btnI=1&q=your+query+string. For me, the latter frequently triggers "Redirect Notice" requiring human interaction. Only thing about the former that isn't ideal is that sometimes I get a query triggers the "Did you mean: __?" which also requires human interaction, but this is less frequent for me given my use case...

...In my case, I'm not very interested in a bookmarklet or integration that has to be done for each browser, so I prefer to just use the former URL syntax and am pretty happy with it. I'm using this from terminal vim to google things under my cursor in various contexts. Is saving me a lot of typing..

timblaktu
  • 111
  • 2
1

When you have Javascript disabled, it seems that Google uses both a cookie and the HTTP Referrer header being set to https://www.google.com to track if you actually came from the Google home page and clicked the "I'm Feeling Lucky" button. I don't think you'll be able to convince Google to hand you the lucky result with just a URL.

Just Jake
  • 738
  • 5
  • 17
0

(All the above posts prior this post creation don't work anymore?)

Solution

I think there are two straightforward ways to implement this:

  1. Skip redirect note (which you get today)
  2. Simulate an user

I made a script for 1, but noticed that "https://google.com/search?btnI=&q=%s" sometimes even doesn't show a redirect note if you use space, e.g. https://google.com/search?btnI=&q=yeah%20super.

But simulating the user works flawlessly as to be expected. I used the code of "arboreal shark" as a basis for beginning making this (so also props to him).

// @name         Google IFL by UserSim
// @match        https://*.google.com/*?lucky=*
// @match        http://*.google.com/*?lucky=*
// @match        https://google.com/*?lucky=*
// @match        http://google.com/*?lucky=*
// @match        https://*.google.de/*?lucky=*
// @match        http://*.google.de/*?lucky=*
// @match        https://google.de/*?lucky=*
// @match        http://google.de/*?lucky=*
// ==/UserScript==


// Idea: Simulate normal user -> Google cannot block that without other effects
// Searches e.g. by "www.google.com/?lucky=searchterm_goes_here" or "www.google.de/?lucky=searchterm_goes_here"

// console.log("Hello, World!"); // Test if script is activated on site: 


// # Show blank screen while working
document.getElementsByTagName("body")[0].style.display = "none";

// # Decode search input
var pathname = document.URL;
var start = pathname.indexOf("?lucky=");
var searchterm = pathname.substring(start+7);
var decodedSearchTerm = decodeURI(searchterm); //otherwise you will have `%20` for space and so on

// # Input this in search field
document.getElementsByName("q")[0].focus();
document.getElementsByName("q")[0].value = decodedSearchTerm;

// # click Feeling Lucky Button
var btnLucky = document.getElementsByName('btnI')[0];
btnLucky.click(); 

Browser Integration

You can use the code e.g. in firefox with greasemonkey (i would recommend greasemonkey and no other user script manager, because others are not open sourced on e.g. github or seem to have privacy problems). Then you can make a new bookmark for http://www.google.de/?lucky=%s and add "i" as a word (for "instant") and now search by i <searchterms> in addressbar. Nice!

Speed

Also to mention: my script for 1. (even though doesn't work for spaced inputs, see above) wasn't significantly faster than my 2. solution. So I stopped looking if another url option doesn't have the problem with space.

But faster is always better for this things. If somebody has a significantly faster version, please post :)

SearchSpace
  • 141
  • 1
  • 10