annotate twitter-default-following-tab.user.js @ 121:f235b50ebdd2

Add twitter default following tab
author nanaya <me@nanaya.net>
date Sat, 21 Jan 2023 01:21:54 +0900
parents
children 34eca18d4cbd
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
121
f235b50ebdd2 Add twitter default following tab
nanaya <me@nanaya.net>
parents:
diff changeset
1 // ==UserScript==
f235b50ebdd2 Add twitter default following tab
nanaya <me@nanaya.net>
parents:
diff changeset
2 // @name twitter default following tab
f235b50ebdd2 Add twitter default following tab
nanaya <me@nanaya.net>
parents:
diff changeset
3 // @namespace https://nanaya.net
f235b50ebdd2 Add twitter default following tab
nanaya <me@nanaya.net>
parents:
diff changeset
4 // @match https://twitter.com/home
f235b50ebdd2 Add twitter default following tab
nanaya <me@nanaya.net>
parents:
diff changeset
5 // @grant none
f235b50ebdd2 Add twitter default following tab
nanaya <me@nanaya.net>
parents:
diff changeset
6 // @version 1.0.0
f235b50ebdd2 Add twitter default following tab
nanaya <me@nanaya.net>
parents:
diff changeset
7 // @author nanaya
f235b50ebdd2 Add twitter default following tab
nanaya <me@nanaya.net>
parents:
diff changeset
8 // @description Always select Following tab on first load
f235b50ebdd2 Add twitter default following tab
nanaya <me@nanaya.net>
parents:
diff changeset
9 // @downloadURL https://hg.nanaya.net/ec-userscripts/raw-file/tip/twitter-default-following-tab.user.js
f235b50ebdd2 Add twitter default following tab
nanaya <me@nanaya.net>
parents:
diff changeset
10 // ==/UserScript==
f235b50ebdd2 Add twitter default following tab
nanaya <me@nanaya.net>
parents:
diff changeset
11
f235b50ebdd2 Add twitter default following tab
nanaya <me@nanaya.net>
parents:
diff changeset
12 'use strict';
f235b50ebdd2 Add twitter default following tab
nanaya <me@nanaya.net>
parents:
diff changeset
13
f235b50ebdd2 Add twitter default following tab
nanaya <me@nanaya.net>
parents:
diff changeset
14 let selected = false;
f235b50ebdd2 Add twitter default following tab
nanaya <me@nanaya.net>
parents:
diff changeset
15
f235b50ebdd2 Add twitter default following tab
nanaya <me@nanaya.net>
parents:
diff changeset
16 const observer = new window.MutationObserver(selectFollowingTab);
f235b50ebdd2 Add twitter default following tab
nanaya <me@nanaya.net>
parents:
diff changeset
17 let stopObservingTimeout;
f235b50ebdd2 Add twitter default following tab
nanaya <me@nanaya.net>
parents:
diff changeset
18
f235b50ebdd2 Add twitter default following tab
nanaya <me@nanaya.net>
parents:
diff changeset
19 function selectFollowingTab () {
f235b50ebdd2 Add twitter default following tab
nanaya <me@nanaya.net>
parents:
diff changeset
20 if (selected) return;
f235b50ebdd2 Add twitter default following tab
nanaya <me@nanaya.net>
parents:
diff changeset
21
f235b50ebdd2 Add twitter default following tab
nanaya <me@nanaya.net>
parents:
diff changeset
22 const followingTab = document.querySelectorAll('[href="/home"][role=tab]')?.[1];
f235b50ebdd2 Add twitter default following tab
nanaya <me@nanaya.net>
parents:
diff changeset
23 if (followingTab == null || followingTab.getAttribute('aria-selected') === 'true') return;
f235b50ebdd2 Add twitter default following tab
nanaya <me@nanaya.net>
parents:
diff changeset
24
f235b50ebdd2 Add twitter default following tab
nanaya <me@nanaya.net>
parents:
diff changeset
25 console.log("selecting 'Following' tab");
f235b50ebdd2 Add twitter default following tab
nanaya <me@nanaya.net>
parents:
diff changeset
26 selected = true;
f235b50ebdd2 Add twitter default following tab
nanaya <me@nanaya.net>
parents:
diff changeset
27 followingTab.click();
f235b50ebdd2 Add twitter default following tab
nanaya <me@nanaya.net>
parents:
diff changeset
28 observer.disconnect();
f235b50ebdd2 Add twitter default following tab
nanaya <me@nanaya.net>
parents:
diff changeset
29 clearTimeout(stopObservingTimeout);
f235b50ebdd2 Add twitter default following tab
nanaya <me@nanaya.net>
parents:
diff changeset
30 }
f235b50ebdd2 Add twitter default following tab
nanaya <me@nanaya.net>
parents:
diff changeset
31
f235b50ebdd2 Add twitter default following tab
nanaya <me@nanaya.net>
parents:
diff changeset
32 function init () {
f235b50ebdd2 Add twitter default following tab
nanaya <me@nanaya.net>
parents:
diff changeset
33 selectFollowingTab();
f235b50ebdd2 Add twitter default following tab
nanaya <me@nanaya.net>
parents:
diff changeset
34 if (!selected) {
f235b50ebdd2 Add twitter default following tab
nanaya <me@nanaya.net>
parents:
diff changeset
35 observer.observe(document, { childList: true, subtree: true });
f235b50ebdd2 Add twitter default following tab
nanaya <me@nanaya.net>
parents:
diff changeset
36 }
f235b50ebdd2 Add twitter default following tab
nanaya <me@nanaya.net>
parents:
diff changeset
37 stopObservingTimeout = setTimeout(() => {
f235b50ebdd2 Add twitter default following tab
nanaya <me@nanaya.net>
parents:
diff changeset
38 console.log("couldn't find 'Following' tab, stop observing page");
f235b50ebdd2 Add twitter default following tab
nanaya <me@nanaya.net>
parents:
diff changeset
39 observer.disconnect();
f235b50ebdd2 Add twitter default following tab
nanaya <me@nanaya.net>
parents:
diff changeset
40 }, 60000);
f235b50ebdd2 Add twitter default following tab
nanaya <me@nanaya.net>
parents:
diff changeset
41 }
f235b50ebdd2 Add twitter default following tab
nanaya <me@nanaya.net>
parents:
diff changeset
42
f235b50ebdd2 Add twitter default following tab
nanaya <me@nanaya.net>
parents:
diff changeset
43 if (document.readyState === 'loading') {
f235b50ebdd2 Add twitter default following tab
nanaya <me@nanaya.net>
parents:
diff changeset
44 document.addEventListener('DOMContentLoaded', init);
f235b50ebdd2 Add twitter default following tab
nanaya <me@nanaya.net>
parents:
diff changeset
45 } else {
f235b50ebdd2 Add twitter default following tab
nanaya <me@nanaya.net>
parents:
diff changeset
46 init();
f235b50ebdd2 Add twitter default following tab
nanaya <me@nanaya.net>
parents:
diff changeset
47 }