Skip to content

Instantly share code, notes, and snippets.

@relipse
Last active January 3, 2016 16:39
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save relipse/8490770 to your computer and use it in GitHub Desktop.
Save relipse/8490770 to your computer and use it in GitHub Desktop.
Easier irccloud channel switching
/**
* The purpose of this script is to allow quick traversing between channels using the keyboard
* For Example, after injecting this script, go to any channel text box and type:
* //<server>#<channel> <optional-text>
* or as below:
* //freenode#javascript hey i have a js question
* It will auto-switch to freenode's #javascript channel and enter that in the text box
* Copyright (C) relipse
* License: Use it however, just don't sell it or say it was you who coded it (Rev 21:8)
*/
glbRelipseInjectedScriptDetector = function(){
function grab_connected_servers(filter){
filter = filter || false;
var servers = [];
$('.connection.connected h2.buffer.active > a.buffer').each(function(){
if (filter && this.href.indexOf(filter) !== -1){
servers.push(this.href);
}
});
return servers;
}
function parsehash(hash){
hash = hash || window.location.hash;
console.log(hash);
var match = /#!\/(ircs:\/\/)?([^\s]+)?\/([^\s]+)?/img.exec(hash);
if (match !== null){
console.log(match);
var res = {};
res.server = match[2];
res.channel = match[3];
return res;
}
return false;
}
function makehash(network, channel){
if (!network){
var curhash = parsehash();
network = curhash.server;
}
if (!network){ return false; }
var prefix = '#!/';
if (network.indexOf('#!/') === 0){
prefix = '';
}
//prefix with '#' automatically
channel = '%23' + channel;
return prefix+network+'/'+channel;
}
function detect_channel_switch(txt){
var match = /\/\/(.*?)#([^\s]+)( .*)?/.exec(txt);
if (match === null){ return false; }
match[1] = match[1] || '';
match[2] = match[2] || '';
match[3] = match[3] || '';
var res = {shortserver: match[1], channel:match[2], text:match[3].trim()}
var possible_servers = grab_connected_servers(res.shortserver);
res.possible_servers = possible_servers;
if (!possible_servers || possible_servers.length === 0){
res.error_code = -1;
res.error = 'no servers found';
return res;
}
if (possible_servers.length > 1){
res.error_code = -2;
res.error = 'multiple servers found, ambiguous';
}
res.found_server = possible_servers[0];
res.server = res.found_server;
return res;
}
//thanks http://bresleveloper.blogspot.co.il/2013/03/jsjq-simulate-enter-event.html
function simulate_enter($textbox){
//now automatically trigger an enter-keypress to send text
var press = jQuery.Event("keypress");
press.bubbles = true;
press.cancelable = true;
press.charCode = 13;
press.currentTarget = $textbox[0];
press.eventPhase = 2;
press.keyCode = 13;
press.returnValue = true;
press.srcElement = $textbox[0];
press.target = $textbox[0];
press.type = "keypress";
press.view = Window;
press.which = 13;
$textbox.trigger(press);
}
function detect_preprocess_code(txt){
if (txt.indexOf('//') !== -1){
var channel_switch = detect_channel_switch(txt);
console.log(channel_switch);
if (channel_switch.error_code){
alert(channel_switch.error);
return false;
}
var nwhash = makehash(channel_switch.server, channel_switch.channel);
if (nwhash){
//alert(nwhash);
window.location.hash = nwhash;
if (channel_switch.text){
setTimeout(function(){
var $textbox = $('td.inputcell textarea');
$textbox.val(channel_switch.text);
simulate_enter($textbox);
},150);
}
}
return true;
}
return false; //preprocess not detected
}
glbRelipseDetectPreProcess = function(txt){ detect_preprocess_code(txt); if (txt.indexOf('//') === 0){ return true; } };
$('.inputcell .input textarea').bind('keydown', function(e) {
var code = e.keyCode || e.which;
if(code == 13) {
if (glbRelipseDetectPreProcess(this.value)){
e.preventDefault();
this.value = '';
}
}
return true;
});
};
$(function(){
//every time user changes channels we need to reload this script
$(window).on('hashchange', function() {
glbRelipseInjectedScriptDetector();
});
glbRelipseInjectedScriptDetector();
});
/**dynamically inject this onto irccloud page like this:
(function(src){
var s = document.createElement("script");
s.src = src;
$("head").append(s);
return s;
})('http://path/to/file.js');
*/
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment