Created
March 30, 2022 12:42
-
-
Save Kenya-West/d366a373ee89c2977ad070dd37219c01 to your computer and use it in GitHub Desktop.
Find messages with links pattern on VK IM page
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 config = { | |
| filterBySender: "Вы", | |
| regex: /coub\.com\/view\/[a-z0-9]+/, | |
| timeToLoad: 750, | |
| }; | |
| const dialogsRoot = document.querySelector("#im_dialogs"); | |
| const dialogScrollableArea = document.querySelector( | |
| "#im_dialogs .ui_scroll_inner.tt_noappend" | |
| ); | |
| let result = new Set(); | |
| const url = new URL(window.location.href); | |
| if (url.searchParams.get("q")) { | |
| console.log("User is on right location. Staring script..."); | |
| let addedMessagesCounter = 1; | |
| const timer = setInterval(() => { | |
| addedMessagesCounter = 0; | |
| console.log("There is new messages, start to search..."); | |
| const messages = document.querySelectorAll("#im_dialogs li.nim-dialog"); | |
| console.log(`Found ${messages.length} messages, proceed to process...`); | |
| messages.forEach((message) => { | |
| const sender = message.querySelector( | |
| ".nim-dialog--content .nim-dialog--text-preview .nim-dialog--who" | |
| )?.innerText; | |
| const text = message.querySelector( | |
| ".nim-dialog--content .nim-dialog--text-preview .nim-dialog--inner-text" | |
| )?.innerText; | |
| if (sender?.includes(config.filterBySender) & config.regex.test(text)) { | |
| const foundString = text.match(config.regex); | |
| const resultBeforeSize = result.size; | |
| const resultCopy = result.add(foundString[0]); | |
| if (resultBeforeSize !== resultCopy.size) { | |
| addedMessagesCounter++; | |
| } | |
| } | |
| }); | |
| console.log( | |
| `Added ${addedMessagesCounter} messages, now scroll to load new messages...` | |
| ); | |
| if (addedMessagesCounter === 0) { | |
| console.info(`Success! Got ${result.size} messages`); | |
| console.dir(result); | |
| clearInterval(timer); | |
| } | |
| dialogScrollableArea.scrollIntoView(false); | |
| dialogScrollableArea.scrollIntoView(false); | |
| }, config.timeToLoad ?? 1000); | |
| } else { | |
| alert("You are not on required location! Script aborting..."); | |
| if (result.size > 0) { | |
| console.info(`Despite fail, still got ${result.size} messages`); | |
| console.dir(result); | |
| } | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment