Skip to content

Instantly share code, notes, and snippets.

@ohAitch
Last active October 13, 2016 03:52
Show Gist options
  • Save ohAitch/9675344 to your computer and use it in GitHub Desktop.
Save ohAitch/9675344 to your computer and use it in GitHub Desktop.
Rudimentary urbit chat tagging, notifications.
// ==UserScript==
// @include http://chat.urbit.org/
// ==/UserScript==
$.getScript("https://cdn.rawgit.com/boronine/husl/v2.0.0/husl.min.js");
colorMap = {}; memo={}; $('.hl').contents().unwrap();
getColor = function (s) {  
var hash = s.split("").reduce(function (a, b) {    
a = ((a << 5) - a) + b.charCodeAt(0);    
return a & a  
}, 0);
return $.husl.toHex(
hash % 360,
85 - 10*Math.log(s.length) + (hash >> 8) % 30, //TODO: tweak/UI
75 + (hash >> 16) % 15)
};
highlightNames = function () {
$('#body, #membership').contents().each(function (s) {
if (this.nodeType != 3|| this.textContent in memo) return;
rep = this.textContent.replace(/~([a-z]{3}|-)+\b/g, function (s) {
            if (!(s in colorMap)) {    
                colorMap[s] = getColor(s);
            }  
            return '<span class="hl" style="background-color: ' + colorMap[s] + '">' + s + '</span>'
        })
        if(this.textContent == rep) memo[rep]=true;
        else $(this).replaceWith(rep);
    });
};
setInterval(highlightNames,400) //this would ideally hook the actual dom update
// ==UserScript==
// @include http://chat.urbit.org/
// ==/UserScript==
notify = function(a){new Notification(a), {icon:"http://urbit.org/favicon.png"}}; //TODO: Sounds
//Notification.requestPermissions() must be run manually once beforehand, 'in response to a user event' e.g. $(document).one('click',…)
bod = $('#body')[0].innerText;
check = function(){
cur = $('#body')[0].innerText;
added = cur.slice(bod.length);
bod = cur;
if(added) notify(added);
}
setInterval(check, 1000)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment