Guest User

Untitled

a guest
Apr 18th, 2025
152
0
38 days
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. // ==UserScript==
  2. // @name        8chan (You) Notification Add-on
  3. // @match       https://8chan.se/*/res/*
  4. // @match       https://8chan.cc/*/res/*
  5. // @match       https://8chan.moe/*/res/*
  6. // @version     1.2
  7. // @grant       GM_notification
  8. // ==/UserScript==
  9.  
  10. (async () => {
  11.  
  12.     function main() {
  13.       document.getElementById("requestBtn").remove()
  14.       const board = location.pathname.match(/^\/?(.+\/)/)[1][0]
  15.       const thread = document.getElementById("divThreads");
  16.       const obs = new MutationObserver((mutationList) => {
  17.         for (const mutation of mutationList) {
  18.           for (const node of mutation.addedNodes) {
  19.             if (node.tagName === "DIV" && node.classList.contains("postCell")) {
  20.               checkForYous(node)
  21.             }
  22.           }
  23.         }
  24.       });
  25.       obs.observe(thread, { childList: true, subtree: true });
  26.  
  27.       for (const post of thread.querySelectorAll(".postCell")) {
  28.         checkForYous(post);
  29.       }
  30.  
  31.       function checkForYous(post) {
  32.         var myPosts = JSON.parse(localStorage.getItem(board + "-yous") || "[]")
  33.         var readPosts = JSON.parse(localStorage.getItem("notifReadPosts") || "{}")
  34.         if (!readPosts[board]) readPosts[board] = []
  35.         if (readPosts[board].length > 200) readPosts[board] = readPosts[board].slice(Math.max(readPosts[board].length - 175, 0))
  36.         myPosts.forEach(a=>{
  37.           let quote = ">>" + a
  38.           let postMessage = post.querySelector(".divMessage").textContent
  39.           let postId = post.id
  40.           if (postMessage.includes(quote)) {
  41.             if (!readPosts[board].includes(postId)) {
  42.               let notification = new Notification( "(You)" , { body: postMessage.replace( quote , "") });
  43.               notification.onclick = function () {
  44.                   document.getElementById(postId).scrollIntoView()
  45.               };
  46.               document.addEventListener("visibilitychange", () => {
  47.                 if (document.visibilityState === "visible") {
  48.                   if (notification) notification.close();
  49.                 }
  50.               });
  51.               readPosts[board].push(postId)
  52.               localStorage.setItem("notifReadPosts", JSON.stringify(readPosts))
  53.             }
  54.           }
  55.         })
  56.       }
  57.     }
  58.  
  59.     async function requestNotifPerm(){
  60.       if (Notification.permission != "granted") {
  61.         let perm = await Notification.requestPermission()
  62.         if (perm == "granted") {
  63.           main()
  64.         }
  65.         else if (perm == "denied") {
  66.           document.getElementById("requestBtn").textContent = "Unblock notifications to try again."
  67.         }
  68.       }
  69.       else {
  70.         main()
  71.       }
  72.     }
  73.  
  74.     let requestBtn = document.createElement("button")
  75.     requestBtn.textContent = "Get (You) Notifications"
  76.     requestBtn.id = "requestBtn"
  77.     requestBtn.onclick = requestNotifPerm
  78.     requestBtn.style.position = "fixed"
  79.     requestBtn.style.top = "30px"
  80.     requestBtn.style.right = "30px"
  81.     document.body.appendChild(requestBtn)
  82.  
  83.     requestNotifPerm()
  84.  
  85. })();
  86.  
Advertisement
Add Comment
Please, Sign In to add comment