Mercurial > ec-userscripts
comparison twitter-default-following-tab.user.js @ 127:a701294f38ff
More reliable state handling
author | nanaya <me@nanaya.net> |
---|---|
date | Sat, 21 Jan 2023 03:15:26 +0900 |
parents | bf1f7164fd4f |
children | f2a53a2b9a5b |
comparison
equal
deleted
inserted
replaced
126:bf1f7164fd4f | 127:a701294f38ff |
---|---|
1 // ==UserScript== | 1 // ==UserScript== |
2 // @name twitter default following tab | 2 // @name twitter default following tab |
3 // @namespace https://nanaya.net | 3 // @namespace https://nanaya.net |
4 // @match https://mobile.twitter.com/ | 4 // @match https://mobile.twitter.com/* |
5 // @match https://mobile.twitter.com/home | 5 // @match https://twitter.com/* |
6 // @match https://twitter.com/ | |
7 // @match https://twitter.com/home | |
8 // @grant none | 6 // @grant none |
9 // @run-at document-end | 7 // @run-at document-start |
10 // @version 1.0.3 | 8 // @version 1.1.0 |
11 // @author nanaya | 9 // @author nanaya |
12 // @description Always select Following tab on first load | 10 // @description Always select Following tab on first load |
13 // @downloadURL https://hg.nanaya.net/ec-userscripts/raw-file/tip/twitter-default-following-tab.user.js | 11 // @downloadURL https://hg.nanaya.net/ec-userscripts/raw-file/tip/twitter-default-following-tab.user.js |
14 // @updateURL https://hg.nanaya.net/ec-userscripts/raw-file/tip/twitter-default-following-tab.user.js | 12 // @updateURL https://hg.nanaya.net/ec-userscripts/raw-file/tip/twitter-default-following-tab.user.js |
15 // ==/UserScript== | 13 // ==/UserScript== |
16 | 14 |
17 'use strict'; | 15 'use strict'; |
18 | 16 |
19 let selected = false; | 17 let selected = false; |
18 let observing = true; | |
20 | 19 |
21 const observer = new window.MutationObserver(selectFollowingTab); | 20 function isHome () { |
22 let stopObservingTimeout; | 21 return window.location.pathname === '/home'; |
22 } | |
23 | |
24 let resumeObserverTimeout; | |
25 // prevent reselect on browser back/forward navigation | |
26 function pauseObserver () { | |
27 console.debug('pausing observer'); | |
28 observing = false; | |
29 clearTimeout(resumeObserverTimeout); | |
30 resumeObserverTimeout = setTimeout(() => { | |
31 console.debug('resuming observer'); | |
32 observing = true; | |
33 selected = isHome(); | |
34 }, 1000); | |
35 } | |
23 | 36 |
24 function selectFollowingTab () { | 37 function selectFollowingTab () { |
38 if (!observing) return; | |
39 | |
40 if (!isHome()) { | |
41 selected = false; | |
42 return; | |
43 } | |
44 | |
25 if (selected) return; | 45 if (selected) return; |
26 | 46 |
27 const followingTab = document.querySelectorAll('[href="/home"][role=tab]')?.[1]; | 47 const followingTab = document.querySelectorAll('[href="/home"][role=tab]')?.[1]; |
28 if (followingTab == null || followingTab.getAttribute('aria-selected') === 'true') return; | 48 if (followingTab == null) return; |
29 | 49 |
30 console.log("selecting 'Following' tab"); | 50 if (followingTab.getAttribute('aria-selected') === 'true') { |
51 selected = true; | |
52 return; | |
53 } | |
54 | |
55 console.log('selecting "Following" tab'); | |
31 selected = true; | 56 selected = true; |
32 followingTab.click(); | 57 followingTab.click(); |
33 observer.disconnect(); | |
34 clearTimeout(stopObservingTimeout); | |
35 } | 58 } |
36 | 59 |
37 function init () { | 60 new window.MutationObserver(selectFollowingTab).observe(document, { childList: true, subtree: true }); |
38 selectFollowingTab(); | 61 selectFollowingTab(); |
39 if (!selected) { | 62 window.addEventListener('popstate', pauseObserver); |
40 observer.observe(document, { childList: true, subtree: true }); | |
41 } | |
42 stopObservingTimeout = setTimeout(() => { | |
43 console.log("couldn't find 'Following' tab, stop observing page"); | |
44 observer.disconnect(); | |
45 }, 60000); | |
46 } | |
47 | |
48 if (document.readyState === 'loading') { | |
49 document.addEventListener('DOMContentLoaded', init); | |
50 } else { | |
51 init(); | |
52 } |