Skip to content

Instantly share code, notes, and snippets.

@kyohsuke
Last active January 14, 2024 09:06
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save kyohsuke/21c3b0ca532a87a1b3f357dfc94d3f24 to your computer and use it in GitHub Desktop.
Save kyohsuke/21c3b0ca532a87a1b3f357dfc94d3f24 to your computer and use it in GitHub Desktop.
// ==UserScript==
// @name Auto Refresh Twitter Timeline
// @description Automatically refreshes follow timeline on Twitter.
// @match https://twitter.com/home
// @version 1.8
// @updateURL https://gist.githubusercontent.com/kyohsuke/21c3b0ca532a87a1b3f357dfc94d3f24/raw/twrefresh.user.js
// @downloadURL https://gist.githubusercontent.com/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://twitter.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/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