Created
January 3, 2024 12:06
-
-
Save mouseos/96d5a06f07f5ffc0cbdd33a1a6b1b848 to your computer and use it in GitHub Desktop.
chrome拡張でMutationObserver使ってリアルタイムにconsoleにツイート表示するコード
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
// Mutation Observerの設定 | |
const targetNode = document.body; // 監視対象の要素 | |
const config = { childList: true, subtree: true }; | |
// 新しいtweet要素が追加された時に行う処理 | |
const tweetObserverCallback = function(mutationsList, observer) { | |
for(const mutation of mutationsList) { | |
if (mutation.type === 'childList') { | |
// [data-testid="tweet"]要素が追加された場合に実行する処理 | |
const newTweets = document.querySelectorAll('[data-testid="tweet"]'); | |
// ここで新しいtweet要素に対する処理を行います | |
newTweets.forEach(tweet => { | |
console.log(tweet.textContent); | |
}); | |
} | |
} | |
}; | |
// Mutation Observerの作成 | |
const tweetObserver = new MutationObserver(tweetObserverCallback); | |
// 監視を開始する | |
tweetObserver.observe(targetNode, config); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment