-
-
Save jtojnar/2b3ad35b4584d83f0c8c3472c29b620b to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// ==UserScript== | |
// @name Dev.to Mark as read | |
// @namespace to.dev.mark-read | |
// @include https://dev.to/* | |
// @version 1 | |
// @grant none | |
// ==/UserScript== | |
Array.from(document.querySelectorAll('.comment-deep-0')).forEach(el => { | |
const toggleReadButton = document.createElement('a'); | |
toggleReadButton.setAttribute('href', '#'); | |
toggleReadButton.textContent = 'Toggle read'; | |
toggleReadButton.addEventListener('click', (e) => { | |
let previous = JSON.parse(localStorage.getItem('read-comments') || '[]'); | |
const id = el.getAttribute('data-comment-id'); | |
const index = previous.indexOf(id); | |
if (index > -1) { | |
el.style.opacity = 1; | |
previous.splice(index, 1); | |
} else { | |
el.style.opacity = 0.5; | |
previous.push(id); | |
} | |
localStorage.setItem('read-comments', JSON.stringify(previous)); | |
e.preventDefault(); | |
}); | |
el.querySelector('.actions').appendChild(toggleReadButton); | |
}); | |
let previous = JSON.parse(localStorage.getItem('read-comments') || '[]'); | |
previous.forEach(id => { | |
const comment = document.getElementById('comment-node-' + id); | |
if (comment) { | |
comment.style.opacity = 0.5; | |
} | |
}); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment