Skip to content

Instantly share code, notes, and snippets.

@rlemon rlemon/checkignore.js
Last active Nov 10, 2016

Embed
What would you like to do?
is a user ignoring you?
function checkIgnore(id) {
return $.post(`/users/thumbs/${id}`, fkey({showUsage:true}));
}
function ignoreParser(node) {
if( !node.classList || !node.classList.contains('user-container') ) return;
let id = node.className.match(/user-([0-9]+)/);
if( !id ) return; // cases where we match the node but has no user class
id = id.pop(); // just want the id. this is very ugly.
if( !id ) return console.log('error');
checkIgnore(id).then( ret => {
let item = ignoring.find(item => item.id === ret.id );
if( ret.id === myid || ret.reputation < 20 ) return;
if( !item ) {
ignoring.push(item = {id: ret.id, ignoring: false});
}
if( !ret.may_pairoff && !item.ignoring ) {
item.ignoring = true;
buildSheet();
} else if (ret.may_pairoff && item.ignoring ) {
item.ignoring = false;
buildSheet();
}
});
}
function buildSheet() {
sheet.textContent = ignoring.map( item => {
if( !item.ignoring ) return;
return `
.user-${item.id} .avatar { background-color: red !important; position: relative; }
.user-${item.id} .avatar img { opacity: 0.3 !important; }
.user-${item.id} .avatar:hover::after { content: 'is ignoring you'; position: absolute; top: 0px; right: -86px; background-color: #222; color: red; border-radius: 6px; padding: 2px 8px; width: 72px; height: 16px; z-index: 9999; text-align: center; font-size: 9.6px; }
`
}).filter(Boolean).join('');
}
const sheet = document.createElement('style');
const ignoring = [];
const myid = Number(document.getElementById("active-user").className.match(/user-([0-9]+)/)[1]);
document.head.appendChild(sheet);
if( typeof DOMObserver !== 'undefined' ) {
DOMObserver.addParser(ignoreParser, '#chat .user-container');
DOMObserver.drain();
} else {
Array.from(document.querySelectorAll('#chat .user-container')).forEach(ignoreParser);
new MutationObserver( records => {
for( const record of records ) {
Array.from(record.addedNodes).forEach(ignoreParser);
}
}).observe(document.body, {
subtree: true,
childList: true
});
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.