Created
October 15, 2024 10:42
-
-
Save hylarucoder/846066e18080d9a80ad9f8485b4c0fe6 to your computer and use it in GitHub Desktop.
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
| // 创建一个对象来存储用户信息和一个数组来存储推文 | |
| const usersMap = {}; | |
| let tweets = []; | |
| // 定义一个函数来提取推文信息 | |
| function extractTweets() { | |
| const tweetElems = document.querySelectorAll('article[data-testid="tweet"]'); | |
| tweetElems.forEach(tweetElem => { | |
| const tweetTextElem = tweetElem.querySelector('[data-testid="tweetText"]'); | |
| const tweetText = tweetTextElem ? tweetTextElem.textContent.trim() : ''; | |
| const userInfoElem = tweetElem.querySelector('[data-testid="User-Name"]'); | |
| const userInfo = userInfoElem ? userInfoElem.textContent.trim() : ''; | |
| const username = userInfo.split('@')[1]?.split('·')[0] || ''; | |
| const nick = userInfo.split('@')[0]?.trim() || ''; | |
| const avatarElem = tweetElem.querySelector('img[alt=""]'); | |
| const avatarUrl = avatarElem ? avatarElem.src : ''; | |
| const timeElem = tweetElem.querySelector('time'); | |
| const created_at_human = timeElem ? timeElem.getAttribute('datetime') : ''; | |
| const isRetweet = tweetElem.querySelector('div:has([data-testid="retweet"])') !== null; | |
| // 创建新的推文对象 | |
| const newTweet = { | |
| username, | |
| nick, | |
| avatarUrl, | |
| tweetText, | |
| created_at_human, | |
| isRetweet | |
| }; | |
| // 检查是否已存在相同用户的推文 | |
| const existingTweetIndex = tweets.findIndex(t => t.username === username); | |
| if (existingTweetIndex === -1) { | |
| // 如果用户的推文不存在,直接添加新推文 | |
| tweets.push(newTweet); | |
| } else { | |
| // 如果用户的推文已存在,比较内容 | |
| const existingTweet = tweets[existingTweetIndex]; | |
| if (existingTweet.tweetText !== newTweet.tweetText) { | |
| // 如果内容不同,拼接新内容 | |
| existingTweet.tweetText += `\n${newTweet.tweetText}`; | |
| // 更新其他可能变化的字段 | |
| existingTweet.created_at_human = newTweet.created_at_human; | |
| existingTweet.isRetweet = existingTweet.isRetweet || newTweet.isRetweet; | |
| } | |
| // 如果内容相同,不做任何操作 | |
| } | |
| // 更新 usersMap | |
| if (!usersMap[username]) { | |
| usersMap[username] = [newTweet]; | |
| } else { | |
| const existingUserTweetIndex = usersMap[username].findIndex(t => t.tweetText === newTweet.tweetText); | |
| if (existingUserTweetIndex === -1) { | |
| usersMap[username].push(newTweet); | |
| } else { | |
| // 更新已存在的推文 | |
| usersMap[username][existingUserTweetIndex] = newTweet; | |
| } | |
| } | |
| }); | |
| console.log(`当前共有 ${tweets.length} 条推文`); | |
| } | |
| // 节流函数 | |
| function throttle(func, limit) { | |
| let inThrottle; | |
| return function() { | |
| const args = arguments; | |
| const context = this; | |
| if (!inThrottle) { | |
| func.apply(context, args); | |
| inThrottle = true; | |
| setTimeout(() => inThrottle = false, limit); | |
| } | |
| } | |
| } | |
| // 使用节流的提取函数 | |
| const throttledExtract = throttle(extractTweets, 1000); | |
| // 添加滚动事件监听器 | |
| window.addEventListener('scroll', throttledExtract); | |
| // 停止监听的函数 | |
| function stopListening() { | |
| window.removeEventListener('scroll', throttledExtract); | |
| console.log(`总共提取了 ${tweets.length} 条推文`); | |
| console.log('按用户分组的推文:', usersMap); | |
| console.log('所有推文:', tweets); | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment