Monday, February 7, 2011

Adds results from your http://historio.us/ bookmarks when you search google


// ==UserScript==
// @id             historio.us results with google search
// @name           historious results with google search
// @namespace      historious
// @include        http://www.google.com.au/search?*
// @include        http://www.google.com/search?*
// @version        0.1
// ==/UserScript==

var searchText = document.querySelector('input[title="Search"]');

if(searchText){


    var historiousGM={
            nrDiv:document.querySelector('#nr_container'),
            hisSearchURL:"http://foo.historio.us/search/?q="+searchText.value+"&search=search+my+history",
            resultsUL:document.createElement('ul'),
            createElements:function(item,first){

                var linkDivResultliCon=document.createElement('li');
                linkDivResultliCon.setAttribute('class','linkDivResultliCon');

                var linkH3=document.createElement('h3');
                linkH3.setAttribute('class','linkH3');

                var link=item;
                if(!first){
                    link=item.firstElementChild.firstElementChild;
                    link.href=item.querySelector('.link').textContent;
                }

                linkH3.appendChild(link);

                linkDivResultliCon.appendChild(linkH3);
            
                if(!first){
                    linkDivResultliCon.appendChild(item.querySelector('.resulttext'));
                }
                historiousGM.resultsUL.appendChild(linkDivResultliCon);

            },
            onload:function(response){

                //console.log(response.responseText);
                var dt = document.implementation.createDocumentType("html", "-//W3C//DTD HTML 4.01 Transitional//EN", "http://www.w3.org/TR/html4/loose.dtd");
                var doc = document.implementation.createDocument('','', dt);
                var html = doc.createElement('html');
                html.innerHTML = response.responseText;
                var newDoc = doc.appendChild(html);  

                var results = newDoc.querySelectorAll('#results>li');

                historiousGM.resultsUL.setAttribute('id','resultsUL');

                var hisA=document.createElement('a');
                hisA.href=historiousGM.hisSearchURL;
                hisA.setAttribute('id','hisA');
                hisA.innerHTML='historio.us';                

                historiousGM.createElements(hisA,true);

                [].forEach.call(results,function(item,index,array){ 
                    historiousGM.createElements(item,false);
                });
             
                historiousGM.nrDiv.appendChild(historiousGM.resultsUL);

            }
    };

    GM_addStyle ('#resultsUL{'+
            'float: right;'+
            'left: '+(document.width-515)+'px;'+
            'list-style: none outside none;'+
            'position: absolute;'+
            'top: 0;'+
            'width: 500px;'+
            'opacity: 0.8;'+
        '}'+
        '.linkDivResultliCon{'+
        '    margin-top: 10px;'+
        '}'+
        '.linkDivResultliCon a{'+
        '   color:#BF1B2C !important;'+
        '   font-size:0.9em !important;'+
        '}'+
        '.linkDivResultliCon #hisA{'+
        '   font-size:1.3em !important;'+
        '}'+
        '.linkDivResultliCon .resulttext{'+
        '   font-size:0.9em !important;'+
        '}'+
        '#cnt{'+
        '   max-width:none !important;'+
        '}'
    );
       


    GM_xmlhttpRequest({
        method: "GET",
        url: historiousGM.hisSearchURL,
        onload: function(response) {
            historiousGM.onload(response);
        }
    });    
    
}

0 comments:

Post a Comment