4chan-page-notifier.user.js
· 2.3 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.2.2
// @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'
let pageCountEl
const lastPage = 10
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)
console.log('Current page:', currPage)
// 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
const subject = document.querySelector('.subject')?.innerText || 'Last Page Reached'
if (window.Notification.permission === 'granted') {
const notification = new window.Notification(
subject,
{
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.2.2 |
| 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 | let pageCountEl |
| 18 | const lastPage = 10 |
| 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 | console.log('Current page:', currPage) |
| 26 | |
| 27 | // Check if it's a valid number and greater than 0 |
| 28 | if (isNaN(currPage) || currPage < 1) { |
| 29 | console.warn('Invalid page number:', currPage) |
| 30 | return |
| 31 | } |
| 32 | |
| 33 | // Reset notification state if page is less than 10 |
| 34 | if (currPage < lastPage && notified) { |
| 35 | notified = false |
| 36 | return |
| 37 | } |
| 38 | |
| 39 | // Skip if already notified or current page is less than the last page |
| 40 | if (currPage < lastPage || notified) { |
| 41 | return |
| 42 | } |
| 43 | |
| 44 | // Send a browser notification |
| 45 | const subject = document.querySelector('.subject')?.innerText || 'Last Page Reached' |
| 46 | if (window.Notification.permission === 'granted') { |
| 47 | const notification = new window.Notification( |
| 48 | subject, |
| 49 | { |
| 50 | body: `The thread has reached page ${currPage}.`, |
| 51 | icon: 'https://www.google.com/s2/favicons?domain=4chan.org', |
| 52 | } |
| 53 | ) |
| 54 | notification.onclick = () => { |
| 55 | window.focus() |
| 56 | notification.close() |
| 57 | } |
| 58 | |
| 59 | notified = true |
| 60 | } |
| 61 | } |
| 62 | |
| 63 | const setupObserver = () => { |
| 64 | pageCountEl = document.getElementById('page-count') |
| 65 | |
| 66 | if (pageCountEl) { |
| 67 | const observer = new window.MutationObserver(pageNotify) |
| 68 | observer.observe( |
| 69 | pageCountEl, |
| 70 | { |
| 71 | childList: true, |
| 72 | subtree: true, |
| 73 | characterData: true, |
| 74 | } |
| 75 | ) |
| 76 | } else { |
| 77 | console.error('Could not find element with ID "page-count"') |
| 78 | } |
| 79 | } |
| 80 | |
| 81 | // Wait 3 seconds before setting up the observer |
| 82 | setTimeout(setupObserver, 3000) |
| 83 | })() |
| 84 |