view twitter-default-following-tab.user.js @ 124:6d280338e113

Apparently both are required? or recommended
author nanaya <me@nanaya.net>
date Sat, 21 Jan 2023 02:08:29 +0900
parents 87442313317d
children 31b1ab00cc7a
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.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;

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();
}