Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- // Vivaldi Tab Scroll
- // https://forum.vivaldi.net/topic/27856/tab-scroll
- // Clicking on an active tab scrolls page to top, clicking it again returns to previous scroll position.
- // Credits to tam710562 from Vivaldi Forum for coming up with the sessionStorage solution, which made this possible.
- // Small improvement from RayTwitty - smooth scrolling.
- {
- function tabScrollExit(tab) {
- tab.removeEventListener('mousemove', tabScrollExit);
- tab.removeEventListener('click', tabScrollTrigger);
- }
- function tabScrollTrigger(tab) {
- chrome.scripting.executeScript({
- target: {tabId: Number(tab.parentNode.id.replace(/\D/g, ''))},
- function: tabScrollScript
- })
- tabScrollExit(tab);
- }
- function tabScroll(e, tab) {
- if (tab.parentNode.classList.contains('active') && e.which === 1 && !e.shiftKey && !e.ctrlKey && !e.altKey && !e.metaKey) {
- tab.addEventListener('mousemove', tabScrollExit(tab));
- tab.addEventListener('click', tabScrollTrigger(tab));
- }
- }
- const tabScrollScript = () => {
- let offset = window.pageYOffset;
- let options = {
- top: 0,
- behavior: 'smooth'
- }
- if (offset > 0) {
- window.sessionStorage.setItem('tabOffset',offset);
- }
- else {
- options.top = window.sessionStorage.getItem('tabOffset')||0;
- }
- window.scrollTo(options);
- }
- var appendChild = Element.prototype.appendChild;
- Element.prototype.appendChild = function() {
- if (arguments[0].tagName === 'DIV' && arguments[0].classList.contains('tab-header')) {
- setTimeout(function() {
- const trigger = event => tabScroll(event, arguments[0]);
- arguments[0].addEventListener('mousedown', trigger);
- }.bind(this, arguments[0]));
- }
- return appendChild.apply(this, arguments);
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement