Monchoman45

Hide thing

May 22nd, 2011
193
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1.     // ***************************************
  2.     // Auto hide (courtesy of Xd1358)
  3.     // ***************************************
  4.  
  5.     function addHideButtons() {
  6.             var hidables = getElementsByClass( 'hidable' );
  7.      
  8.             for( var i = 0; i < hidables.length; i++ ) {
  9.                     var box = hidables[i];
  10.                     var button = getElementsByClass( 'hidable-button', box, 'span' );
  11.      
  12.                     if( button != null && button.length > 0 ) {
  13.                             button = button[0];
  14.      
  15.                             button.onclick = toggleHidable;
  16.                             button.appendChild( document.createTextNode( '[Hide]' ) );
  17.      
  18.                             if( new ClassTester( 'start-hidden' ).isMatch( box ) ) {
  19.                                     button.onclick( 'bypass' );
  20.                             }
  21.                     }
  22.             }
  23.     }
  24.      
  25.     function toggleHidable() {
  26.             var parent = getParentByClass( 'hidable', this );
  27.             var content = getElementsByClass( 'hidable-content', parent );
  28.             var nowShown;
  29.      
  30.             if( content != null && content.length > 0 ) {
  31.                     content = content[0];
  32.      
  33.                     if( content.style.display == 'none' ) {
  34.                             content.style.display = content.oldDisplayStyle;
  35.                             this.firstChild.nodeValue = '[Hide]';
  36.                             nowShown = true;
  37.                     } else {
  38.                             content.oldDisplayStyle = content.style.display;
  39.                             content.style.display = 'none';
  40.                             this.firstChild.nodeValue = '[Show]';
  41.                             nowShown = false;
  42.                     }
  43.             }
  44.     }
  45.  
  46. function getElementsByClass( searchClass, node, tag ) {
  47.     var classElements = new Array();
  48.  
  49.     if( node == null ) {
  50.         node = document;
  51.     }
  52.  
  53.     if( tag == null ) {
  54.         tag = '*';
  55.     }
  56.  
  57.     var els = node.getElementsByTagName( tag );
  58.     var elsLen = els.length;
  59.     var tester = new ClassTester( searchClass );
  60.  
  61.     for( i = 0, j = 0; i < elsLen; i++ ) {
  62.         if( tester.isMatch( els[i] ) ) {
  63.             classElements[j] = els[i];
  64.             j++;
  65.         }
  66.     }
  67.  
  68.     return classElements;
  69. }
  70.  
  71. function getParentByClass( className, element ) {
  72.     var tester = new ClassTester( className );
  73.     var node = element.parentNode;
  74.  
  75.     while( node != null && node != document ) {
  76.         if( tester.isMatch( node ) ) {
  77.             return node;
  78.         }
  79.  
  80.         node = node.parentNode;
  81.     }
  82.  
  83.     return null;
  84. }
  85.  
  86. function ClassTester( className ) {
  87.     this.regex = new RegExp( "(^|\\s)" + className + "(\\s|$)" );
  88. }
  89.  
  90. ClassTester.prototype.isMatch = function( element ) {
  91.     return this.regex.test( element.className );
  92. }
Advertisement
Add Comment
Please, Sign In to add comment