Jump to content

User:Rorschach/SearchHelper.js

From Wikipedia, the free encyclopedia
Note: After saving, you have to bypass your browser's cache to see the changes. Google Chrome, Firefox, Microsoft Edge and Safari: Hold down the ⇧ Shift key and click the Reload toolbar button. For details and instructions about other browsers, see Wikipedia:Bypass your cache.
/*
The script adds a few options beside article
Main script by Writ and may be seen at https://en.wikipedia.org/wiki/User:Writ_Keeper/Scripts/googleTitle.js
Script improved by User:SD0001, see diff https://en.wikipedia.org/wiki/Special:ComparePages?page1=User%3ATitodutta%2Fscripts%2FSearchHelper.js&rev1=&page2=User%3ASD0001%2FSearchHelper.js&rev2=&action=&diffonly=&unhide=&diffmode=source

Attribution: Code rewritten by Mr. Stradivarius on 8 December 2021 to fix cross-site scripting vulnerability in the Wikidata link, see details here https://en.wikipedia.org/enwiki/w/index.php?title=User%3ATitodutta%2Fscripts%2FSearchHelper.js&type=revision&diff=1059282493&oldid=1053495203
*/

$(document).ready(function() {
    function getSubject() {
        let pageName = mw.config.get("wgTitle");
        if (pageName.charAt(pageName.length - 1) == ")") {
            return pageName.substring(0, pageName.lastIndexOf("(") - 1);
        } else {
            return pageName;
        }
    }
    
    function escapeSubject(subject) {
        return encodeURIComponent(subject.replace(/_/g, " "))
    }
    
    function createLink(url, display) {
        return $("<a>")
            .attr("href", url)
            .text(display);
    }

    function createMenu(links) {
        let $menu = $("<ul>")
            .attr("id", "popup-menu")
            .css({
                "display": "none",
                "position": "absolute",
                "background-color": "#f9f9f9",
                "border": "1px solid #ddd",
                "box-shadow": "0px 8px 16px 0px rgba(0,0,0,0.2)",
                "z-index": "1",
                "list-style": "none",
                "padding": "0",
                "margin": "0"
            })
            .addClass("popup-menu");

        $.each(links, function(index, $link) {
            $("<li>")
                .css({
                    "padding": "8px",
                    "border-bottom": "1px solid #ddd",
                    "margin": "0"
                })
                .append($link)
                .appendTo($menu);
        });

        $("<li>")
            .css({
                "padding": "8px",
                "border-bottom": "1px solid #ddd",
                "margin": "0",
                "cursor": "pointer"
            })
            .text("Close")
            .click(function() {
                $("#popup-menu").hide();
            })
            .appendTo($menu);

        return $menu;
    }

    function addPopupMenu($node, links, preLink) {
        let $button = $("<button>")
            .text("Show Links")
            .addClass("mw-ui-button")
            .click(function(event) {
                event.stopPropagation();
                $("#popup-menu").toggle();
                let offset = $(this).offset();
                $("#popup-menu").css({
                    "top": offset.top + $(this).outerHeight(),
                    "left": offset.left
                });
            });

        let $menu = createMenu(links);
        $("body").append($button).append($menu);

        $(document).click(function(event) {
            if (!$(event.target).closest('#popup-menu, button').length) {
                $("#popup-menu").hide();
            }
        });
    }

    $firstHeading = $("#firstHeading");
    
    if (mw.config.get("wgCanonicalNamespace") == "" || mw.config.get("wgCanonicalNamespace") == "Draft") {
        let subject = getSubject();
        let links = [
            /*WRS from User:Sam Sailor/Scripts/WRStitle.js*/
            createLink(
                "https://www.google.com/custom?hl=en&cx=007734830908295939403%3Agalkqgoksq0&cof=FORID%3A13%3BAH%3Aleft%3BCX%3AWikipedia%2520Reference%2520Search&q=" + escapeSubject(subject),
                "WRS"
            ),
            createLink(
                "http://www.google.com/search?q=" + escapeSubject(subject) + "+-wikipedia.org",
                "Google search"
            ),
            createLink(
                "https://news.google.com/search?q=" + escapeSubject(subject) + "+-wikipedia.org",
                "Google News"
            ),
            createLink(
                "https://www.google.com/search?tbm=bks&q=" + escapeSubject(subject) + "+-wikipedia.org",
                "Google Books"
            ),
            createLink(
                "https://www.google.com/search?tbm=isch&q=" + escapeSubject(subject),
                "Google images"
            ),
            createLink(
                "https://search.yahoo.com/search?p=" + escapeSubject(subject),
                "Yahoo"
            ),
            createLink(
                "https://duckduckgo.com/?q=" + escapeSubject(subject),
                "DuckDuckGo"
            ),
            createLink(
                "https://www.youtube.com/results?search_query=" + escapeSubject(subject),
                "YouTube"
            ),
            createLink(
                "https://commons.wikimedia.org/enwiki/w/index.php?search=" + escapeSubject(subject),
                "Commons"
            ),
            createLink(
                "http://www.flickr.com/search/?l=commderiv&q=" + escapeSubject(subject),
                "Flickr (CC)"
            ),
            createLink(
                "https://www.imdb.com/find?q=" + escapeSubject(subject),
                "IMDb"
            ),
            createLink(
                "https://www.jstor.org/action/doBasicSearch?Query=" + escapeSubject(subject),
                "JSTOR"
            ),
            createLink(
                "http://edwardbetts.com/find_link/" + escapeSubject(subject),
                "Find link"
            ),
            createLink(
                "https://www.google.com/custom?hl=en&cx=014196815420355252912:fownxmjrhle&cof=FORID%3A13%3BAH%3Aleft%3BCX%3AWikipedia%2520Reference%2520Search&q=" + escapeSubject(subject),
                "Indian newspapers"
            )
        ];
        let preLink = createLink(
            "http://example.com", // Replace with your desired URL
            "Pre-link"
        );
        addPopupMenu($firstHeading, links, preLink);
    }

    if (mw.config.get("wgCanonicalNamespace") == "" && $('#t-wikibase').length) {
        let link = createLink(
            $('li#t-wikibase a:first').attr('href'),
            "Wikidata"
        );
        addPopupMenu($firstHeading, [link]);
    }
});