Advertisement
Guest User

Vivaldi Tab Scroll (page to top)

a guest
Aug 1st, 2021
79
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. // Vivaldi Tab Scroll
  2. // https://forum.vivaldi.net/topic/27856/tab-scroll
  3. // Clicking on an active tab scrolls page to top, clicking it again returns to previous scroll position.
  4. // Credits to tam710562 from Vivaldi Forum for coming up with the sessionStorage solution, which made this possible.
  5. // Small improvement from RayTwitty - smooth scrolling.
  6.  
  7. {
  8.     function tabScrollExit(tab) {
  9.         tab.removeEventListener('mousemove', tabScrollExit);
  10.         tab.removeEventListener('click', tabScrollTrigger);
  11.     }
  12.  
  13.     function tabScrollTrigger(tab) {
  14.         chrome.scripting.executeScript({
  15.             target: {tabId: Number(tab.parentNode.id.replace(/\D/g, ''))},
  16.             function: tabScrollScript
  17.         })
  18.         tabScrollExit(tab);
  19.     }
  20.  
  21.     function tabScroll(e, tab) {
  22.         if (tab.parentNode.classList.contains('active') && e.which === 1 && !e.shiftKey && !e.ctrlKey && !e.altKey && !e.metaKey) {
  23.             tab.addEventListener('mousemove', tabScrollExit(tab));
  24.             tab.addEventListener('click', tabScrollTrigger(tab));
  25.         }
  26.     }
  27.  
  28.     const tabScrollScript = () => {
  29.         let offset = window.pageYOffset;
  30.         let options = {
  31.             top: 0,
  32.             behavior: 'smooth'
  33.         }
  34.         if (offset > 0) {
  35.             window.sessionStorage.setItem('tabOffset',offset);
  36.         }
  37.         else {
  38.             options.top = window.sessionStorage.getItem('tabOffset')||0;
  39.         }
  40.         window.scrollTo(options);
  41.     }
  42.  
  43.     var appendChild = Element.prototype.appendChild;
  44.     Element.prototype.appendChild = function() {
  45.         if (arguments[0].tagName === 'DIV' && arguments[0].classList.contains('tab-header')) {
  46.             setTimeout(function() {
  47.                 const trigger = event => tabScroll(event, arguments[0]);
  48.                 arguments[0].addEventListener('mousedown', trigger);
  49.             }.bind(this, arguments[0]));
  50.         }
  51.         return appendChild.apply(this, arguments);
  52.     }
  53. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement