Last active
November 10, 2016 19:38
-
-
Save rlemon/201d2cc02719771a401dceba0444e7da to your computer and use it in GitHub Desktop.
is a user ignoring you?
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
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