Google Reddit Button

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

θα χρειαστεί να εγκαταστήσετε μια επέκταση όπως το Tampermonkey, το Greasemonkey ή το Violentmonkey για να εγκαταστήσετε αυτόν τον κώδικα.

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

θα χρειαστεί να εγκαταστήσετε μια επέκταση όπως το Tampermonkey ή το Violentmonkey για να εγκαταστήσετε αυτόν τον κώδικα.

θα χρειαστεί να εγκαταστήσετε μια επέκταση όπως το Tampermonkey ή το Userscripts για να εγκαταστήσετε αυτόν τον κώδικα.

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

Θα χρειαστεί να εγκαταστήσετε μια επέκταση διαχείρισης κώδικα χρήστη για να εγκαταστήσετε αυτόν τον κώδικα.

(Έχω ήδη έναν διαχειριστή κώδικα χρήστη, επιτρέψτε μου να τον εγκαταστήσω!)

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.

(Έχω ήδη έναν διαχειριστή στυλ χρήστη, επιτρέψτε μου να τον εγκαταστήσω!)

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