Advertisement
Guest User

Untitled

a guest
Apr 26th, 2017
529
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.61 KB | None | 0 0
  1. 'use strict';
  2.  
  3. // INFORMATION
  4. // ===========
  5. // COPY THIS CODE INTO YOU BROWSER (chrome version >= 55)
  6. //
  7. // Description:
  8. // It will automatically change the client viewport
  9. // downwards. If the client viewport have reach the
  10. // the bottom the client viewport is moved to the
  11. // top of the page.
  12. //
  13. // Author:
  14. // thorgeir <thorgeir.sigurdsson@CYREN.com>
  15. //
  16. // CONSTANST
  17. // =========
  18. // CONTAINER:
  19. // A class name of an element that has its
  20. // Y coordinate at the bottom of the page.
  21. // TIME_INTERVAL:
  22. // The interval measures the time during which
  23. // the client viewport is visible.
  24. // DOWN:
  25. // The proportion of the client viewport height.
  26. // e.g. if DOWN equals 2 the viewport goes downwards
  27. // the half size of the client viewport height.
  28.  
  29. const CONTAINER='main-view'
  30. const TIME_INTERVAL=8000//ms = 8 sec
  31. const DOWN=2
  32.  
  33. // Expand all the views.
  34. document.querySelectorAll('.dash-row-header-title')
  35. .forEach(function(element) { element.click(); });
  36.  
  37. //
  38. // HELPER FUNCTIONS
  39. //
  40.  
  41. // Positioning the viewport
  42. // at the top of the page.
  43. function topPage(){
  44. console.log( 'Go to top' )
  45. window.scrollTo(0, 0);
  46. }
  47.  
  48. // Return the height of
  49. // the clients viewport
  50. function clientHeight(){
  51. return window.innerHeight;
  52. }
  53.  
  54. // Moves the client viewport downwards
  55. function down(){
  56. console.log( 'Scroll down by', clientHeight()/DOWN)
  57. window.scrollBy(0, clientHeight()/DOWN);
  58. }
  59.  
  60. // Return True if the client
  61. // viewport have reach the bottom
  62. // of the page otherwise False.
  63. function atBottom() {
  64. var main_view = document.getElementsByClassName(CONTAINER)
  65. var maxY = main_view[0].offsetHeight
  66. if ((clientHeight() + window.scrollY) >= maxY) {
  67. console.log( 'Reach the bottom', true )
  68. return true
  69. } else {
  70. console.log( 'Reach the bottom', false )
  71. return false
  72. }
  73. }
  74.  
  75. // Copy from stackoverflow
  76. // -> http://stackoverflow.com/questions/951021
  77. function sleep(ms) {
  78. return new Promise(resolve => setTimeout(resolve, ms));
  79. }
  80.  
  81. //
  82. // THE LOGIC
  83. //
  84. // Ping Pong calls between these two functions.
  85.  
  86. // Waits for a given milliseconds
  87. // and then starts the work.
  88. async function wait( ms ) {
  89. console.log('Taking a break...');
  90. var counter = counter - 1;
  91. await sleep( ms );
  92. work();
  93. }
  94.  
  95. // Move the client viewport down
  96. // or too the top of the page and
  97. // then wait for a while.
  98. function work() {
  99. if( atBottom() ){
  100. topPage();
  101. } else{
  102. down();
  103. }
  104. wait( TIME_INTERVAL );
  105. }
  106.  
  107. // Start the process
  108. work()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement