Reddit on Google Search

Adds a button to search Reddit via Google Search

< Feedback on Reddit on Google Search

سؤال / تعليق

§
Posted: 30-10-2023
Edited: 30-10-2023

doesnt work for me, but i fixed, and better icon...

my present to you:

// ==/UserScript==

// Change this to false if you don't want an icon
const useIcon = true;
// Change this to true if you want to add the button to the right of the 'Tools' button
const appendRight = false;

const queryRegex = /q=[^&]+/g;
const siteRegex = /\+site(?:%3A|\:).+\.[^&+]+/g;
const redditUrl = '+site%3Areddit.com';
let redditIcon = '<svg foscusable="false" xmlns="http://www.w3.org/2000/svg" width="16" height="16" viewBox="0 0 16 16"><path d="M15.9997 8.10962C15.9639 7.12349 15.1517 6.35345 14.1813 6.38693C13.7513 6.40214 13.3423 6.58172 13.0317 6.88304C11.6642 5.93342 10.0578 5.41296 8.40655 5.37644L9.18587 1.55667L11.7567 2.10757C11.8284 2.78021 12.4196 3.2672 13.0795 3.19415C13.7394 3.1211 14.2171 2.51846 14.1455 1.84582C14.0738 1.17317 13.4826 0.686191 12.8227 0.759238C12.4435 0.798805 12.1031 1.02403 11.915 1.35883L8.97089 0.759238C8.77083 0.713584 8.57078 0.841416 8.52599 1.04838C8.52599 1.05143 8.52599 1.05143 8.52599 1.05447L7.63917 5.30339C5.96706 5.33078 4.33974 5.85429 2.95428 6.80999C2.2496 6.1343 1.13884 6.16778 0.475968 6.88913C-0.186905 7.60742 -0.154059 8.73966 0.553602 9.41535C0.690954 9.54622 0.849207 9.65884 1.02538 9.73797C1.01343 9.91755 1.01343 10.0971 1.02538 10.2767C1.02538 13.019 4.16058 15.25 8.02734 15.25C11.8941 15.25 15.0293 13.0221 15.0293 10.2767C15.0412 10.0971 15.0412 9.91755 15.0293 9.73797C15.6325 9.43056 16.0117 8.79749 15.9997 8.10962ZM3.9874 9.33621C3.9874 8.66052 4.52785 8.10962 5.19072 8.10962C5.8536 8.10962 6.39405 8.66052 6.39405 9.33621C6.39405 10.0119 5.8536 10.5628 5.19072 10.5628C4.52487 10.5567 3.9874 10.0119 3.9874 9.33621ZM10.9655 12.7025C10.1115 13.3569 9.06644 13.6917 7.99748 13.646C6.92852 13.6917 5.88346 13.3569 5.02948 12.7025C4.91602 12.5625 4.93692 12.3525 5.07427 12.2368C5.19371 12.1364 5.36391 12.1364 5.48633 12.2368C6.20892 12.7755 7.08976 13.0494 7.98554 13.0068C8.88131 13.0555 9.76514 12.7938 10.4967 12.2611C10.6281 12.1303 10.8431 12.1333 10.9714 12.2672C11.0998 12.4012 11.0969 12.6203 10.9655 12.7512V12.7025ZM10.7505 10.6054C10.0876 10.6054 9.54717 10.0545 9.54717 9.37882C9.54717 8.70313 10.0876 8.15224 10.7505 8.15224C11.4134 8.15224 11.9538 8.70313 11.9538 9.37882C11.9807 10.0545 11.4641 10.6237 10.8013 10.6511C10.7804 10.6511 10.7624 10.6511 10.7415 10.6511L10.7505 10.6054Z"/></svg>';
const isImageSearch = /[?&]tbm=isch/.test(location.search);

if (typeof trustedTypes !== 'undefined') {
    const policy = trustedTypes.createPolicy('html', { createHTML: input => input });
    redditIcon = policy.createHTML(redditIcon);
}

(function () {
    // Creating the element
    let el = document.createElement('div');
    el.className = 'hdtb-mitem';
    const link = document.createElement('a');

    // Adding the svg icon
    if (useIcon) {
        const span = document.createElement('span');
        span.className = isImageSearch ? 'm3kSL' : 'bmaJhd iJddsb';
        span.style.cssText = 'height:16px;width:16px';
        span.innerHTML = redditIcon;
        link.appendChild(span);
    }

    // Hyperlink to add 'site:reddit.com' to the query
    link.appendChild(document.createTextNode('Reddit'));
    link.href = window.location.href.replace(queryRegex, (match) => {
        // Replaces the existing `site` flags
        return match.search(siteRegex) >= 0 ? match.replace(siteRegex, redditUrl) : match + redditUrl;
    });
    if (isImageSearch) {
        link.classList.add('NZmxZe');
        el = link;
    } else {
        el.appendChild(link);
    }

    // Inserting the element into Google search
    if (appendRight) {
        const toolsBtn = document.querySelector(isImageSearch ? '.ssfWCe' : '.t2vtad');
        toolsBtn.parentNode.insertBefore(el, toolsBtn.nextSibling);
    } else {
        const menuBar = document.querySelector(isImageSearch ? '.T47uwc' : '.MUFPAc');
        if (isImageSearch) {
            menuBar.insertBefore(el, menuBar.children[menuBar.childElementCount - 1]);
        } else {
            menuBar.appendChild(el);
        }
    }
})();

Post reply

تسجيل الدخول إلى مرحلة ما بعد الرد.