kiennguyen1101 / Click visible button using shortkeys

// ==UserScript==
// @name          Click visible button using shortkeys
// @namespace http://userscripts.org/users/524433
// @description Click visible inputs that are defined as button and has value "VP" using ctrl+shift+z or "Hide" using ctrl+shift+x
// @include http://www.tangthuvien.com/* 
// @include http://www.tangthuvien.vn/*
// @include http://www.lsb-thuquan.eu/*
// @version     1.9.4
// @grant none
// @require http://code.jquery.com/jquery-2.0.3.min.js
// ==/UserScript==


(function(d){d.fn.visible=function(e,i){var a=d(this).eq(0),f=a.get(0),c=d(window),g=c.scrollTop();c=g+c.height();var b=a.offset().top,h=b+a.height();a=e===true?h:b;b=e===true?b:h;return!!(i===true?f.offsetWidth*f.offsetHeight:true)&&b<=c&&a>=g}})(jQuery);

jQuery(document).ready(function($){
    
    if (window.location.hostname.indexOf('tangthuvien') != -1) {
      //  $("div[id^='post_message']").find('font').attr('size', 5)
        $("div[id^='post_message']").css('font-size','large')
        $("div[id^='post_message']").css('font-family','Palatino Linotype')
    }
    
    if (window.location.hostname.indexOf('lsb-thuquan') != -1) {
        $(".maincontent").css('font-size', '165%');
    }

//posts = jQuery('input[type=button][value~=VP]');
    posts = getPosts();
    //posts.find('span').css('font-family', 'Palatino Linotype');
buttons = posts.find('input[type=button]').filter(function(index) {
    
    if (this.value)
       text = this.value.toLowerCase();
    else
        text = this.innerHTML.toLowerCase();
    
    text = trim(text);
    text = removeVietnamese(text);
    
    if (RegExp("\\b" + 'han'+ "\\b").test(text)) {
        return false;
    }
    
    if (isNumber(text))
        return true;
    
    if (RegExp("[0-9]+([-,_][0-9]+)+").test(text))
        return true;
    
    return text.indexOf('viet') != -1 || 
           text.indexOf('vp') != -1 ||           
           text.indexOf('chuong') != -1 ||
           text.indexOf('hien') != -1 ||
           text.indexOf('chap') != -1 ||
           text.indexOf('doc') != -1 ||
           text.indexOf('mo') != -1 ||
           text.indexOf('...') != -1 ||
           text.indexOf('xem') != -1 ||
           text.indexOf('moi') != -1  ||
           text.indexOf('edit') != -1 ||
           text.indexOf('click') != -1
});


JKscroll = {index: -1, newIndex: -1, scrollSpeed: 150, debug: false, offset: 180}; // you can modify scrollSpeed as you wish
$(document).off('keydown').on('keydown', function (e) {
//document.addEventListener('keydown', function(e) {
    
    // pressed ctrl+shift+z  
	if (e.keyCode == 90 && e.shiftKey && e.ctrlKey && !e.altKey && !e.metaKey) {   
       	ShowCurrentPost();
		e.preventDefault();
	}

	//pressed ctrl+shift+x
	if (e.keyCode == 88 && e.shiftKey && e.ctrlKey && !e.altKey && !e.metaKey) {
		HideAllPosts();  
		e.preventDefault();
	}

	//J
	if(e.keyCode == 74 && !e.shiftKey && !e.ctrlKey && !e.altKey && !e.metaKey) {	
        MoveDown();
		e.preventDefault();
	}

	//K
	if(e.keyCode == 75 && !e.shiftKey && !e.ctrlKey && !e.altKey && !e.metaKey){
		MoveUp();
		e.preventDefault();
	}

	// shift + z:  hide post + move down + open visible
	if (e.keyCode == 90 && e.shiftKey && !e.ctrlKey && !e.altKey && !e.metaKey) {		
		HideAllPosts();	
		
		setTimeout(function() {
            if (MoveDown()) {
               ShowCurrentPost();
            }
        },250);
		
		e.preventDefault();	
	}
	
	//shift + A
	if (e.keyCode == 65 && e.shiftKey && !e.ctrlKey && !e.altKey && !e.metaKey) {
	   if (window.location.href.indexOf('page=') != -1 ) {
	       var nextPage =  parseInt(getParameterByName('page')) +1;
	       nextPage = isNaN(nextPage) ? 2 : nextPage;
	       var url = updateURLParameter(window.location.href, 'page', nextPage);
	       window.open(url);
	       /*
	       if (!$('#goToNextPage').length)
    	       $('body').append('<a id="goToNextPage" href="' + url + '" target="_blank">x </a>');
    	       
    	   $('#goToNextPage').get(0).click();*/
	   }
	}
  
  //d
  if (e.which == 68 && !e.shiftKey && !e.ctrlKey && !e.altKey && !e.metaKey) {
      //pagedown
      if (!jQuery('input.textbox').is(':focus'))
          $('html, body').animate({
            scrollTop: window.scrollY+window.innerHeight*.9
          }, JKscroll.scrollSpeed);		
    //  window.scrollTo(0,window.scrollY+window.innerHeight*.9);
  }
    
  //e
  if (e.which == 69 && !e.shiftKey && !e.ctrlKey && !e.altKey && !e.metaKey) {
      //pageup
      //window.scrollTo(0,window.scrollY-window.innerHeight*.9);
      if (!jQuery('input.textbox').is(':focus'))
          $('html, body').animate({
            scrollTop: window.scrollY-window.innerHeight*.9
          }, JKscroll.scrollSpeed);		
  }
  
});
    
    function getPosts() { 
        if (window.location.hostname.indexOf('tangthuvien') != -1) 
            return jQuery("[id^='post_message_']");
            //return jQuery('#postlist input[type=button]')
        
        return jQuery('input[type=button] .folded.clickable')
        
    }

    function ShowCurrentPost() {
        var currentPost = jQuery(buttons[JKscroll.index]);
        if (currentPost.is(':visible'))
			currentPost.click();
    }
	
	function ShowVisiblePosts() {
		for (var i =0; i < buttons.length; i++) {
			if (jQuery(buttons[i]).is(':visible'))
					buttons[i].click();			
		}
	}
	
	function HideAllPosts() {
        var hidePosts = posts.find('input[type=button]').filter(function(index) {
            if (this.value)
               text = this.value.toLowerCase();
            else
               text = this.innerHTML.toLowerCase();
            
            /*console.log(text);*/
            
            /*if (isNumber(text))
                return true;*/
            
            text = text.trim();
//             text = removeVietnamese(text);
            return text.indexOf('hide') != -1 || 
                   text.indexOf('ẩn') != -1 
                   
        });
	   hidePosts.each(function(index) {
	       this.click();
	   });
		/*for (var i =0; i < buttons.length; i++) {
			if (buttons[i].value.indexOf('Hide') !==-1) {		
				buttons[i].click();
			}
		}  */
	}  
  
	function Scroll() {
		$('html, body').animate({
				scrollTop: jQuery(buttons[JKscroll.newIndex]).offset().top - JKscroll.offset
			}, JKscroll.buttons);		
	}
	
	function MoveDown() {
		if (JKscroll.index < buttons.length -1) {
			JKscroll.index++;
			JKscroll.newIndex = JKscroll.index;
			Scroll();	
			return true;
		}
		
		return false;
	}
	
	function MoveUp() {
		if (JKscroll.index > 0) {
			JKscroll.index--;
			JKscroll.newIndex = JKscroll.index;
			Scroll();	
		}
	}    
    
  function simulateKeyPress(keyCode) {
      jQuery.event.trigger({ type: 'keypress', keyCode})
  }
	
	function getParameterByName(name) {
        name = name.replace(/[\[]/, "\\\[").replace(/[\]]/, "\\\]");
        var regex = new RegExp("[\\?&]" + name + "=([^&#]*)"),
            results = regex.exec(location.search);
        return results == null ? "" : decodeURIComponent(results[1].replace(/\+/g, " "));
    }
    
    function updateURLParameter(url, param, paramVal) {
        var TheAnchor = null;
        var newAdditionalURL = "";
        var tempArray = url.split("?");
        var baseURL = tempArray[0];
        var additionalURL = tempArray[1];
        var temp = "";
    
        if (additionalURL) 
        {
            var tmpAnchor = additionalURL.split("#");
            var TheParams = tmpAnchor[0];
                TheAnchor = tmpAnchor[1];
            if(TheAnchor)
                additionalURL = TheParams;
    
            tempArray = additionalURL.split("&");
    
            for (i=0; i<tempArray.length; i++)
            {
                if(tempArray[i].split('=')[0] != param)
                {
                    newAdditionalURL += temp + tempArray[i];
                    temp = "&";
                }
            }        
        }
        else
        {
            var tmpAnchor = baseURL.split("#");
            var TheParams = tmpAnchor[0];
                TheAnchor  = tmpAnchor[1];
    
            if(TheParams)
                baseURL = TheParams;
        }
    
        if(TheAnchor)
            paramVal += "#" + TheAnchor;
    
        var rows_txt = temp + "" + param + "=" + paramVal;
        return baseURL + "?" + newAdditionalURL + rows_txt;
    }
    
    function isNumber(n) {
        return !isNaN(parseFloat(n)) && isFinite(n);
    }
    
    function removeVietnamese(str) {  
      str= str.toLowerCase();  
      str= str.replace(/à|á|ạ|ả|ã|â|ầ|ấ|ậ|ẩ|ẫ|ă|ằ|ắ|ặ|ẳ|ẵ/g,"a");  
      str= str.replace(/è|é|ẹ|ẻ|ẽ|ê|ề|ế|ệ|ể|ễ/g,"e");  
      str= str.replace(/ì|í|ị|ỉ|ĩ/g,"i");  
      str= str.replace(/ò|ó|ọ|ỏ|õ|ô|ồ|ố|ộ|ổ|ỗ|ơ|ờ|ớ|ợ|ở|ỡ/g,"o");  
      str= str.replace(/ù|ú|ụ|ủ|ũ|ư|ừ|ứ|ự|ử|ữ/g,"u");  
      str= str.replace(/ỳ|ý|ỵ|ỷ|ỹ/g,"y");  
      str= str.replace(/đ/g,"d");  
      return str;  
    }
  

});