Duckduckgo - Quick bang

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

您需要先安裝使用者腳本管理器擴展,如 TampermonkeyGreasemonkeyViolentmonkey 之後才能安裝該腳本。

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

您需要先安裝使用者腳本管理器擴充功能,如 TampermonkeyViolentmonkey 後才能安裝該腳本。

您需要先安裝使用者腳本管理器擴充功能,如 TampermonkeyUserscripts 後才能安裝該腳本。

你需要先安裝一款使用者腳本管理器擴展,比如 Tampermonkey,才能安裝此腳本

您需要先安裝使用者腳本管理器擴充功能後才能安裝該腳本。

(我已經安裝了使用者腳本管理器,讓我安裝!)

你需要先安裝一款使用者樣式管理器擴展,比如 Stylus,才能安裝此樣式

你需要先安裝一款使用者樣式管理器擴展,比如 Stylus,才能安裝此樣式

你需要先安裝一款使用者樣式管理器擴展,比如 Stylus,才能安裝此樣式

你需要先安裝一款使用者樣式管理器擴展後才能安裝此樣式

你需要先安裝一款使用者樣式管理器擴展後才能安裝此樣式

你需要先安裝一款使用者樣式管理器擴展後才能安裝此樣式

(我已經安裝了使用者樣式管理器,讓我安裝!)

// ==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);
      }
    });
  }
})();