Tatoeba Page Navigator

Makes the current page number a number input field or navigate with the up or down keys (hit enter to submit)

  1. // ==UserScript==
  2. // @name Tatoeba Page Navigator
  3. // @copyright Jakob V. <jakov@gmx.at>
  4. // @description Makes the current page number a number input field or navigate with the up or down keys (hit enter to submit)
  5. // @include http://*.tatoeba.org/*
  6. // @include https://*.tatoeba.org/*
  7. // @match http://*.tatoeba.org/*
  8. // @match https://*.tatoeba.org/*
  9. // @require http://code.jquery.com/jquery-1.5.js
  10. // @require https://greasyfork.org/scripts/9476-jscroll/code/jScroll.js?version=48212
  11. // @grant GM_addStyle
  12. // @grant unsafeWindow
  13. // @version 0.0.1.20150428074847
  14. // @namespace https://greasyfork.org/users/10789
  15. // ==/UserScript==
  16.  
  17. // Embedded script "jscroll":
  18. // * jScroll - jQuery Plugin for Infinite Scrolling / Auto-Paging
  19. // * http://jscroll.com/
  20. // *
  21. // * Copyright 2011-2013, Philip Klauzinski
  22. // * http://klauzinski.com/
  23. // * Dual licensed under the MIT and GPL Version 2 licenses.
  24. // * http://jscroll.com/#license
  25.  
  26. // PAGE NAVIGATOR
  27.  
  28. loc = $('.paging .current:first').text();
  29. console.log('loc:'+loc);
  30. $('.paging .current').removeClass('current').text('').append($('<input class="current" value="'+loc+'">').css({'border':'0', 'width':loc.length*8+'px'}).bind('keyup', function(e){
  31. num = $(this).val();
  32. total = 1*( $('.paging :contains(">>")').is('.disabled') ? $('.paging .numbers a:last').attr('href').split(':')[1] : $('.paging a:contains(">>")').attr('href').split(':')[1] );
  33. console.log('e.which:'+e.which);
  34. if(e.which == 13){
  35. if( num*1<=total && num*1>0 ){
  36. document.location.href = document.location.href.replace(/\/?(?:page:(\d+))?$/, '/page:' + num );
  37. }
  38. else if( ((num*1+total*99999-1)%total+1)>0 ){
  39. num = (num*1+total*99999-1)%total+1;
  40. $(this).val( num );
  41. $(this).css({'outline':'2px solid #6CAA50'});
  42. }
  43. else if( (num+'').replace(/[^0-9]/g, '')!='' && (( ((num+'').replace(/[^-0-9]/g, '').substr(0,1)+(num+'').substr(1).replace(/[^0-9]/g, ''))*1 +total*99999-1)%total+1)>0 ){
  44. console.log('num before replace:'+num);
  45. num = ( ((num+'').replace(/[^-0-9]/g, '').substr(0,1)+(num+'').substr(1).replace(/[^0-9]/g, ''))*1 +total*99999-1)%total+1;
  46. console.log('num after replace:'+num);
  47. $(this).val( num );
  48. $(this).css({'outline':'2px solid #6CAA50'});
  49. }
  50. }
  51. else {
  52. if(e.which == 38){
  53. num = (num*1+total*99999+1-1)%total+1;
  54. $(this).val(num);
  55. }
  56. else if(e.which == 40){
  57. num = (num*1+total*99999-1-1)%total+1;
  58. $(this).val(num);
  59. }
  60.  
  61. if( num*1<=total && num*1>0 ){
  62. $(this).css({'outline':'2px solid #6CAA50'});
  63. }
  64. else if( (num+'').replace(/[^0-9]/g, '')!='' && ((((num+'').replace(/[^-0-9]/g, '').substr(0,1)+(num+'').substr(1).replace(/[^0-9]/g, ''))*1+total*99999-1)%total+1)>0 ){
  65. $(this).css({'outline':'2px solid #F8B815'});
  66. }
  67. else{
  68. $(this).css({'outline':'2px solid #E44242'});
  69. }
  70. }
  71. $(this).css({'width':((num+'').length)*8+'px'});
  72. console.log('(num+"").length:'+(num+'').length);
  73. }));
  74. // JSCROLL-ENHANCED IN-PAGE LOADING
  75. // replace "autoTrigger: false," with "autoTrigger: true," for automatic loading.
  76.  
  77. css = "\
  78. .jscroll-inner { } \
  79. .jscroll-added:last-child .paging, \
  80. .paging.initial { font-size:2em !important; padding: 1em; } \
  81. .jscroll-added .paging a, \
  82. .jscroll-added h2 + .paging, \
  83. .jscroll-added .sendMessageForm + .paging, \
  84. .jscroll-added > .module > #sendMessageForm, \
  85. .jscroll-added > .module > #sendMessageForm + .paging, \
  86. .jscroll-added h2, \
  87. .paging.initial a { display:none;} \
  88. .jscroll-added:last-child .paging:last-child a.next, \
  89. .paging.initial a.next { display:inline-block; } \
  90. .jscroll-added:last-child .paging:last-child a.next:before, \
  91. .paging.initial a.next:before { content:'>>>>>>'; } \
  92. .jscroll-added .wall :first-child { margin-top: 0;} \
  93. ";
  94. GM_addStyle(css);
  95.  
  96.  
  97. initial_paging = $('#main_content .module .paging:last');
  98. initial_paging.after(initial_paging.clone().addClass('initial'));
  99.  
  100. $('#main_content .module').jscroll({
  101. debug:true,
  102. autoTrigger: false,
  103. loadingHtml: '<div class="paging"><img src="http://flags.tatoeba.org/img/loading.gif" alt="Loading" /></div>',
  104. padding: 20,
  105. nextSelector: '.paging a.next:last',
  106. contentSelector: '#main_content .module',
  107. callback: function(){
  108. var num = $('.current:last').text()*1;
  109. window.history.pushState("object or string", document.title.replace(/\/?(?:page:(\d+))?$/, '/page:' + num ), document.location.href.replace(/\/?(?:page:(\d+))?$/, '/page:' + num ));
  110. //$('.jscroll-added a.replyLink')[0].setAttribute('onclick','function() {manageReplyForm($(this));}');
  111. $('.jscroll-added a.replyLink').each(function(){$(this).attr('href', document.location.href.replace(/\/?(?:page:(\d+))?(#.*)?$/, '/page:0' + num ) + '#'+ $(this).attr('id'))});
  112. }
  113. });