Added temperature conversation plugin

jeena 2013-01-14 13:35:31 -08:00
parent 012f00f281
commit 4d3644df8c

@ -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)";
}
```