Skip to content

Instantly share code, notes, and snippets.

@Mellen
Last active November 2, 2018 10:32
Show Gist options
  • Save Mellen/a33ece3cfebc0690800c to your computer and use it in GitHub Desktop.
Save Mellen/a33ece3cfebc0690800c to your computer and use it in GitHub Desktop.
grease monkey script for Stack Exchange chat that allows other users to force you to tell them the result of converting from one temperature scale to another
// ==UserScript==
// @name Chat Commands
// @namespace StackScripts
// @description commands that can be executed in chat
// @version 1
// @grant none
// @include http://chat.stackexchange.com/rooms/1/sandbox
// @include http://chat.stackexchange.com/rooms/35/the-bridge
// @author http://english.stackexchange.com/users/3559/matt-%D0%AD%D0%BB%D0%BB%D0%B5%D0%BD
// ==/UserScript==
var outputArea = document.getElementById('input');
var sendButton = document.getElementById('sayit-button');
var commands =
{
c2f:
{
run:function()
{
var celsius = parseFloat(arguments[0]);
if(Number.isNaN(celsius))
{
return;
}
return (celsius * 9/5 + 32) + '°F';
},
desc:'Converts Celsius to Fahrenheit',
usage:'c2f <Celsius value>\ne.g. c2f 100',
},
f2c:
{
run:function()
{
var fahrenheit = parseFloat(arguments[0]);
if(Number.isNaN(fahrenheit))
{
return;
}
return ((fahrenheit - 32) * 5/9) + '°C';
},
desc:'Converts Fahrenheit to Celsius',
usage:'f2c <Fahrenheit value>\ne.g. f2c 212',
},
c2k:
{
run:function()
{
var celsius = parseFloat(arguments[0]);
if(Number.isNaN(celsius))
{
return;
}
return (celsius + 273.15) + 'K';
},
desc:'Converts Celsius to Kelvin',
usage:'c2k <Celsius value>\ne.g. c2k 100',
},
f2k:
{
run:function()
{
var fahrenheit = parseFloat(arguments[0]);
if(Number.isNaN(fahrenheit))
{
return;
}
return (((fahrenheit - 32) * 5/9) + 273.15) + 'K';
},
desc:'Converts Fahrenheit to kevin',
usage:'f2k <Fahrenheit value>\ne.g. f2k 212',
},
k2c:
{
run:function()
{
var kelvin = parseFloat(arguments[0]);
if(Number.isNaN(kelvin))
{
return;
}
return (kelvin - 273.15) + '°C';
},
desc:'Converts Kelvin to Celsius',
usage:'k2c <Kelvin value>\ne.g. k2c 273.15',
},
k2f:
{
run:function()
{
var kelvin = parseFloat(arguments[0]);
if(Number.isNaN(kelvin))
{
return;
}
var celsius = kelvin - 273.15;
return (celsius * 9/5 + 32) + '°F';
},
desc:'Converts Kelvin to Fahrenheit',
usage:'k2f <Kelvin value>\ne.g. k2f 100',
},
help:
{
run:function()
{
if(arguments[0] != '')
{
var command = arguments[0];
if(commands[command])
{
return 'usage: ' + commands[command].usage;
}
else
{
return;
}
}
else
{
return 'usage: ' + commands['help'].usage;
}
},
desc:'Get help on a command',
usage:'help <command name>\ne.g. help c2f\nfor a list of commands type list',
},
list:
{
run:function()
{
if (arguments[0] == '')
{
var listText = '';
for(var i in commands)
{
listText += i + ': ' + commands[i].desc + '\n';
}
return listText;
}
return;
},
desc:'List available commands',
usage:'list'
},
};
var lastCommand = '';
function runCommand(fullCommand, replyto)
{
if(lastCommand == fullCommand)
return;
lastCommand = fullCommand;
var command = fullCommand.split(' ')[0].toLowerCase();
if(commands[command])
{
var params = fullCommand.split(' ');
params.splice(0,1);
var result = commands[command].run(params);
if(result)
{
outputArea.value = ':' + replyto + ' ' + result;
sendButton.click();
}
}
else
{
console.log('no such command');
}
}
var target = document.getElementById('chat');
var observer = new MutationObserver(function(mutations) {
mutations.forEach(function(mutation)
{
var hasPopup = false;
if(mutation.addedNodes.length > 0)
{
hasPopup = mutation.addedNodes[0].className == 'popup';
}
if(mutation.removedNodes.length > 0)
{
hasPopup = mutation.removedNodes[0].className == 'popup'
}
if(mutation.target.className.contains('monologue')
|| hasPopup)
{
return;
}
console.log(mutation);
var content = null;
var id = mutation.target.id;
if(id)
{
content = document.getElementById(mutation.target.id).querySelector('.content');
}
else
{
id = mutation.target.lastChild.id;
content = document.getElementById(id).querySelector('.content');
}
if(content)
{
var fullCommand = content.textContent;
var replyto = id.split('-')[1];
if(replyto)
runCommand(fullCommand, replyto);
}
});
});
// configuration of the observer:
var config = { attributes: false, childList: true, subtree: true, characterData: false };
// pass in the target node, as well as the observer options
observer.observe(target, config);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment