annotate twitter-default-following-tab.user.js @ 125:31b1ab00cc7a

Not sure how tampermonkey even work
author nanaya <me@nanaya.net>
date Sat, 21 Jan 2023 02:15:02 +0900
parents 6d280338e113
children bf1f7164fd4f
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
122
34eca18d4cbd Also apply to mobile subdomain
nanaya <me@nanaya.net>
parents: 121
diff changeset
5 // @match https://mobile.twitter.com/home
121
f235b50ebdd2 Add twitter default following tab
nanaya <me@nanaya.net>
parents:
diff changeset
6 // @grant none
123
87442313317d Apparently tampermonkey requires run-at ?_?
nanaya <me@nanaya.net>
parents: 122
diff changeset
7 // @run-at document-end
125
31b1ab00cc7a Not sure how tampermonkey even work
nanaya <me@nanaya.net>
parents: 124
diff changeset
8 // @version 1.0.2
121
f235b50ebdd2 Add twitter default following tab
nanaya <me@nanaya.net>
parents:
diff changeset
9 // @author nanaya
f235b50ebdd2 Add twitter default following tab
nanaya <me@nanaya.net>
parents:
diff changeset
10 // @description Always select Following tab on first load
f235b50ebdd2 Add twitter default following tab
nanaya <me@nanaya.net>
parents:
diff changeset
11 // @downloadURL https://hg.nanaya.net/ec-userscripts/raw-file/tip/twitter-default-following-tab.user.js
124
6d280338e113 Apparently both are required? or recommended
nanaya <me@nanaya.net>
parents: 123
diff changeset
12 // @updateURL https://hg.nanaya.net/ec-userscripts/raw-file/tip/twitter-default-following-tab.user.js
121
f235b50ebdd2 Add twitter default following tab
nanaya <me@nanaya.net>
parents:
diff changeset
13 // ==/UserScript==
f235b50ebdd2 Add twitter default following tab
nanaya <me@nanaya.net>
parents:
diff changeset
14
f235b50ebdd2 Add twitter default following tab
nanaya <me@nanaya.net>
parents:
diff changeset
15 'use strict';
f235b50ebdd2 Add twitter default following tab
nanaya <me@nanaya.net>
parents:
diff changeset
16
f235b50ebdd2 Add twitter default following tab
nanaya <me@nanaya.net>
parents:
diff changeset
17 let selected = false;
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 const observer = new window.MutationObserver(selectFollowingTab);
f235b50ebdd2 Add twitter default following tab
nanaya <me@nanaya.net>
parents:
diff changeset
20 let stopObservingTimeout;
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 function selectFollowingTab () {
f235b50ebdd2 Add twitter default following tab
nanaya <me@nanaya.net>
parents:
diff changeset
23 if (selected) 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 const followingTab = document.querySelectorAll('[href="/home"][role=tab]')?.[1];
f235b50ebdd2 Add twitter default following tab
nanaya <me@nanaya.net>
parents:
diff changeset
26 if (followingTab == null || followingTab.getAttribute('aria-selected') === 'true') return;
f235b50ebdd2 Add twitter default following tab
nanaya <me@nanaya.net>
parents:
diff changeset
27
f235b50ebdd2 Add twitter default following tab
nanaya <me@nanaya.net>
parents:
diff changeset
28 console.log("selecting 'Following' tab");
f235b50ebdd2 Add twitter default following tab
nanaya <me@nanaya.net>
parents:
diff changeset
29 selected = true;
f235b50ebdd2 Add twitter default following tab
nanaya <me@nanaya.net>
parents:
diff changeset
30 followingTab.click();
f235b50ebdd2 Add twitter default following tab
nanaya <me@nanaya.net>
parents:
diff changeset
31 observer.disconnect();
f235b50ebdd2 Add twitter default following tab
nanaya <me@nanaya.net>
parents:
diff changeset
32 clearTimeout(stopObservingTimeout);
f235b50ebdd2 Add twitter default following tab
nanaya <me@nanaya.net>
parents:
diff changeset
33 }
f235b50ebdd2 Add twitter default following tab
nanaya <me@nanaya.net>
parents:
diff changeset
34
f235b50ebdd2 Add twitter default following tab
nanaya <me@nanaya.net>
parents:
diff changeset
35 function init () {
f235b50ebdd2 Add twitter default following tab
nanaya <me@nanaya.net>
parents:
diff changeset
36 selectFollowingTab();
f235b50ebdd2 Add twitter default following tab
nanaya <me@nanaya.net>
parents:
diff changeset
37 if (!selected) {
f235b50ebdd2 Add twitter default following tab
nanaya <me@nanaya.net>
parents:
diff changeset
38 observer.observe(document, { childList: true, subtree: true });
f235b50ebdd2 Add twitter default following tab
nanaya <me@nanaya.net>
parents:
diff changeset
39 }
f235b50ebdd2 Add twitter default following tab
nanaya <me@nanaya.net>
parents:
diff changeset
40 stopObservingTimeout = setTimeout(() => {
f235b50ebdd2 Add twitter default following tab
nanaya <me@nanaya.net>
parents:
diff changeset
41 console.log("couldn't find 'Following' tab, stop observing page");
f235b50ebdd2 Add twitter default following tab
nanaya <me@nanaya.net>
parents:
diff changeset
42 observer.disconnect();
f235b50ebdd2 Add twitter default following tab
nanaya <me@nanaya.net>
parents:
diff changeset
43 }, 60000);
f235b50ebdd2 Add twitter default following tab
nanaya <me@nanaya.net>
parents:
diff changeset
44 }
f235b50ebdd2 Add twitter default following tab
nanaya <me@nanaya.net>
parents:
diff changeset
45
f235b50ebdd2 Add twitter default following tab
nanaya <me@nanaya.net>
parents:
diff changeset
46 if (document.readyState === 'loading') {
f235b50ebdd2 Add twitter default following tab
nanaya <me@nanaya.net>
parents:
diff changeset
47 document.addEventListener('DOMContentLoaded', init);
f235b50ebdd2 Add twitter default following tab
nanaya <me@nanaya.net>
parents:
diff changeset
48 } else {
f235b50ebdd2 Add twitter default following tab
nanaya <me@nanaya.net>
parents:
diff changeset
49 init();
f235b50ebdd2 Add twitter default following tab
nanaya <me@nanaya.net>
parents:
diff changeset
50 }