Advertisement
Javi

Greasemonkey: links con teclado

Oct 12th, 2017
117
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.31 KB | None | 0 0
  1. // ==UserScript==
  2. // @name Keylinks
  3. // @namespace programar.cloud
  4. // @match *://*/*
  5. // @grant none
  6. // @require https://code.jquery.com/jquery-2.2.4.min.js
  7. // ==/UserScript==
  8.  
  9. console.log('Initializing keyliks.');
  10.  
  11. console.log('Declaring isInViewport plugin.');
  12. $.fn.isInViewport = function() {
  13. var elementTop = $(this).offset().top;
  14. var elementBottom = elementTop + $(this).outerHeight();
  15.  
  16. var viewportTop = $(window).scrollTop();
  17. var viewportBottom = viewportTop + $(window).height();
  18.  
  19. return elementBottom > viewportTop && elementTop < viewportBottom;
  20. };
  21.  
  22. console.log('Creating tip template.');
  23. let $elem=$('<a>')
  24. .addClass('keylink')
  25. .css({
  26. //position: 'absolute',
  27. display : 'inline-block',
  28. //width: '16px',
  29. //height: '16px',
  30. zIndex: 9999,
  31. left: '-16px',
  32. top : '0px',
  33. backgroundColor : 'yellow',
  34. color : 'black',
  35. fontSize: '60%'
  36. });
  37.  
  38.  
  39. $(document).keyup(function(evt) {
  40. console.log(evt.which, evt.shiftKey);
  41. if (evt.which !== 70 /*f*/ || evt.shiftKey === false || evt.ctrlKey === false) return;
  42.  
  43. let $oldLinks = $('.keylink');
  44. if ($oldLinks.length > 0) {
  45. $oldLinks.detach();
  46. } else {
  47. let $links = $('a')
  48. .filter(function(){return $(this).isInViewport()})
  49. .prepend(idx => $elem.clone().text('l'+idx));
  50. }
  51.  
  52. });
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement