diff --git a/Plugins.md b/Plugins.md index 2a71d58..489e0b7 100644 --- a/Plugins.md +++ b/Plugins.md @@ -79,4 +79,51 @@ Better readability on big resolutions screens (css) ol li .message { font-size: 1.5em; } +``` + +## Add converted temperature + +``` javascript +// A Tentia plugin to automatically convert fahrenheit to celcius +// and vice versa. Change the unit in the line: var unit = "F"; + +$(document).ready(function() { + if(tentia_instance && tentia_instance.body) { + tentia_instance.body.addEventListener( + 'DOMNodeInserted', + tmpConv, + false + ); + } +}); + +function tmpConv(e) { + + var unit = "F"; // either F or C, the one you want to convert + + var element = e.target; + if(element.parentNode != tentia_instance.body) return; + + var message = $(element).find(".message"); + + var callback = f2cCallback; + if (unit == "C") callback = c2fCallback; + + var regex = new RegExp("(-?[0-9]+) ?°? ?" + unit + "+", "ig"); + + text = message.html().replace(regex, callback); + message.html(text); +} + +function f2cCallback(f, fahrenheit) { + fahrenheit = parseInt(fahrenheit, 10); + var celsius = (fahrenheit - 32) * 5 / 9; + return f + " (" + parseInt(celsius) + "°C)"; +} + +function c2fCallback(c, celsius) { + celsius = parseInt(celsius, 10); + var fahrenheit = celsius * 1.8 + 32; + return c + " (" + parseInt(fahrenheit) + "°F)"; +} ``` \ No newline at end of file