Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
// ==UserScript==
// @name Twitter search default to live
// @namespace http://tampermonkey.net/
// @version 0.6
// @description Twitter検索のデフォルトタブを「話題のツイート」から「最新」に変更する
// @author kawaz
// @supportURL https://twitter.com/kawaz
// @updateURL https://gist.github.com/kawaz/33e60bd3fbba4e5cc1615d30b97f8ad4/raw/twitter-search-default-to-live.user.js
// @downloadURL https://gist.github.com/kawaz/33e60bd3fbba4e5cc1615d30b97f8ad4/raw/twitter-search-default-to-live.user.js
// @match https://twitter.com/*
// @icon https://www.google.com/s2/favicons?domain=twitter.com
// @noframes
// @grant window.onurlchange
// @grant GM_setValue
// @grant GM_getValue
// ==/UserScript==
// Timeout付きInterval。cbに渡されるstopで繰り返しを停止することも出来る
const setIntervalTimeout = (cb, interval = 200, timeout = 5000) => { const stop = () => { clearInterval(i); clearTimeout(t) }, i = setInterval(cb, interval, stop), t = setTimeout(stop, timeout); cb(stop); return stop }
// 直近が live かどうかの状態管理用(ホントは onurlchange の前のURLを教えてもらえると楽なんだけどな)
const isLive = (url) => (location.pathname === '/search' && new URLSearchParams(location.search).get("f") == "live") || GM_getValue('live')
const setLive = (live) => {
if(setLive.timeoutId) {
clearTimeout(setLive.timeoutId);
}
GM_setValue('live', live);
if(live) {
setLive.timeoutId = setTimeout(()=>GM_setValue('live', false), 5000);
}
}
const handleUrlchange = ({url}) => {
setLive(isLive(url))
if(isLive(url)) {
return;
}
const u = new URL(url)
if(u.pathname === '/search' && (u.searchParams.get("f") === "top" || u.searchParams.get("f") == null)) {
console.log("キーワード検索の最初のタブ(f=top)を話題のツイートから最新(f=live)にします");
setIntervalTimeout(
stop => [...document.querySelectorAll('a[href^="/search?"][href*="&f=live"]')].forEach(a=>{a.click(); stop();}),
50, 3000
);
}
}
window.addEventListener('urlchange', handleUrlchange)
handleUrlchange(location.href)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment