changeset 127:a701294f38ff

More reliable state handling
author nanaya <me@nanaya.net>
date Sat, 21 Jan 2023 03:15:26 +0900
parents bf1f7164fd4f
children f2a53a2b9a5b
files twitter-default-following-tab.user.js
diffstat 1 files changed, 38 insertions(+), 28 deletions(-) [+]
line wrap: on
line diff
--- a/twitter-default-following-tab.user.js	Sat Jan 21 02:30:07 2023 +0900
+++ b/twitter-default-following-tab.user.js	Sat Jan 21 03:15:26 2023 +0900
@@ -1,13 +1,11 @@
 // ==UserScript==
 // @name        twitter default following tab
 // @namespace   https://nanaya.net
-// @match       https://mobile.twitter.com/
-// @match       https://mobile.twitter.com/home
-// @match       https://twitter.com/
-// @match       https://twitter.com/home
+// @match       https://mobile.twitter.com/*
+// @match       https://twitter.com/*
 // @grant       none
-// @run-at      document-end
-// @version     1.0.3
+// @run-at      document-start
+// @version     1.1.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
@@ -17,36 +15,48 @@
 'use strict';
 
 let selected = false;
+let observing = true;
 
-const observer = new window.MutationObserver(selectFollowingTab);
-let stopObservingTimeout;
+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('[href="/home"][role=tab]')?.[1];
-  if (followingTab == null || followingTab.getAttribute('aria-selected') === 'true') return;
+  if (followingTab == null) return;
 
-  console.log("selecting 'Following' tab");
+  if (followingTab.getAttribute('aria-selected') === 'true') {
+    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();
-}
+new window.MutationObserver(selectFollowingTab).observe(document, { childList: true, subtree: true });
+selectFollowingTab();
+window.addEventListener('popstate', pauseObserver);