Created
May 23, 2016 19:01
-
-
Save ConorOBrien-Foxx/6972521058d50d195c503fc35ed593fd to your computer and use it in GitHub Desktop.
Group pings
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 storageAvailable(type) { | |
try { | |
var storage = window[type], | |
x = '__storage_test__'; | |
storage.setItem(x, x); | |
storage.removeItem(x); | |
return true; | |
} | |
catch(e) { | |
return false; | |
} | |
} | |
var observeDOM = (function(){ | |
var MutationObserver = window.MutationObserver || window.WebKitMutationObserver, | |
eventListenerSupported = window.addEventListener; | |
return function(obj, callback){ | |
if( MutationObserver ){ | |
// define a new observer | |
var obs = new MutationObserver(function(mutations, observer){ | |
if( mutations[0].addedNodes.length || mutations[0].removedNodes.length ) | |
callback(); | |
}); | |
// have the observer observe foo for changes in children | |
obs.observe( obj, { childList:true, subtree:true }); | |
} | |
else if( eventListenerSupported ){ | |
obj.addEventListener('DOMNodeInserted', callback, false); | |
obj.addEventListener('DOMNodeRemoved', callback, false); | |
} | |
} | |
})(); | |
// add the button for adding a group | |
var btn = document.createElement("button"); | |
btn.innerHTML = "add/remove a group"; | |
btn.id = "group-add"; | |
btn.className = "button"; | |
btn.addEventListener("click", function(){ | |
if(!storageAvailable("localStorage")){ | |
alert("ERROR: no storage available/storage unsupported"); | |
return; | |
} | |
var groupName = prompt("enter the name of the group"); | |
// validate name--cannot have { or } in it. | |
localStorage.chatGroups = localStorage.chatGroups || "{}"; | |
var ch = JSON.parse(localStorage.chatGroups); | |
ch[groupName] = !ch[groupName]; | |
alert((ch[groupName] ? "You are now" : "You are no longer") + " listening to group notifications for @{"+ groupName + "}"); | |
localStorage.chatGroups = JSON.stringify(ch); | |
}); | |
document.getElementById("chat-buttons").appendChild(btn); | |
var lastMessage = ""; | |
observeDOM(document.getElementById("chat"), function(){ | |
// get groups | |
if(!storageAvailable("localStorage")){ | |
alert("ERROR: no storage available/storage unsupported"); | |
return; | |
} | |
// get most recent message | |
var msg = document.querySelector(".user-container:last-child [id^='message-']:last-child .content"); | |
// get innards | |
var txt = msg.innerText || msg.innerHTML; | |
if(lastMessage === txt) return; // don't ping the same message twice | |
lastMessage = txt; | |
var groupPings = txt.match(/@\{[^{}]+?\}/g); | |
if(!groupPings) return; | |
var ch = JSON.parse(localStorage.chatGroups); | |
// iterate through pings | |
for(var i = 0; i < groupPings.length; i++){ | |
var ping = groupPings[i].slice(2, -1); | |
// console.log(ch[ping], ping, ch); | |
if(ch[ping]){ | |
// break and ping | |
new Audio("http://cdn-chat.sstatic.net/chat/se.mp3").play(); | |
break; | |
} | |
} | |
}); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment