Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- // Removes non-English chat messages and superchat in YouTue live chat
- // Modified and improved version of https://pastebin.com/XpiKsr8T (it didn't support superchats and removed number-only messages)
- // v9002
- // - Fixed messages not appearing after some time
- // - Fixed chat scrolling
- // - Removed printing of filtered messages as otherwise they would be duplicated over and over
- javascript:(() => {
- let ALLOWED_MESSAGES = [""]; /*In lower case*/
- let FILTER_THRESHOLD = 85; /*How much % of the message should have english letters to not be filtered*/
- let func = () => {
- let chat_list = window.top.document.getElementById("chatframe")
- .contentDocument.querySelector("#items.style-scope.yt-live-chat-item-list-renderer");
- let observer = new MutationObserver((mutationsList, observer) => {
- let chat_items = chat_list.children;
- for(let i = 0; i < chat_items.length; i++) {
- let msgNode = chat_items[i].querySelector("#content #message");
- if(!msgNode)
- continue;
- let msg = "";
- for(let i = 0; i < msgNode.childNodes.length; i++) {
- let node = msgNode.childNodes[i];
- if(node.nodeType === Node.TEXT_NODE) {
- msg += node.nodeValue.trim();
- }
- }
- if(!msg || ALLOWED_MESSAGES.includes(msg.toLowerCase()))
- continue;
- let original = msg;
- msg = msg.replace(/[\^\(\)\[\]w\s.!?,\ufe0e]/g, "");
- if(msg.length === 0)
- continue;
- let full_len = msg.length;
- let en_only_array = msg.match(/[a-z\d]+/gi);
- let en_len = en_only_array ? en_only_array.reduce((a, b) => a + b.length, 0) : 0;
- let percentage = en_len / full_len * 100;
- if(percentage < FILTER_THRESHOLD) {
- chat_items[i].style.display = "none";
- } else {
- chat_items[i].style.display = "";
- let scroller = window.top.document.getElementById("chatframe").contentDocument.querySelector("#item-scroller");
- scroller.scrollTop = scroller.scrollHeight;
- }
- }
- });
- observer.observe(chat_list, { attributes: false, childList: true, subtree: false });
- };
- if(window.top.document.readyState === "complete") {
- let chat_frame = window.top.document.getElementById("chatframe");
- let chat_doc = chat_frame.contentDocument;
- let chat_list = chat_doc.querySelector("#items.style-scope.yt-live-chat-item-list-renderer");
- if(chat_doc && chat_doc.readyState === "complete" && chat_list) {
- console.log("All loaded already");
- func();
- } else {
- console.log("Waiting for chat to load");
- chat_frame.contentWindow.addEventListener("load", func);
- }
- } else {
- console.log("Waiting for top to load");
- window.top.addEventListener("load", func);
- }
- })();
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement