Google Reddit Button

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

K instalaci tototo skriptu si budete muset nainstalovat rozšíření jako Tampermonkey, Greasemonkey nebo Violentmonkey.

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

K instalaci tohoto skriptu si budete muset nainstalovat rozšíření jako Tampermonkey nebo Violentmonkey.

K instalaci tohoto skriptu si budete muset nainstalovat rozšíření jako Tampermonkey nebo Userscripts.

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

K instalaci tohoto skriptu si budete muset nainstalovat manažer uživatelských skriptů.

(Už mám manažer uživatelských skriptů, nechte mě ho nainstalovat!)

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.

(Už mám manažer uživatelských stylů, nechte mě ho nainstalovat!)

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