Last active
August 8, 2024 09:59
-
-
Save kyohsuke/21c3b0ca532a87a1b3f357dfc94d3f24 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// ==UserScript== | |
// @name Auto Refresh Twitter Timeline | |
// @description Automatically refreshes follow timeline on Twitter. | |
// @match https://twitter.com/* | |
// @match https://x.com/* | |
// @version 1.9.4 | |
// @homepageURL https://gist.github.com/kyohsuke/21c3b0ca532a87a1b3f357dfc94d3f24 | |
// @updateURL https://raw.github.com/gist/kyohsuke/21c3b0ca532a87a1b3f357dfc94d3f24/raw/twrefresh.user.js | |
// @downloadURL https://raw.github.com/gist/kyohsuke/21c3b0ca532a87a1b3f357dfc94d3f24/raw/twrefresh.user.js | |
// @icon https://help.twitter.com/content/dam/help-twitter/brand/logo.png | |
// @grant none | |
// ==/UserScript== | |
setTimeout(function() { | |
'use strict'; | |
let MutationObserver = window.MutationObserver || window.WebKitMutationObserver || window.MozMutationObserver; | |
if (MutationObserver) { | |
console.log('Auto Refresh Twitter Timelineis enabled.'); | |
let observer = new MutationObserver(e => { | |
function isAtTopOfPage() { | |
const maxScrollY = 120; | |
return window.scrollY <= maxScrollY; | |
} | |
function isHomeTimeline() { | |
const tabText = "フォロー中"; | |
const locationURL = 'https://x.com/home'; | |
const selectQuery = 'a[href="/home"][role="tab"][aria-selected="true"]'; | |
return (window.location.href === locationURL) && (document.querySelector(selectQuery)?.innerText === tabText); | |
} | |
function refreshTimeline() { | |
if (isAtTopOfPage() && isHomeTimeline()) { | |
const evaluateStr = '//div[@data-testid="cellInnerDiv"]/div/*[@role="button"]'; | |
let cells = document.evaluate(evaluateStr, document, null, XPathResult.ORDERED_NODE_SNAPSHOT_TYPE, null); | |
let firstCell = (0 < cells.snapshotLength) ? cells.snapshotItem(0) : null; | |
if (firstCell) { | |
firstCell.click(); | |
} | |
} | |
} | |
refreshTimeline(); | |
}); | |
observer.observe(document.body, {childList: true, subtree: true}); | |
} | |
}, 5000); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment