Duckduckgo - Quick bang

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

Aby zainstalować ten skrypt, wymagana jest instalacje jednego z następujących rozszerzeń: Tampermonkey, Greasemonkey lub Violentmonkey.

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

Aby zainstalować ten skrypt, wymagana jest instalacje jednego z następujących rozszerzeń: Tampermonkey, Violentmonkey.

Aby zainstalować ten skrypt, wymagana będzie instalacja rozszerzenia Tampermonkey lub Userscripts.

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

Aby zainstalować ten skrypt, musisz zainstalować rozszerzenie menedżera skryptów użytkownika.

(Mam już menedżera skryptów użytkownika, pozwól mi to zainstalować!)

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.

Będziesz musiał zainstalować rozszerzenie menedżera stylów użytkownika, aby zainstalować ten styl.

Będziesz musiał zainstalować rozszerzenie menedżera stylów użytkownika, aby zainstalować ten styl.

Musisz zainstalować rozszerzenie menedżera stylów użytkownika, aby zainstalować ten styl.

(Mam już menedżera stylów użytkownika, pozwól mi to zainstalować!)

// ==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      2
// @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);
  } else {
    document.querySelector("input[type=submit]").click();
  }
}

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