Last active
June 14, 2021 11:22
-
-
Save silver-mixer/9c7a49dfbf018ce3f1566be6ff8f04c4 to your computer and use it in GitHub Desktop.
[UserScript] Display RT time in tweet
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 RT Time Display | |
// @version 6 | |
// @description RTされた時間を表示します。 | |
// @author silver-mixer | |
// @match https://twitter.com/* | |
// @grant none | |
// @run-at document-start | |
// ==/UserScript== | |
const NAME = "RT Time Display"; | |
const VERSION = 6; | |
try{ | |
let rtdata = {}; | |
let oXHROpen = XMLHttpRequest.prototype.open; | |
let oXHRSend = XMLHttpRequest.prototype.send; | |
XMLHttpRequest.prototype.open = function(){ | |
this.openMethod = arguments[0]; | |
this.openURL = arguments[1]; | |
oXHROpen.apply(this, arguments); | |
}; | |
XMLHttpRequest.prototype.send = function(){ | |
let isTL = /\/[a-zA-Z0-9_]+\.json/.test(this.openURL) && this.openURL.indexOf('/timeline/') !== -1; | |
let isUTL = /graphql\/[a-zA-Z0-9-_]+\/UserTweets/.test(this.openURL); | |
if(isTL || isUTL){ | |
console.log(isTL, isUTL, this.openURL); | |
this.addEventListener('load', () => { | |
if(this.status === 200){ | |
let data = JSON.parse(this.responseText); | |
if(isTL){ | |
let tweets = data.globalObjects.tweets; | |
Object.keys(tweets).forEach(tid => { | |
if('retweeted_status_id_str' in tweets[tid]){ | |
let date = new Date(Number((BigInt(tweets[tid].id_str) >> 22n) + 1288834974657n)); | |
let dateStr = date.getFullYear() + '年' + | |
(date.getMonth() + 1) + '月' + | |
date.getDate() + '日' + | |
('0' + date.getHours()).slice(-2) + ':' + | |
('0' + date.getMinutes()).slice(-2) + ':' + | |
('0' + date.getSeconds()).slice(-2) + '.' + | |
('00' + date.getMilliseconds()).slice(-3); | |
rtdata[tweets[tid].retweeted_status_id_str] = dateStr; | |
} | |
}); | |
}else if(isUTL){ | |
let tweets = data.data.user.result.timeline.timeline.instructions[0].entries; | |
Object.keys(tweets).forEach(i => { | |
if(tweets[i].content.entryType === 'TimelineTimelineItem' && 'retweeted_status_result' in tweets[i].content.itemContent.tweet_results.result.legacy){ | |
console.log(tweets[i]); | |
let rtid = tweets[i].sortIndex; | |
let tid = tweets[i].content.itemContent.tweet_results.result.legacy.retweeted_status_result.result.rest_id; | |
let date = new Date(Number((BigInt(rtid) >> 22n) + 1288834974657n)); | |
let dateStr = date.getFullYear() + '年' + | |
(date.getMonth() + 1) + '月' + | |
date.getDate() + '日' + | |
('0' + date.getHours()).slice(-2) + ':' + | |
('0' + date.getMinutes()).slice(-2) + ':' + | |
('0' + date.getSeconds()).slice(-2) + '.' + | |
('00' + date.getMilliseconds()).slice(-3); | |
rtdata[tid] = dateStr; | |
} | |
}); | |
} | |
} | |
}); | |
} | |
oXHRSend.apply(this, arguments); | |
}; | |
new MutationObserver(mutations => { | |
mutations.forEach(mutation => { | |
let tl = mutation.target.parentNode; | |
if(tl && tl.hasAttribute('aria-label') && tl.getAttribute('aria-label').indexOf('タイムライン') === 0){ | |
mutation.addedNodes.forEach(node => { | |
let timeElem = node.querySelector('time'); | |
if(timeElem !== null && timeElem.parentNode !== null && timeElem.parentNode.tagName === 'A'){ | |
let id = timeElem.parentNode.href.match(/\/([0-9]+)($|\?)/)[1]; | |
if(id in rtdata){ | |
let tweetElem = timeElem.parentNode; | |
while(tweetElem !== null && tweetElem.tagName !== 'HTML'){ | |
if(tweetElem.tagName === 'ARTICLE')break; | |
tweetElem = tweetElem.parentNode; | |
} | |
let aLabelElem = tweetElem.querySelector('a'); | |
if(aLabelElem.href.match(/twitter\.com\/[a-zA-Z0-9_]+$/)){ | |
aLabelElem.appendChild(document.createTextNode('Retweeted at ' + rtdata[id])); | |
} | |
} | |
} | |
}); | |
} | |
}); | |
}).observe(document.body, { | |
childList: true, | |
subtree: true | |
}); | |
}catch(e){ | |
console.error(`Error at "${NAME}" v${VERSION}\n`, e); | |
}} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
RT Time Display
簡易バージョン履歴
v6 ユーザTLで動作しなくなったバグを修正
v5 ユーザTLで動作しなくなったバグを修正
v4 新Twitter用に修正/Tampermonkey向けにヘッダコメントを付与/公開
v3 バグ修正(非公開)
v2 バグ修正(非公開)
v1 旧Twitter用に作成(非公開)