Skip to content

Instantly share code, notes, and snippets.

@relthyg
Created June 22, 2026 05:46
Show Gist options
  • Select an option

  • Save relthyg/f1500c8688d250d2e9432911ce4e1fe4 to your computer and use it in GitHub Desktop.

Select an option

Save relthyg/f1500c8688d250d2e9432911ce4e1fe4 to your computer and use it in GitHub Desktop.
"mark all as read" button for old.reddit.com userscript
// ==UserScript==
// @name "mark all as read" button for old.reddit.com
// @description Adds a "mark all as read" button to https://old.reddit.com/message/unread/
// @match https://old.reddit.com/message/unread/*
// @author relthyg
// ==/UserScript==
(function() {
function getModhash() {
// Prefer the global reddit object, fall back to the hidden input
return (typeof reddit !== 'undefined' && reddit.modhash)
|| document.querySelector('input[name="uh"]')?.value
|| null;
}
function markAllAsRead() {
var modhash = getModhash();
if (!modhash) {
console.log('"mark all as read": could not find modhash, aborting.');
return;
}
fetch(location.origin + "/api/read_all_messages", {
method: "POST",
headers: { "Content-Type": "application/x-www-form-urlencoded" },
body: "uh=" + encodeURIComponent(modhash) + "&api_type=json"
})
.then(function(res) {
if (res.ok) {
location.reload();
} else {
console.log('"mark all as read" failed. Status:', res.status);
}
})
.catch(function(err) {
console.log('"mark all as read" error:', err);
});
}
function addButton() {
var firstUnread = document.querySelector('.message.new');
if (!firstUnread || document.getElementById('mark-all-read-btn')) {
return;
}
var btn = document.createElement('button');
btn.id = 'mark-all-read-btn';
btn.textContent = 'mark all as read';
btn.style.cssText = 'margin:15px 0px 0px 13px;';
btn.onclick = markAllAsRead;
firstUnread.parentNode.insertBefore(btn, firstUnread);
}
if (document.readyState === 'loading') {
document.addEventListener('DOMContentLoaded', addButton);
} else {
addButton();
}
})();
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment