All pastes #1021411 Raw Edit

search_and_navigate.js

public javascript v1 · immutable
#1021411 ·published 2008-05-17 23:49 UTC
rendered paste body
// ==UserScript==// @name           search_and_navigate.js// @namespace      http://example.org// @description    adds navigational shortcuts and an instant-search features// @include        http://www.norbits.net/*// @include        http://norbits.net/*// ==/UserScript== (function(){    var labelColor = 'green';   //If the search box should be initially displayed. "block" or "none"   var initialDisplay = 'none';       var closeButton='data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAAAoAAAAKCAYAAACNMs+9AAAAAXNSR0IArs' +'4c6QAAAAZiS0dEAP8A/wD/oL2nkwAAAAlwSFlzAAALEwAACxMBAJqcGAAAAAd0SU1FB9gFERYGGE9ha3' +'oAAAFfSURBVBjTBcHLahNhGIDh9/sPM+OkmKRNJFKiVnugMVBqKW7UlYVegAtvwZW34j14D90JupDair' +'S4kOIBjMRQtYVkKkb/mfyfzyMr4E8xnRJ9rGhPoQmCoIUgny3y/CpxKKlzXVNVT3KkW4PXXvRIRGIZ6U' +'/hwW+hKI17ZusxPl1G7u8I35cs4VEipo52d6ykXaUWVDemqk3XQO5uG6ltpbF3LGwdKOO/jvxeqj9bgX' +'n+Sf4nsm36RlfXvUrnEtVuxuCwZGXd86vlmHZyytsJ9Ay3TEso6okWc5bhi0B+x/PuY4ksGIYNw7e618' +'k8euEmlpczww2v/Nj0jB5e5svelHYmBKeEmdC4sHywbYdpKtevCUU/Y+A949WMM5RYVNSOSubez9iz59' +'4VMcbxRFgcQXoKcRBJjgMLbyuy/ZJXb7AH4lPTdBqTNZGljtWbVxLaBjgLcj5S+Xqi8VNQE/4DwQmN67' +'EZdgMAAAAASUVORK5CYII=';   /* ---------------------------------- No edit ---------------------------------- */   if(location.hostname.indexOf('norbits.net') == -1) {       return false;   }    function getNodes(expression, startElement) {               startElement = startElement || document;       var nodes = document.evaluate( expression, startElement, null,                                       XPathResult.ORDERED_NODE_SNAPSHOT_TYPE, null );       return nodes;   }    function _toggleVisibility(){        var box = document.getElementById('__sn_container');        if(!box) {            return;        }        if(box.style.display == 'block') {            box.style.display = 'none';        } else {               box.style.display = 'block';            var searchBox = document.getElementById('___sn_search');            if(searchBox) {                searchBox.focus();                searchBox.select();            }        }    }      var get = {};   // http://www.bennadel.com/blog/695-Ask-Ben-Getting-Query-String-Values-In-JavaScript.htm   window.location.search.replace(   new RegExp( "([^?=&]+)(=([^&]*))?", "g" ),       function( $0, $1, $2, $3 ) {           get[$1] = unescape($3).replace(/\+/g, ' ');       }   );    function _t(e) {        try{            var body = document.body;           if(!body) {                return;           }             //s is reserved, and will not be triggered           var labels = {h: ['Hjem'], f:['Forum'], q:['FAQ'], b:['Liste over torrents'],                         u:['Last opp'], p:['Profil'], d:['Detaljer'],                         v:['Venner'], t:['Mine torrents']};                      //add labels to the links present in the dom           var sidePanel = getNodes('//table[@class="main"]//td[@class="embedded"]'+                        '//h2[contains(text(), "Generelt")]/..').snapshotItem(0);           //opera before 9.5 would return a text node (nodeType=3),           //newer versions plus fx skips text nodes           // and returns a node with nodeType=1           // "../" in opera before 9.5 would jump one node in the DOM           //while it should jump to the previous node           // defined in the xpath, the "td"           if(sidePanel.nodeType == 3) {                           sidePanel = sidePanel.parentNode.parentNode;                      }           for(var modifier in labels) {                var text = labels[modifier][0];                var node = getNodes('.//a[text()="' + text + '"]', sidePanel).snapshotItem(0);                if(!node) {                    continue;                }                                //save nodes for later use                labels[modifier][1] = node;                                var label = document.createElement('span');                label.style.color = labelColor;                label.appendChild(document.createTextNode(' [' + modifier + ']' ));                node.appendChild(label);                                          }            var container = document.createElement('div');           container.id = '__sn_container'                       container.style.backgroundColor='#e8e8e8';           container.style.display = initialDisplay;                                 container.style.width = '240px';           container.style.height = '50px';             container.style.position = 'fixed'           container.style.top='50%';           container.style.left='50%';            container.style.marginTop = '-25px'           container.style.marginLeft = '-100px'           container.style.border = '2px solid black';           container.MozBorderRadius = '2em';             var win =  document.createElement('div');           win.style.backgroundColor='#d7d7d7'           win.style.color='red'            win.style.fontSize='18px'           var box= document.createElement('span');            //box.appendChild(document.createTextNode('*'))           var btn = new Image();           btn.src = closeButton;           btn.addEventListener('mouseover', function(){            btn.style.opacity = 0.8;                      }, false);                      btn.addEventListener('mouseout', function(){            btn.style.opacity = 1;                      }, false);                      box.appendChild(btn)                      win.appendChild(box)            box.addEventListener('click', _toggleVisibility, false);             container.appendChild(win);            var form = document.createElement('form');           form.method="get";           form.action="browse.php";            var controlsContainer = document.createElement('div');           controlsContainer.style.margin = '20px';            var searchLabel = document.createElement('label');           searchLabel.setAttribute('for', 'search');           searchLabel.appendChild(document.createTextNode('search '));            controlsContainer.appendChild(searchLabel);            var search = document.createElement('input');                      search.name='search';           search.id='___sn_search';           search.type='text'           search.value = get['search'] || '';           controlsContainer.appendChild(search);            var submit = document.createElement('input');           submit.type='submit';           submit.value='Go!';           controlsContainer.appendChild(submit);            form.appendChild(controlsContainer);           container.appendChild(form)            body.appendChild(container);            window.addEventListener('keypress',            function(event){                var target = event.target;                var isFormElement = (target.form != undefined);                 //let user type normally on form elements                //do not listen if modifiers are present                if(isFormElement || event.altKey || event.ctrlKey ||                        event.metaKey || event.shiftKey || event.which == 0) {                    return;                }                var keyPressed = String.fromCharCode(event.which).toLowerCase();                                //e.isChar hack                if(!keyPressed) {                    return;                }                if(keyPressed == 's') {                    //stops focusd element from recieving char "s"                    event.preventDefault();                    _toggleVisibility();                } else {                    if(labels.hasOwnProperty(keyPressed)) {                                            var anchor = labels[keyPressed][1];                        if(!anchor) {                            throw new Error('failure to get href associated with modifier ' + keyPressed);                        }                        anchor.focus();                        //alert(newLocation);                        window.location = anchor.href;                                        }                }           }, false);        }catch(e) {            alert('feil: ' + e)       }   }   if(window.opera) {         window.addEventListener('DOMContentLoaded', _t, false);    } else {        window.addEventListener('load', _t, false);   }})();