Duckduckgo - Quick bang

Skip one unnecessary page load and inmediately go to the bang search result on Enter

As of 2017-12-26. See the latest version.

You will need to install an extension such as Tampermonkey, Greasemonkey or Violentmonkey to install this script.

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

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

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

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

You will need to install a user script manager extension to install this script.

(I already have a user script manager, let me install it!)

You will need to install an extension such as Stylus to install this style.

You will need to install an extension such as Stylus to install this style.

You will need to install an extension such as Stylus to install this style.

You will need to install a user style manager extension to install this style.

You will need to install a user style manager extension to install this style.

You will need to install a user style manager extension to install this style.

(I already have a user style manager, let me install it!)

// ==UserScript==
// @name         Duckduckgo - Quick bang
// @namespace    https://openuserjs.org/users/cuzi
// @description  Skip one unnecessary page load and inmediately go to the bang search result on Enter  
// @license      MIT
// @copyright    2017, cuzi (https://openuserjs.org/users/cuzi)
// @version      1
// @include      https://duckduckgo.com/*
// @include      https://start.duckduckgo.com/*
// @grant        none
// ==/UserScript==

// ==OpenUserJS==
// @author       cuzi
// ==/OpenUserJS==

"use strict";

const bangUrl = "https://duckduckgo.com/bang.js";
var input;

function load(s) {
  var req = new XMLHttpRequest();
  req.responseType = "json";
  req.open("GET", bangUrl, true);
  req.onload = function () {
    let arr = req.response;
    for (let i = 0; i < arr.length; i++) {
      localStorage.setItem(arr[i]["t"], arr[i]["u"]);
    }
    bang(s);
  };
  req.send(null);
}

function bang(s) {
  try {
    if (!localStorage.getItem("g")) {
      return load(s);
    }
    let m = s.match(/(.*)\!(\w+)(\s*.*)/);
    let t = m[2];
    let q = encodeURIComponent(m[1] + m[3]);
    let u = localStorage.getItem(t);
    document.location.replace(u.replace("{{{s}}}", q));
  }
  catch (e) {
    document.querySelector("input[type=submit]").click();
  }
}

function onsubmit(ev) {
  if (input.value.indexOf("!") !== -1) {
    bang(input.value);
  }
}

(function () {
  input = document.querySelector("input[name=q]");
  if (input) {
    input.addEventListener("keydown", function (ev) {
      if (ev.keyCode === 13) {
        ev.preventDefault();
        return onsubmit(ev);
      }
    });
  }
})();