Last active
March 18, 2022 03:03
-
-
Save ngseke/9269892b392bc515a4ca81bcc290a947 to your computer and use it in GitHub Desktop.
計算外國 YouTuber 底下留言台灣感謝率
This file contains 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
function calc () { | |
const patterns = [ | |
{ | |
regex: /(謝謝|感謝)/, | |
description: '普通感謝', | |
}, | |
{ | |
regex: /謝(.*?)(愛|喜)(.*?)灣/, | |
description: '精準感謝', | |
}, | |
] | |
const $allElements = [...document.querySelectorAll('ytd-comment-renderer')] | |
const result = patterns.map(({ regex, description }) => { | |
const $matchedElements = $allElements.filter(i => { | |
return regex.test(i.innerText) | |
}) | |
const matchedComments = $matchedElements.map($el => { | |
return $el.querySelector('ytd-expander').innerText | |
}) | |
const rate = $matchedElements.length / $allElements.length | |
const rateText = `${(rate * 100).toFixed(2)} % (${$matchedElements.length}則 / ${$allElements.length}則)` | |
return { regex, description, rateText, matchedComments } | |
}) | |
console.table( | |
result.map(({ regex, description, rateText }) => ({ | |
regex, | |
description, | |
rateText, | |
})) | |
) | |
window.result = result | |
} | |
function scrollToBottom () { | |
window.scrollTo({ | |
top: document.querySelector('ytd-app').scrollHeight, | |
}) | |
} | |
function toggleRunTask () { | |
const key = 'timerIds' | |
if (window[key]) { | |
window[key].forEach(clearInterval) | |
delete window[key] | |
console.log('🛑 暫停統計') | |
} else { | |
window[key] = [ | |
setInterval(calc, 3000), | |
setInterval(scrollToBottom, 250), | |
] | |
console.log(`🟢 開始統計\n(執行 \`${arguments.callee.name}()\` 來停止)`) | |
} | |
} | |
toggleRunTask() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment