search on Reddit with Google

adds a button to show results only on Reddit (2024)

You will need to install an extension such as Tampermonkey, Greasemonkey or Violentmonkey 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 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.

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

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

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         search on Reddit with Google
// @namespace    http://tampermonkey.net/
// @version      1.0.1
// @description  adds a button to show results only on Reddit (2024)
// @author       Taha Wael
// @match        https://www.google.com/*
// @icon         https://i.imgur.com/cMJgGWw.jpeg
// @grant        none
// @license      MIT
// ==/UserScript==

(function() {
    'use strict';





    var div = document.createElement("div");
    div.className = "lmao";
    document.querySelector('.dRYYxd').appendChild(div);
    const custom_style={
        flex: '1 0 auto',
        display: 'flex',
        cursor: 'pointer',
        alignItems: 'center',
        border: '0',
        background: 'transparent',
        outline: 'none',
        padding: '0 8px',
        width: '24px',
        lineHeight: '44px',
        // order: '-1'
    }
    let redditB = document.querySelector(".lmao")
    redditB.setAttribute('aria-label', 'Search With Reddit')

    Object.assign(redditB.style,custom_style);

    // Define the SVG code as a string
    const svgCode = `
<svg xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" version="1.1" height: '24px' width: '24px' verticalAlign: 'middle' viewBox="0 0 256 256" xml:space="preserve">

<defs>
</defs>
<g style="stroke: none; stroke-width: 0; stroke-dasharray: none; stroke-linecap: butt; stroke-linejoin: miter; stroke-miterlimit: 10; fill: none; fill-rule: nonzero; opacity: 1;" transform="translate(1.4065934065934016 1.4065934065934016) scale(2.81 2.81)" >
	<path d="M 89.998 45.604 c -0.201 -5.442 -4.77 -9.691 -10.229 -9.506 c -2.419 0.084 -4.719 1.075 -6.466 2.737 c -7.692 -5.24 -16.729 -8.113 -26.017 -8.314 L 51.67 9.442 l 14.461 3.041 c 0.402 3.712 3.728 6.4 7.44 5.996 c 3.712 -0.402 6.4 -3.728 5.996 -7.44 s -3.728 -6.4 -7.44 -5.996 c -2.134 0.218 -4.048 1.461 -5.105 3.309 L 50.461 5.043 c -1.125 -0.252 -2.251 0.453 -2.503 1.596 c 0 0.017 0 0.017 0 0.033 L 42.97 30.119 c -9.406 0.152 -18.559 3.041 -26.352 8.314 c -3.964 -3.728 -10.212 -3.544 -13.94 0.437 c -3.728 3.964 -3.544 10.212 0.437 13.94 c 0.773 0.722 1.662 1.344 2.653 1.781 c -0.068 0.991 -0.068 1.982 0 2.973 c 0 15.133 17.636 27.444 39.386 27.444 s 39.386 -12.295 39.386 -27.444 c 0.068 -0.991 0.068 -1.982 0 -2.973 C 87.932 52.894 90.066 49.4 89.998 45.604 z" style="stroke: none; stroke-width: 1; stroke-dasharray: none; stroke-linecap: butt; stroke-linejoin: miter; stroke-miterlimit: 10; fill: rgb(255,69,0); fill-rule: nonzero; opacity: 1;" transform=" matrix(1 0 0 1 0 0) " stroke-linecap="round" />
	<path d="M 22.429 52.373 c 0 -3.728 3.041 -6.769 6.769 -6.769 s 6.769 3.041 6.769 6.769 s -3.041 6.769 -6.769 6.769 C 25.453 59.108 22.429 56.102 22.429 52.373 z" style="stroke: none; stroke-width: 1; stroke-dasharray: none; stroke-linecap: butt; stroke-linejoin: miter; stroke-miterlimit: 10; fill: rgb(255,255,255); fill-opacity="0";" transform=" matrix(1 0 0 1 0 0) " stroke-linecap="round" />
	<path d="M 61.681 71.218 v -0.269 c -4.803 3.611 -10.682 5.458 -16.696 5.207 c -6.014 0.252 -11.891 -1.596 -16.696 -5.207 c -0.638 -0.773 -0.521 -1.931 0.252 -2.569 c 0.671 -0.554 1.629 -0.554 2.318 0 c 4.065 2.973 9.02 4.485 14.058 4.249 c 5.039 0.269 10.011 -1.176 14.125 -4.114 c 0.739 -0.722 1.948 -0.706 2.671 0.033 C 62.436 69.287 62.419 70.496 61.681 71.218 z" style="stroke: none; stroke-width: 1; stroke-dasharray: none; stroke-linecap: butt; stroke-linejoin: miter; stroke-miterlimit: 10; fill: rgb(255,255,255); fill-rule: nonzero; opacity: 1;" transform=" matrix(1 0 0 1 0 0) " stroke-linecap="round" />
	<path d="M 60.757 59.629 c -0.117 0 -0.218 0 -0.336 0 l 0.051 -0.252 c -3.728 0 -6.769 -3.041 -6.769 -6.769 s 3.041 -6.769 6.769 -6.769 c 3.728 0 6.769 3.041 6.769 6.769 C 67.391 56.337 64.486 59.477 60.757 59.629 z" style="stroke: none; stroke-width: 1; stroke-dasharray: none; stroke-linecap: butt; stroke-linejoin: miter; stroke-miterlimit: 10; fill: rgb(255,255,255); fill-rule: nonzero; opacity: 1;" transform=" matrix(1 0 0 1 0 0) " stroke-linecap="round" />
</g>
</svg>
`;

    // Add the SVG code to the div
    div.innerHTML = svgCode;



    redditB.addEventListener("click", function() {
        // Get the input field element
        const searchInput = document.querySelector('[aria-label="Search"]');

        // Get the current value of the input field and trim any extra whitespace
        let searchTerm = searchInput.value.trim();

        // Check if the search term is at least 1 character long
        if (searchTerm.length >= 1) {
            // If "site:www.reddit.com" exists in the input, remove it
            searchTerm = searchTerm.replace(/site:www\.reddit\.com/g, "").trim();

            // Combine the search term with the site filter
            let fullQuery = searchTerm + " site:www.reddit.com";

            // Create the search URL
            let searchUrl = "https://www.google.com/search?q=" + encodeURIComponent(fullQuery);

            // Redirect the browser to the search URL
            window.location.replace(searchUrl);


        } else {
            // If the input field is empty, redirect to a general Reddit search on Google
            window.location.replace("https://www.google.com/search?q=reddit");
        }

    });
    const searchInput = document.querySelector('[aria-label="Search"]');
    searchInput.addEventListener("click", function() {
        if (searchInput.value.includes("site:www.reddit.com")) {
            searchInput.value = searchInput.value.replace(/site:www\.reddit\.com/g, "").trim();
        }
    });




})();