-
-
Save rakinishraq/7f7d77d85aa57d2391c31d98bec6ab81 to your computer and use it in GitHub Desktop.
Adds !s to StartPage, like DDG
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// ==UserScript== | |
// @name StartPage Bangs | |
// @namespace http://tarkus.co/ | |
// @version 0.7 | |
// @description Adds !s to StartPage, like DDG | |
// @match https://www.startpage.com/* | |
// @icon https://icons.duckduckgo.com/ip3/www.startpage.com.ico | |
// @run-at document-start | |
// ==/UserScript== | |
/* | |
Example Uses: | |
!i Search Query Here = Startpage Images search | |
Search !g Query Here = Google search | |
Search Query Here! = Open first search result | |
*/ | |
var bangs = { | |
"!g": "https://www.google.com/search?q=", | |
"!gi": "https://encrypted.google.com/search?tbm=isch&q=", | |
"!i": "https://www.startpage.com/sp/search?cat=images&query=", | |
"!gis": "https://www.google.com/searchbyimage?image_url=", | |
"!so": "https://stackoverflow.com/search?q=", | |
"!ddg": "https://duckduckgo.com/?q=", | |
"!a": "https://www.amazon.com/s?k=", | |
"!e": "https://www.ebay.com/sch/i.html?_nkw=", | |
"!yt": "https://www.youtube.com/results?search_query=" | |
}; | |
function get_dict() { | |
var out = {}; | |
var params = document.location.search.substr(1).split("&"); | |
var param = []; | |
for(var i = 0; i < params.length; i++) { | |
param = params[i].split("="); | |
out[param[0]] = param[1]; | |
} | |
return out; | |
} | |
async function bang(query) { | |
var bangRegex = /!\w*/; | |
var bangMatch = query.match(bangRegex); | |
if(bangMatch) { | |
var bangKey = bangMatch[0]; | |
var searchTerm = query.replace(bangKey, '').trim(); | |
if(bangKey === '!') { | |
// Fetch the Startpage search results page | |
let response = await fetch(`https://www.startpage.com/sp/search?query=${searchTerm}`); | |
let text = await response.text(); | |
let parser = new DOMParser(); | |
let htmlDocument = parser.parseFromString(text, "text/html"); | |
let firstResultLink = htmlDocument.querySelector('.w-gl__result-title').href; | |
// Open the first search result's link | |
window.location.replace(firstResultLink); | |
} else if(bangs[bangKey] !== undefined) { | |
window.location.replace(bangs[bangKey] + searchTerm); | |
} | |
} | |
} | |
var query = get_dict()["query"]; | |
if(query === undefined) { | |
window.onload = function () { | |
bang(document.getElementById("query_top").value); | |
}; | |
} else { | |
bang(decodeURIComponent(query.replace(/\%21/g, "!").replace(/\+/g, " "))); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment