Created
August 31, 2016 08:26
-
-
Save EvidentlyCube/b64cc8b28cb86685d23e9c046f897a3e to your computer and use it in GitHub Desktop.
Displays a tooltip with precise number when hovering over all numbers in Trimps. Warning: the numbers rendered before the plugin is loaded won't register the tooltip handlers, so for example HPs before you fight might not react.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// ==UserScript== | |
// @name Trimps precise number hover | |
// @namespace http://tampermonkey.net/ | |
// @version 0.1 | |
// @description Displays a tooltip with precise number when hovering over all numbers in Trimps. Warning: the numbers rendered before the plugin is loaded won't register the tooltip handlers, so for example HPs before you fight might not react. | |
// @author Maurycy Zarzycki, Retrocade.net | |
// @match https://trimps.github.io/ | |
// @require http://code.jquery.com/jquery-latest.js | |
// @grant none | |
// ==/UserScript== | |
(function() { | |
'use strict'; | |
var mouseX = 0; | |
var mouseY = 0; | |
var $tooltip = $('<div class="number-tooltip"></div>').appendTo('body'); | |
$('<style>.number-tooltip{position: absolute; display: none; background: black; padding: 10px; color: white;}</style>').appendTo('head'); | |
$(document).mousemove(function(event) { | |
mouseX = event.pageX; | |
mouseY = event.pageY; | |
}); | |
function number_format (number, decimals, decPoint, thousandsSep) { | |
// Taken from: http://locutus.io/php/strings/number_format/ | |
number = (number + '').replace(/[^0-9+\-Ee.]/g, ''); | |
var n = !isFinite(+number) ? 0 : +number; | |
var prec = !isFinite(+decimals) ? 0 : Math.abs(decimals); | |
var sep = (typeof thousandsSep === 'undefined') ? ',' : thousandsSep; | |
var dec = (typeof decPoint === 'undefined') ? '.' : decPoint; | |
var s = ''; | |
var toFixedFix = function (n, prec) { | |
var k = Math.pow(10, prec); | |
return '' + (Math.round(n * k) / k) | |
.toFixed(prec); | |
}; | |
// @todo: for IE parseFloat(0.55).toFixed(0) = 0; | |
s = (prec ? toFixedFix(n, prec) : '' + Math.round(n)).split('.'); | |
if (s[0].length > 3) { | |
s[0] = s[0].replace(/\B(?=(?:\d{3})+(?!\d))/g, sep); | |
} | |
if ((s[1] || '').length < prec) { | |
s[1] = s[1] || ''; | |
s[1] += new Array(prec - s[1].length + 1).join('0'); | |
} | |
return s.join(dec); | |
} | |
var prettify_old = window.prettify; | |
window.prettify = function(n, d){ | |
var prettyNumber = number_format(n, 2, ".", " "); | |
return '<span onmouseout="numberTooltipOut()" onmousemove="numberTooltipHover(\'' +prettyNumber+ '\')">'+prettify_old(n, d)+'</span>'; | |
}; | |
window.numberTooltipHover = function(number){ | |
$tooltip.text(number).show(); | |
$tooltip.css('left', mouseX + 10); | |
$tooltip.css('top', mouseY + 10); | |
}; | |
window.numberTooltipOut = function(){ | |
$tooltip.hide(); | |
}; | |
})(); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment