view twitter-default-following-tab.user.js @ 125:31b1ab00cc7a

Not sure how tampermonkey even work
author nanaya <me@nanaya.net>
date Sat, 21 Jan 2023 02:15:02 +0900
parents 6d280338e113
children bf1f7164fd4f
line wrap: on
line source

// ==UserScript==
// @name        twitter default following tab
// @namespace   https://nanaya.net
// @match       https://twitter.com/home
// @match       https://mobile.twitter.com/home
// @grant       none
// @run-at      document-end
// @version     1.0.2
// @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;

const observer = new window.MutationObserver(selectFollowingTab);
let stopObservingTimeout;

function selectFollowingTab () {
  if (selected) return;

  const followingTab = document.querySelectorAll('[href="/home"][role=tab]')?.[1];
  if (followingTab == null || followingTab.getAttribute('aria-selected') === 'true') return;

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

function init () {
  selectFollowingTab();
  if (!selected) {
    observer.observe(document, { childList: true, subtree: true });
  }
  stopObservingTimeout = setTimeout(() => {
    console.log("couldn't find 'Following' tab, stop observing page");
    observer.disconnect();
  }, 60000);
}

if (document.readyState === 'loading') {
  document.addEventListener('DOMContentLoaded', init);
} else {
  init();
}