Skip to content

Instantly share code, notes, and snippets.

@fredley
Last active August 29, 2015 14:16
Show Gist options
  • Save fredley/b07f98ddb87e37f3a22f to your computer and use it in GitHub Desktop.
Save fredley/b07f98ddb87e37f3a22f to your computer and use it in GitHub Desktop.
Chat Topic Change Diffs
// ==UserScript==
// @name Room Topic Diff
// @description Whenever the room topic is changed, displays what it was before
// @author fredley
// @version 1.0.0
// @include *://chat.meta.stackoverflow.com/rooms/*
// @include *://chat.meta.stackexchange.com/rooms/*
// @include *://chat.stackexchange.com/rooms/*
// @include *://chat.stackoverflow.com/rooms/*
// @include *://chat.askubuntu.com/rooms/*
// @run-at document-end
// ==/UserScript==
function livequery($) {
$.extend($.fn,{livequery:function(e,i,t){var u,n=this;return $.isFunction(e)&&(t=i,i=e,e=void 0),$.each($.livequery.queries,function(r,s){return n.selector!=s.selector||n.context!=s.context||e!=s.type||i&&i.$lqguid!=s.fn.$lqguid||t&&t.$lqguid!=s.fn2.$lqguid?void 0:(u=s)&&!1}),u=u||new $.livequery(this.selector,this.context,e,i,t),u.stopped=!1,u.run(),this},expire:function(e,i,t){var u=this;return $.isFunction(e)&&(t=i,i=e,e=void 0),$.each($.livequery.queries,function(n,r){u.selector!=r.selector||u.context!=r.context||e&&e!=r.type||i&&i.$lqguid!=r.fn.$lqguid||t&&t.$lqguid!=r.fn2.$lqguid||this.stopped||$.livequery.stop(r.id)}),this}}),$.livequery=function(e,i,t,u,n){return this.selector=e,this.context=i,this.type=t,this.fn=u,this.fn2=n,this.elements=[],this.stopped=!1,this.id=$.livequery.queries.push(this)-1,u.$lqguid=u.$lqguid||$.livequery.guid++,n&&(n.$lqguid=n.$lqguid||$.livequery.guid++),this},$.livequery.prototype={stop:function(){var e=this;this.type?this.elements.unbind(this.type,this.fn):this.fn2&&this.elements.each(function(i,t){e.fn2.apply(t)}),this.elements=[],this.stopped=!0},run:function(){if(!this.stopped){var e=this,i=this.elements,t=$(this.selector,this.context),u=t.not(i);this.elements=t,this.type?(u.bind(this.type,this.fn),i.length>0&&$.each(i,function(i,u){$.inArray(u,t)<0&&$.event.remove(u,e.type,e.fn)})):(u.each(function(){e.fn.apply(this)}),this.fn2&&i.length>0&&$.each(i,function(i,u){$.inArray(u,t)<0&&e.fn2.apply(u)}))}}},$.extend($.livequery,{guid:0,queries:[],queue:[],running:!1,timeout:null,checkQueue:function(){if($.livequery.running&&$.livequery.queue.length)for(var e=$.livequery.queue.length;e--;)$.livequery.queries[$.livequery.queue.shift()].run()},pause:function(){$.livequery.running=!1},play:function(){$.livequery.running=!0,$.livequery.run()},registerPlugin:function(){$.each(arguments,function(e,i){if($.fn[i]){var t=$.fn[i];$.fn[i]=function(){var e=$,i=t.apply(this,arguments);return e.livequery.run(),i}}})},run:function(e){void 0!=e?$.inArray(e,$.livequery.queue)<0&&$.livequery.queue.push(e):$.each($.livequery.queries,function(e){$.inArray(e,$.livequery.queue)<0&&$.livequery.queue.push(e)}),$.livequery.timeout&&clearTimeout($.livequery.timeout),$.livequery.timeout=setTimeout($.livequery.checkQueue,20)},stop:function(e){void 0!=e?$.livequery.queries[e].stop():$.each($.livequery.queries,function(e){$.livequery.queries[e].stop()})}}),$.livequery.registerPlugin("append","prepend","after","before","wrap","attr","removeAttr","addClass","removeClass","toggleClass","empty","remove","html"),$(function(){$.livequery.play()});
}
function inject() {
for (var i = 0; i < arguments.length; ++i) {
if (typeof(arguments[i]) == 'function') {
var script = document.createElement('script');
script.type = 'text/javascript';
script.textContent = '(' + arguments[i].toString() + ')(jQuery)';
document.body.appendChild(script);
}
}
}
inject(livequery,function ($) {
function checkMessages(){
$('.message').each(function(){
if(!$(this).hasClass('topic-checked')){
$(this).addClass('topic-checked');
if($(this).find('.content').text().indexOf('room topic changed to') === 0){
var newtopic = $(this).find('.content').text().substring(22);
$(this).find('.content').html('<p><b>Room topic changed to:</b></p><p>'+newtopic+'</p><p><b>Previously it was:</b></p><p>'+window.topic+'</p>');
window.topic = newtopic;
}
}
});
}
$(document).ready(function() {
window.topic = $('#roomname').html() + ': ' + $('#roomdesc').html();
$('#room-tags .tag').each(function(){
window.topic += ' [' + $(this).html() + ']';
});
var wait = setInterval(function(){
if($('.message').length > 0){
$('.message').addClass('topic-checked');
$('#chat .message').livequery(checkMessages);
clearInterval(wait);
}
}, 500);
});
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment