changeset 121:f235b50ebdd2

Add twitter default following tab
author nanaya <me@nanaya.net>
date Sat, 21 Jan 2023 01:21:54 +0900
parents f3f646436567
children 34eca18d4cbd
files twitter-default-following-tab.user.js
diffstat 1 files changed, 47 insertions(+), 0 deletions(-) [+]
line wrap: on
line diff
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/twitter-default-following-tab.user.js	Sat Jan 21 01:21:54 2023 +0900
@@ -0,0 +1,47 @@
+// ==UserScript==
+// @name        twitter default following tab
+// @namespace   https://nanaya.net
+// @match       https://twitter.com/home
+// @grant       none
+// @version     1.0.0
+// @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
+// ==/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();
+}