Skip to content

Instantly share code, notes, and snippets.

@silver-mixer
Last active January 3, 2026 14:52
Show Gist options
  • Select an option

  • Save silver-mixer/ba6f5fb1e1eda9822988a56e2c9dceca to your computer and use it in GitHub Desktop.

Select an option

Save silver-mixer/ba6f5fb1e1eda9822988a56e2c9dceca to your computer and use it in GitHub Desktop.
[UserScript] Extract and display user unique id in twitter
// ==UserScript==
// @name Twitter User ID Extractor
// @namespace https://estar-lab.net/
// @version 5.1
// @description ユーザの固有IDを抽出します。
// @author silver-mixer
// @match https://x.com/*
// @icon https://www.google.com/s2/favicons?sz=64&domain=twitter.com
// @grant none
// ==/UserScript==
(() => {
'use strict';
const userMap = new Map(); // <screen_name:string, userId:string>
const updateUserIDElement = () => {
const screenName = location.href.match(/x\.com\/([a-zA-Z0-9_]+)/)[1] ?? null;
const userId = userMap.get(screenName) ?? null;
if(screenName === null || userId === null){
console.log('failed to get screen_name or userId', screenName, userId);
return;
}
const linkTag = document.getElementById('_tuie_link');
if(linkTag !== null){
console.log('update link');
linkTag.href = `https://x.com/intent/user?user_id=${userId}`;
linkTag.textContent = `@${screenName} (${userId})`;
}else{
console.log('create link');
const infoPane = document.querySelectorAll('.css-175oi2r.r-1adg3ll.r-6gpygo')[2];
infoPane.insertAdjacentHTML('beforeend', `<div><a id="_tuie_link" style="color: rgb(139, 152, 165);" href="https://x.com/intent/user?user_id=${userId}">@${screenName} (${userId})</a></div>`);
}
};
// Extract UserID from UserByScreenName
const oXHROpen = XMLHttpRequest.prototype.open;
XMLHttpRequest.prototype.open = function(){
const openURL = arguments[1];
if(/\/UserByScreenName/.test(openURL)){
this.addEventListener('load', () => {
const data = JSON.parse(this.responseText);
const screenName = data?.data?.user?.result?.core?.screen_name;
const userId = data?.data?.user?.result?.rest_id;
if(screenName !== null && userId !== null){
userMap.set(screenName, userId);
updateUserIDElement();
}
});
}
oXHROpen.apply(this, arguments);
};
const oPushState = history.pushState.bind(history);
history.pushState = function(){
oPushState(...arguments);
setTimeout(() => {
updateUserIDElement();
}, 500);
};
})();
@silver-mixer
Copy link
Author

silver-mixer commented Sep 4, 2020

Twitter User ID Extractor

簡易バージョン履歴

バージョン 修正日 変更内容
v5.1 2026/01/03 ページ遷移時にユーザID表示が消えるバグを修正
v5.0 2025/12/31 Twitter(現X)向けに処理を一新。Tampermonkeyでの動作を前提とした形に修正。
v4 Tampermonkey向けにヘッダコメントを修正
v3 動作しないバグを修正
v2 現Twitter用に再作成
v1 旧Twitter用に作成(非公開)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment