Skip to content

Instantly share code, notes, and snippets.

@ADIX7
Created July 18, 2019 13:12
Show Gist options
  • Save ADIX7/18b0fcfb5a83492f472e9386641f372b to your computer and use it in GitHub Desktop.
Save ADIX7/18b0fcfb5a83492f472e9386641f372b to your computer and use it in GitHub Desktop.
DuckDuckGo GreaseMonkey scripts

InstantBang.js

ReSearch.js

SearchOnNewTab.js

// ==UserScript==
// @name Duck Duck Go insta-bang
// @namespace qwerjk
// @description Hitting "g" focuses search and appends an !
// @include https://www.duckduckgo.com/*
// @include https://duckduckgo.com/*
// @include http://duckduckgo.com/*
// ==/UserScript==
(function () {
var searchBox = document.getElementById("search_form_input")
|| document.getElementById("search_form_input_homepage");
document.addEventListener('keydown', function (e) {
switch (e.keyCode) {
case 71:
if (document.activeElement !== searchBox) {
if (!searchBox.value.startsWith("!")) {
searchBox.value = "! " + searchBox.value;
}
else if(searchBox.value[1] != " ")
{
let spacecPos = searchBox.value.indexOf(" ");
searchBox.value = "!" + searchBox.value.substr(spacecPos);
}
searchBox.setSelectionRange(1, 1);
searchBox.focus();
e.stopPropagation();
e.preventDefault();
}
break;
}
}, false);
})();
// ==UserScript==
// @name Duck Duck Go !! re-search
// @description Search last search with !!
// @include https://www.duckduckgo.com/*
// @include https://duckduckgo.com/*
// @include http://duckduckgo.com/*
// @grant GM.setValue
// @grant GM.getValue
// ==/UserScript==
var ddg_lastSearchPropName = "ddg_lastSearch";
var searchForm = document.getElementById("search_form")
|| document.getElementById("search_form_homepage");
var searchBox = document.getElementById("search_form_input")
|| document.getElementById("search_form_input_homepage");
async function saveSearch() {
let val = searchBox.value;
if ((val !== "") && (val !== "!!")) {
if (val.startsWith("!")) {
val = val.substr(val.indexOf(" "));
}
await GM.setValue(ddg_lastSearchPropName, val);
}
}
(async () => {
document.addEventListener('keydown', function (e) {
if (e.keyCode == 13) {
saveSearch();
}
}, false);
let currentVal = searchBox.value;
if (currentVal !== "") {
if (currentVal == "!!") {
searchBox.value = await GM.getValue(ddg_lastSearchPropName, "");
searchForm.submit();
}
else if (currentVal.startsWith("!!") && !currentVal.startsWith("!!!") && currentVal.indexOf(" ") == -1) {
searchBox.value = currentVal.substr(1) + " " + await GM.getValue(ddg_lastSearchPropName, "");
searchForm.submit();
}
else {
saveSearch();
}
}
})();
// ==UserScript==
// @name Duck Duck Go search on new tab
// @description In the search bar pressing CTRL + ENTER will search on a new tab
// @include https://www.duckduckgo.com/*
// @include https://duckduckgo.com/*
// @include http://duckduckgo.com/*
// ==/UserScript==
(function () {
var searchForm = document.getElementById("search_form")
|| document.getElementById("search_form_homepage");
document.addEventListener('keydown', function (e) {
if (e.keyCode == 17) {
searchForm.target = "_blank";
}
}, false);
document.addEventListener('keyup', function (e) {
if (e.keyCode == 17 || e.keyCode == 13) {
searchForm.target = "_self";
}
}, false);
})();
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment