Always Latest Tweets for Mobile Twitter

Auto-switch to latest tweets for mobile version Twitter

您需要先安裝使用者腳本管理器擴展,如 TampermonkeyGreasemonkeyViolentmonkey 之後才能安裝該腳本。

您需要先安裝使用者腳本管理器擴充功能,如 TampermonkeyViolentmonkey 後才能安裝該腳本。

您需要先安裝使用者腳本管理器擴充功能,如 TampermonkeyViolentmonkey 後才能安裝該腳本。

您需要先安裝使用者腳本管理器擴充功能,如 TampermonkeyUserscripts 後才能安裝該腳本。

你需要先安裝一款使用者腳本管理器擴展,比如 Tampermonkey,才能安裝此腳本

您需要先安裝使用者腳本管理器擴充功能後才能安裝該腳本。

(我已經安裝了使用者腳本管理器,讓我安裝!)

你需要先安裝一款使用者樣式管理器擴展,比如 Stylus,才能安裝此樣式

你需要先安裝一款使用者樣式管理器擴展,比如 Stylus,才能安裝此樣式

你需要先安裝一款使用者樣式管理器擴展,比如 Stylus,才能安裝此樣式

你需要先安裝一款使用者樣式管理器擴展後才能安裝此樣式

你需要先安裝一款使用者樣式管理器擴展後才能安裝此樣式

你需要先安裝一款使用者樣式管理器擴展後才能安裝此樣式

(我已經安裝了使用者樣式管理器,讓我安裝!)

// ==UserScript==
// @name         Always Latest Tweets for Mobile Twitter
// @namespace    https://github.com/gslin/always-latest-tweets-for-mobile-twitter
// @version      0.20190710.0
// @description  Auto-switch to latest tweets for mobile version Twitter
// @author       Gea-Suan Lin <[email protected]>
// @match        https://mobile.twitter.com/*
// @grant        none
// @run-at       document-start
// @license      MIT
// ==/UserScript==

(function() {
    'use strict';

    let ob = new window.MutationObserver(ml => {
        // If it's already in latest timeline, uninstall observer.
        if (document.querySelector('div[aria-label="Timeline: Your Home Timeline"]')) {
            console.debug('Already on latest timeline');
            ob.disconnect();
        }

        ml.forEach(el => {
            if (document.evaluate('.//span[text() = "See latest Tweets instead"]', el.target, null, XPathResult.ANY_TYPE, null).iterateNext()) {
                for (let span of el.target.getElementsByTagName('span')) {
                    if ('See latest Tweets instead' == span.innerText) {
                        span.click();
                        return;
                    }
                }
            }
        });

        let star_el = document.querySelector('main div[data-testid="primaryColumn"] h2');
        if (star_el && 'Home' === star_el.innerText) {
            let el = document.querySelector('div[aria-label="Top Tweets on"]');
            if (el) {
                el.click();
                return;
            }
        }
    });

    ob.observe(document, {
        childList: true,
        subtree: true,
    });
})();