Google Reddit Button

Add a Reddit button to Google search to restrict your searches to results from reddit

Du musst eine Erweiterung wie Tampermonkey, Greasemonkey oder Violentmonkey installieren, um dieses Skript zu installieren.

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

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

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

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

Sie müssten eine Skript Manager Erweiterung installieren damit sie dieses Skript installieren können

(Ich habe schon ein Skript Manager, Lass mich es installieren!)

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.

You will need to install a user style manager extension to install this style.

You will need to install a user style manager extension to install this style.

You will need to install a user style manager extension to install this style.

(I already have a user style manager, let me install it!)

// ==UserScript==
// @name         Google Reddit Button
// @namespace    http://tampermonkey.net/
// @version      1.1
// @description  Add a Reddit button to Google search to restrict your searches to results from reddit
// @match        https://www.google.com/search*
// @grant        none
// @license MIT
// ==/UserScript==

(function() {
    'use strict';

    function waitForElement(selector, callback) {
        const el = document.querySelector(selector);
        if (el) {
            callback(el);
        } else {
            const observer = new MutationObserver(() => {
                const el = document.querySelector(selector);
                if (el) {
                    observer.disconnect();
                    callback(el);
                }
            });
            observer.observe(document.body, { childList: true, subtree: true });
        }
    }

    waitForElement('#hdtb-tls', (toolsBtn) => {
        const toolbar = toolsBtn.parentNode;

        // Create the new button
        const redditBtn = document.createElement('a');
        redditBtn.textContent = "Reddit";
        redditBtn.style.cursor = "pointer";
        redditBtn.style.marginLeft = "12px";
        redditBtn.style.fontWeight = "500";
        redditBtn.style.color = "#80868b";

        redditBtn.onclick = () => {
            const params = new URLSearchParams(window.location.search);
            const q = params.get("q") || "";
            if (!q.includes("site:www.reddit.com")) {
                params.set("q", q + " site:www.reddit.com");
            }
            window.location.search = params.toString();
        };

        toolbar.appendChild(redditBtn);
        console.log("Reddit button added");
    });
})();