Google Lucky Bang

Add a ! to the end of your google search to perform a lucky search

您需要先安装一个扩展,例如 篡改猴Greasemonkey暴力猴,之后才能安装此脚本。

You will need to install an extension such as Tampermonkey to install this script.

您需要先安装一个扩展,例如 篡改猴暴力猴,之后才能安装此脚本。

您需要先安装一个扩展,例如 篡改猴Userscripts ,之后才能安装此脚本。

您需要先安装一款用户脚本管理器扩展,例如 Tampermonkey,才能安装此脚本。

您需要先安装用户脚本管理器扩展后才能安装此脚本。

(我已经安装了用户脚本管理器,让我安装!)

您需要先安装一款用户样式管理器扩展,比如 Stylus,才能安装此样式。

您需要先安装一款用户样式管理器扩展,比如 Stylus,才能安装此样式。

您需要先安装一款用户样式管理器扩展,比如 Stylus,才能安装此样式。

您需要先安装一款用户样式管理器扩展后才能安装此样式。

您需要先安装一款用户样式管理器扩展后才能安装此样式。

您需要先安装一款用户样式管理器扩展后才能安装此样式。

(我已经安装了用户样式管理器,让我安装!)

// ==UserScript==
// @name         Google Lucky Bang
// @namespace    https://greasyfork.org/en/users/8615-joeytwiddle
// @version      1.0.3
// @description  Add a ! to the end of your google search to perform a lucky search
// @author       joeytwiddle
// This is supposed to be all google search sites.  Copied from faviconizegoogle.user.js
// But then I also added /url pages, to catch "Redirect Notice"s
// @include      /https?:\/\/((www\.)?|encrypted\.)google\.[a-z]{2,3}(\.[a-z]{2})?\/(search|webhp|\?gws_rd|\?gfe_rd|url)?.*/
// TODO This currently acts on pages which do not support lucky search (e.g. Google Images).  Ideally we would activate only on pages where we can actually help!
// @grant        GM_setValue
// @grant        GM_getValue
// @run-at       document-start
// ==/UserScript==

(function() {
  'use strict';

  const params = {};
  document.location.search.slice(1).split('&').forEach(pair => {
    const [key, value] = pair.split('=');
    params[decodeURIComponent(key)] = decodeURIComponent(value);
  });

  // If we are on a search page, and the query ends with "!", then change to a lucky search (and remove the "!")
  if (document.location.pathname === '/search') {

    if (params.q && params.q.slice(-1) === '!') {
      const newQuery = params.q.slice(0, -1);

      console.log("Doing lucky search for:", newQuery);

      // Store the time
      GM_setValue('lucky_search_initiated', String(Date.now()));

      // Example lucky URL: https://www.google.com/search?sxsrf=ACYBGNS3YpGqRlxp6MfsZ-8oYSGTyllGOA%3A1569379745064&source=hp&ei=odWKXeO0Af_B3LUPoNaDuAQ&q=test%21&btnI=I%27m+Feeling+Lucky&oq=test%21&gs_l=psy-ab.3..0i131j0l9.9511.10195..14336...0.0..0.69.340.6......0....1..gws-wiz.....10..35i362i39j35i39j0i67j0i131i67.HtcsjjxXywM

      //const newUrl = `${document.location.origin}/search?q=${encodeURIComponent(newQuery)}&btnI=I%27m+Feeling+Lucky`;
      const { origin, pathname, search } = document.location;
      const newUrl = `${origin}${pathname}${search.replace(/\bq=[^&]*/, `q=${encodeURIComponent(newQuery)}`)}&btnI=I%27m+Feeling+Lucky`;

      //document.location.replace(newUrl);
      document.location.assign(newUrl);
    }
  }

  // Problem: After the above code, Google was taking us to a "Redirect Notice" page.
  // "The page you were on is trying to send you to ..."
  // Solution: If we are on a "Redirect Notice" page, and within a reasonable time window of a lucky search, then automatically follow the link
  if (document.location.pathname === '/url') {
    const startTime = Number(GM_getValue('lucky_search_initiated'));
    if (typeof startTime === 'number' && startTime >= Date.now() - 15 * 1000 && startTime <= Date.now()) {
      const url = params.q;
      if (url) {
        console.log("Automatically following the redirect to:", url);
        GM_setValue('lucky_search_initiated', '');
        //document.location.replace(url);
        document.location.assign(url);
      }
    }
  }
})();