4chan-page-notifier.user.js
· 2.2 KiB · JavaScript
Raw
// ==UserScript==
// @name 4chan Page 10 Notifier
// @namespace moe.rita.4chanPage10Notifier
// @match https://boards.4chan.org/*/thread/*
// @run-at document-end
// @grant none
// @version 1.1.0
// @author Kody
// @description Notify when the thread gets to page 10.
// @icon https://www.google.com/s2/favicons?domain=4chan.org
// @downloadURL https://gist.rita.moe/kody/c3cd33a8d923423c9045ad738ec302ba/raw/HEAD/4chan-page-notifier.user.js
// ==/UserScript==
(function () {
'use strict'
const lastPage = 10
let pageCountEl
let notified = false
const pageNotify = () => {
// Extract the current page number from the text
// but be careful that it might be 10(x)
const currPage = parseInt(pageCountEl.innerText.split('(')[0]?.trim(), 10)
// Check if it's a valid number and greater than 0
if (isNaN(currPage) || currPage < 1) {
console.warn('Invalid page number:', currPage)
return
}
// Reset notification state if page is less than 10
if (currPage < lastPage && notified) {
notified = false
return
}
// Skip if already notified or current page is less than the last page
if (currPage < lastPage || notified) {
return
}
// Send a browser notification
if (window.Notification.permission === 'granted') {
const notification = new window.Notification(
'Last Page Reached', {
body: `The thread has reached page ${currPage}.`,
icon: 'https://www.google.com/s2/favicons?domain=4chan.org',
}
)
notification.onclick = () => {
window.focus()
notification.close()
}
notified = true
}
}
const setupObserver = () => {
pageCountEl = document.getElementById('page-count')
if (pageCountEl) {
const observer = new window.MutationObserver(pageNotify)
observer.observe(
pageCountEl,
{
childList: true,
subtree: true,
characterData: true,
}
)
} else {
console.error('Could not find element with ID "page-count"')
}
}
// Wait 3 seconds before setting up the observer
setTimeout(setupObserver, 3000)
})()
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 |