view twitter-default-following-tab.user.js @ 129:dc960ef1248b

Add lashinbang always consent script
author nanaya <me@nanaya.net>
date Tue, 07 Feb 2023 21:51:34 +0900
parents f2a53a2b9a5b
children 7a7aafa65bae
line wrap: on
line source

// ==UserScript==
// @name        twitter default following tab
// @namespace   https://nanaya.net
// @match       https://mobile.twitter.com/*
// @match       https://twitter.com/*
// @grant       none
// @run-at      document-start
// @version     1.1.1
// @author      nanaya
// @description Always select Following tab on first load
// @downloadURL https://hg.nanaya.net/ec-userscripts/raw-file/tip/twitter-default-following-tab.user.js
// @updateURL   https://hg.nanaya.net/ec-userscripts/raw-file/tip/twitter-default-following-tab.user.js
// ==/UserScript==

'use strict';

let selected = false;
let observing = true;

function isHome () {
  return window.location.pathname === '/home';
}

let resumeObserverTimeout;
// prevent reselect on browser back/forward navigation
function pauseObserver () {
  console.debug('pausing observer');
  observing = false;
  clearTimeout(resumeObserverTimeout);
  resumeObserverTimeout = setTimeout(() => {
    console.debug('resuming observer');
    observing = true;
    selected = isHome();
  }, 1000);
}

function selectFollowingTab () {
  if (!observing) return;

  if (!isHome()) {
    selected = false;
    return;
  }

  if (selected) return;

  const followingTab = document.querySelectorAll('a[href="/home"][role=tab]')?.[1];
  if (followingTab == null) return;

  if (followingTab.getAttribute('aria-selected') === 'true') {
    selected = true;
    return;
  }

  console.log('selecting "Following" tab');
  selected = true;
  followingTab.click();
}

new window.MutationObserver(selectFollowingTab).observe(document, { childList: true, subtree: true });
selectFollowingTab();
window.addEventListener('popstate', pauseObserver);