Last active 1753457066

Revision 66a7912b6074eee628e5ec54641520c670d4af48

4chan-page-notifier.user.js Raw
1// ==UserScript==
2// @name 4chan Page 10 Notifier
3// @namespace moe.rita.4chanPage10Notifier
4// @match https://boards.4chan.org/*/thread/*
5// @run-at document-end
6// @grant none
7// @version 1.1.0
8// @author Kody
9// @description Notify when the thread gets to page 10.
10// @icon https://www.google.com/s2/favicons?domain=4chan.org
11// @downloadURL https://gist.rita.moe/kody/c3cd33a8d923423c9045ad738ec302ba/raw/HEAD/4chan-page-notifier.user.js
12// ==/UserScript==
13
14(function () {
15 'use strict'
16
17 const lastPage = 10
18 let pageCountEl
19 let notified = false
20
21 const pageNotify = () => {
22 // Extract the current page number from the text
23 // but be careful that it might be 10(x)
24 const currPage = parseInt(pageCountEl.innerText.split('(')[0]?.trim(), 10)
25
26 // Check if it's a valid number and greater than 0
27 if (isNaN(currPage) || currPage < 1) {
28 console.warn('Invalid page number:', currPage)
29 return
30 }
31
32 // Reset notification state if page is less than 10
33 if (currPage < lastPage && notified) {
34 notified = false
35 return
36 }
37
38 // Skip if already notified or current page is less than the last page
39 if (currPage < lastPage || notified) {
40 return
41 }
42
43 // Send a browser notification
44 if (window.Notification.permission === 'granted') {
45 const notification = new window.Notification(
46 'Last Page Reached', {
47 body: `The thread has reached page ${currPage}.`,
48 icon: 'https://www.google.com/s2/favicons?domain=4chan.org',
49 }
50 )
51 notification.onclick = () => {
52 window.focus()
53 notification.close()
54 }
55
56 notified = true
57 }
58 }
59
60 const setupObserver = () => {
61 pageCountEl = document.getElementById('page-count')
62
63 if (pageCountEl) {
64 const observer = new window.MutationObserver(pageNotify)
65 observer.observe(
66 pageCountEl,
67 {
68 childList: true,
69 subtree: true,
70 characterData: true,
71 }
72 )
73 } else {
74 console.error('Could not find element with ID "page-count"')
75 }
76 }
77
78 // Wait 3 seconds before setting up the observer
79 setTimeout(setupObserver, 3000)
80})()
81