From 86522cc0d6708b7f1c5fe23abf31a3a0884c7aa8 Mon Sep 17 00:00:00 2001 From: Simon Bruder Date: Sun, 26 May 2019 15:36:15 +0000 Subject: [PATCH] add user script --- startpage.user.js | 38 ++++++++++++++++++++++++++++++++++++++ 1 file changed, 38 insertions(+) create mode 100644 startpage.user.js diff --git a/startpage.user.js b/startpage.user.js new file mode 100644 index 0000000..f35b6d2 --- /dev/null +++ b/startpage.user.js @@ -0,0 +1,38 @@ +// ==UserScript== +// @name Startpage Bangs +// @version 1 +// @grant none +// @run-at document-start +// @include https://*.startpage.com/* +// ==/UserScript== + +fetch('https://s3.sbruder.de/cdn/bangs/bangs.json') + .then(text => text.json()) + .then(bangs => { + const urlParams = new URLSearchParams(window.location.search) + const searchQuery = urlParams.get('query') + + let matchBang = null + let parsedBang = null + + matchBang = searchQuery.match(/(.*) \!(.*)/) + if (matchBang !== null) { + parsedBang = { + 'bang': matchBang[2], + 'query': matchBang[1] + } + } + + matchBang = searchQuery.match(/\!(.*) (.*)/) + if (matchBang !== null) { + parsedBang = { + 'bang': matchBang[1], + 'query': matchBang[2] + } + } + + if (parsedBang !== null) { + redirectURL = bangs[parsedBang.bang].replace('%s', parsedBang.query) + window.location.replace(redirectURL); + } +})