annotate twitter-default-following-tab.user.js @ 123:87442313317d

Apparently tampermonkey requires run-at ?_?
author nanaya <me@nanaya.net>
date Sat, 21 Jan 2023 02:00:59 +0900
parents 34eca18d4cbd
children 6d280338e113
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
87442313317d Apparently tampermonkey requires run-at ?_?
nanaya <me@nanaya.net>
parents: 122
diff changeset
8 // @version 1.0.1
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
f235b50ebdd2 Add twitter default following tab
nanaya <me@nanaya.net>
parents:
diff changeset
12 // ==/UserScript==
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 'use strict';
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 let selected = false;
f235b50ebdd2 Add twitter default following tab
nanaya <me@nanaya.net>
parents:
diff changeset
17
f235b50ebdd2 Add twitter default following tab
nanaya <me@nanaya.net>
parents:
diff changeset
18 const observer = new window.MutationObserver(selectFollowingTab);
f235b50ebdd2 Add twitter default following tab
nanaya <me@nanaya.net>
parents:
diff changeset
19 let stopObservingTimeout;
f235b50ebdd2 Add twitter default following tab
nanaya <me@nanaya.net>
parents:
diff changeset
20
f235b50ebdd2 Add twitter default following tab
nanaya <me@nanaya.net>
parents:
diff changeset
21 function selectFollowingTab () {
f235b50ebdd2 Add twitter default following tab
nanaya <me@nanaya.net>
parents:
diff changeset
22 if (selected) return;
f235b50ebdd2 Add twitter default following tab
nanaya <me@nanaya.net>
parents:
diff changeset
23
f235b50ebdd2 Add twitter default following tab
nanaya <me@nanaya.net>
parents:
diff changeset
24 const followingTab = document.querySelectorAll('[href="/home"][role=tab]')?.[1];
f235b50ebdd2 Add twitter default following tab
nanaya <me@nanaya.net>
parents:
diff changeset
25 if (followingTab == null || followingTab.getAttribute('aria-selected') === 'true') return;
f235b50ebdd2 Add twitter default following tab
nanaya <me@nanaya.net>
parents:
diff changeset
26
f235b50ebdd2 Add twitter default following tab
nanaya <me@nanaya.net>
parents:
diff changeset
27 console.log("selecting 'Following' tab");
f235b50ebdd2 Add twitter default following tab
nanaya <me@nanaya.net>
parents:
diff changeset
28 selected = true;
f235b50ebdd2 Add twitter default following tab
nanaya <me@nanaya.net>
parents:
diff changeset
29 followingTab.click();
f235b50ebdd2 Add twitter default following tab
nanaya <me@nanaya.net>
parents:
diff changeset
30 observer.disconnect();
f235b50ebdd2 Add twitter default following tab
nanaya <me@nanaya.net>
parents:
diff changeset
31 clearTimeout(stopObservingTimeout);
f235b50ebdd2 Add twitter default following tab
nanaya <me@nanaya.net>
parents:
diff changeset
32 }
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 function init () {
f235b50ebdd2 Add twitter default following tab
nanaya <me@nanaya.net>
parents:
diff changeset
35 selectFollowingTab();
f235b50ebdd2 Add twitter default following tab
nanaya <me@nanaya.net>
parents:
diff changeset
36 if (!selected) {
f235b50ebdd2 Add twitter default following tab
nanaya <me@nanaya.net>
parents:
diff changeset
37 observer.observe(document, { childList: true, subtree: true });
f235b50ebdd2 Add twitter default following tab
nanaya <me@nanaya.net>
parents:
diff changeset
38 }
f235b50ebdd2 Add twitter default following tab
nanaya <me@nanaya.net>
parents:
diff changeset
39 stopObservingTimeout = setTimeout(() => {
f235b50ebdd2 Add twitter default following tab
nanaya <me@nanaya.net>
parents:
diff changeset
40 console.log("couldn't find 'Following' tab, stop observing page");
f235b50ebdd2 Add twitter default following tab
nanaya <me@nanaya.net>
parents:
diff changeset
41 observer.disconnect();
f235b50ebdd2 Add twitter default following tab
nanaya <me@nanaya.net>
parents:
diff changeset
42 }, 60000);
f235b50ebdd2 Add twitter default following tab
nanaya <me@nanaya.net>
parents:
diff changeset
43 }
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 if (document.readyState === 'loading') {
f235b50ebdd2 Add twitter default following tab
nanaya <me@nanaya.net>
parents:
diff changeset
46 document.addEventListener('DOMContentLoaded', init);
f235b50ebdd2 Add twitter default following tab
nanaya <me@nanaya.net>
parents:
diff changeset
47 } else {
f235b50ebdd2 Add twitter default following tab
nanaya <me@nanaya.net>
parents:
diff changeset
48 init();
f235b50ebdd2 Add twitter default following tab
nanaya <me@nanaya.net>
parents:
diff changeset
49 }