Reddit Image Search Add-on

Create instant reverse image search links for image posts

As of 2018-04-12. See the latest version.

// ==UserScript==
// @name         Reddit Image Search Add-on
// @namespace    https://greasyfork.org/en/users/76021-dalimcodes
// @version      0.3
// @description  Create instant reverse image search links for image posts
// @author       DaLimCodes
// @match        https://*.reddit.com/*
// @require      https://cdn.rawgit.com/uzairfarooq/arrive/cfabddbd2633a866742e98c88ba5e4b75cb5257b/minified/arrive.min.js
// @grant        none
// ==/UserScript==

const imageSupported = /\.(gif|jpe?g|png)/i;
const debug = false;

$(document).ready(generateLinks);
$(document).arrive('div.sitetable', generateLinks);

if (debug){
    $("body").append(`<div style="position:fixed;top:40px;right:40px"><button id="generatorButton">Generate Links</button></div>`);
    $("#generatorButton").click(generateLinks);
}

function generateLinks(){
    if(debug){
        console.log("Generating links!");
    }
    $(".reverse-search-button").remove();

    var hasRES = document.getElementsByClassName("res-ner-listing").length;
    if(hasRES){
        var things = $("div.thing.link");
        for (i=0; i<things.length; i++){
            var obj = $(things[i]);
            var link = obj.attr("data-url");
            if(link.match("imgur.com") && !link.match("imgur.com/a/") && !link.match(imageSupported)){
                link += ".jpg";
            }
            link = encodeURIComponent(link);
            if (link.match(imageSupported)){
                // Check if it's a three or four letter image extension
                if(link.charAt(link.search(imageSupported)+4).match(/[a-zA-Z]/)){
                    link = link.substr(0,link.search(imageSupported)+5);
                } else {
                    link = link.substr(0,link.search(imageSupported)+4);
                }
                obj.find("ul.buttons").append(`<li class="reverse-search-button saucenao"><a style="color:green;text-decoration: underline;" href="https://saucenao.com/search.php?url=${link}" target="_blank">SauceNAO</a></li>`);
                obj.find("ul.buttons").append(`<li class="reverse-search-button google"><a style="color:green;text-decoration: underline;" href="https://images.google.com/searchbyimage?image_url=${link}" target="_blank">Google</a></li>`);
                if (debug){
                    console.log("Image link:", link);
                }
            } else {
                if (debug){
                    console.log("Non-image link:", link);
                }
            }
        }
    } else {
        // first pass to find links inside expando
        entry = $("a.title");
        for (i=0; i<entry.length; i++){
            var obj = $(entry[i]);
            var link = obj.attr("href");
            if(link.match("imgur.com") && !link.match("imgur.com/a/") && !link.match(imageSupported)){
                link += ".jpg";
            }
            link = encodeURIComponent(link);
            if (link.match(imageSupported)){
                // Check if it's a three or four letter image extension
                if(link.charAt(link.search(imageSupported)+4).match(/[a-zA-Z]/)){
                    link = link.substr(0,link.search(imageSupported)+5);
                } else {
                    link = link.substr(0,link.search(imageSupported)+4);
                }
                obj.closest("div.entry").find("ul.buttons").append(`<li class="reverse-search-button saucenao"><a style="color:green;text-decoration: underline;" href="https://saucenao.com/search.php?url=${link}" target="_blank">SauceNAO</a></li>`);
                obj.closest("div.entry").find("ul.buttons").append(`<li class="reverse-search-button google"><a style="color:green;text-decoration: underline;" href="https://images.google.com/searchbyimage?image_url=${link}" target="_blank">Google</a></li>`);
                if (debug){
                    console.log("Image link:", link);
                }
            } else {
                if (debug){
                    console.log("Non-image link:", link);
                }
            }

        }

        // second pass to find links in the title
        var entry = $(".expando");
        for (i=0; i<entry.length; i++){
            var obj = $(entry[i]);
            if(obj.closest("div.entry").find("li.reverse-search-button").length == 0){
                if(obj.attr("data-cachedhtml") !== undefined){
                    var cachedhtml = $($.parseHTML(obj.attr("data-cachedhtml").trim()));
                    var link = cachedhtml.find("a.may-blank").attr("href");
                    if (link !== undefined){
                        if(link.match("imgur.com") && !link.match("imgur.com/a/") && !link.match(imageSupported)){
                            link += ".jpg";
                        }
                        link = encodeURIComponent(link);
                        obj.closest("div.entry").find("ul.buttons").append(`<li class="reverse-search-button saucenao"><a style="color:green;text-decoration: underline;" href="https://saucenao.com/search.php?url=${link}" target="_blank">SauceNAO</a></li>`);
                        obj.closest("div.entry").find("ul.buttons").append(`<li class="reverse-search-button google"><a style="color:green;text-decoration: underline;" href="https://images.google.com/searchbyimage?image_url=${link}" target="_blank">Google</a></li>`);
                    }
                }
            }
        }

    }
}