cjmling / findings

Notes on stuff i finds worth keeping for quick reference later on.
2 stars 0 forks source link

Github search on google search #142

Open cjmling opened 5 years ago

cjmling commented 5 years ago

script for searching issues on github when doing google search.

This script is meant to be used in tempermonkey chrome plugin.

NOTE: alot of code in this script is unused and need to be removed/refactored. I'm just too lazy.

// ==UserScript==
// @name         New Userscript
// @namespace    http://tampermonkey.net/
// @version      2024-05-07
// @description  try to take over the world!
// @author       You
// @match        https://www.google.com/*
// @require      https://ajax.googleapis.com/ajax/libs/jquery/3.3.1/jquery.min.js
// @icon         data:image/gif;base64,R0lGODlhAQABAAAAACH5BAEKAAEALAAAAAABAAEAAAICTAEAOw==
// @grant        none
// ==/UserScript==

(function() {
    'use strict';

    var existSerps = [];

    function onArrival(selector, callback, interval, limit) {
        console.log('onArrival');
        interval = interval || 1000; // Default wait time is 1 second.
        limit = limit || 10; // Default number of attempts is 10.
        var el = document.querySelector(selector);
        if (el != null) {
            console.log('!el');
            if (callback != null) {
                console.log('callback');
                callback(el);
            }
        } else {
            if (limit > 0) {
                console.log('limit > 0');
                setTimeout(function() {
                    onArrival(selector, callback, interval, limit - 1);
                }, interval);
            } else {
                console.log('Element not found!');
            }
        }
    }

    // Wait 5 times, 1 second at a time, for the input element to arrive.
    onArrival('textarea', function(el) {
        console.log('Google Search Text : ' + el.value);
        // Search Github
        startSearch(el.value);
    }, 1000, 5)

async function startSearch(search) {
    console.log('startSearh');
    var serps = await searchGithub(search);
    rankSerps(serps);

    if (serps.length == 0) {
        iterateSearch(search)
    }

    showSerp();
}

async function iterateSearch(search) {
    await splitSearch(search);
}

/**
 This function will divide our search string into half and half and half and goes on
 And then search github with that divided half.
**/
async function splitSearch(search) {
    var searchArrays = search.split(" ");
    var halfs = splitHalf(searchArrays);
    var serps = [];

    // This mean either one of them is already empty string, so too short , lets no more query
    if (halfs[0].length >= 2 && halfs[1].length >= 3) {

        var str1 = halfs[0].join(' ');
        var str2 = halfs[1].join(' ');

        if (search == str1 || search == str2) {
            return;
        }

        serps = await searchGithub(str1);
        rankSerps(serps);

        serps = await searchGithub(str2);
        rankSerps(serps);

        iterateSearch(str1);
        iterateSearch(str2);

        return;
    }
}

function splitHalf(searchArrays) {
    var leftSide = searchArrays.splice(0, Math.floor(searchArrays.length / 2));
    var rightSide = searchArrays;

    return [leftSide, rightSide];
}

function rankSerps(newSerps) {
    let found = false;
    if (!existSerps) {
        existSerps = results;
    }

    newSerps.forEach(function (newSerp) {
        existSerps.forEach(function (existSerp) {
            if (newSerp.id == existSerp.id) {
                found = true;
                existSerp.score = existSerp.score * 1.5;
            }
        });

        if (!found) {
            existSerps.push(newSerp);
        }
    });
    console.log('Ranking Serps done');
}

function sortSerps() {
    console.log('Sorting serps');
    existSerps.sort(function (a, b) {
        return b.score - a.score;
    });
}

async function searchGithub(searchText) {
    console.log('Searching github : ' + searchText);
    // insertResult('Searching github : ' + searchText, false);
    var results = await $.ajax({
        url: "https://api.github.com/search/issues?q="+searchText+"+user:cjmling"
    });
    // insertResult('Found : ' + results.items.length + ' result', false);
    return results.items;
}

function showSerp() {
    // insertResult('Finishing Serp Process', false);
    var serp = '';
    if(existSerps.length == 0){
        serp = "<h2>-- NO GITHUB ISSUES FOUND --</h2>";
    }
    console.log('Found ' + existSerps.length + ' results');
    existSerps.forEach(function(item){
        serp +=   `<div style="border-bottom:1px solid #EEE"><h4><a href="${item.html_url}">${Math.floor(item.score)} : ${item.title}</a></h4><p>${item.body.substring(0, 100)}</p></div>`;
    });

    insertResult(serp, true);
}

function insertResult(text, prepend) {
    console.log('insertResult');
    var targetElement = $('body');
    if (targetElement.length === 0) {
        targetElement = $(".rhsvw");
    }

    if (targetElement.length === 0) {
        targetElement = $(".rhscol");
    }

    if (targetElement.length === 0) {
        targetElement = $("#center_col");
    }

    if (targetElement.length === 0) {
        console.log('Target Element Not Found');
    }

    targetElement.prepend('<div style="position: absolute; right: 0; top: 100px; z-index: 9; border:1px solid #EEE;padding:10px;width: 550px; background-color: #000 ">'+text+'</div>');
}

})();
cjmling commented 5 years ago

NOTE :

@match in header is important, script will run only on those page.