woubuc / TGC Inches to CM converter

// ==UserScript==
// @name         TGC Inches to CM converter
// @namespace    http://tampermonkey.net/
// @version      0.1
// @description  Converts all sizes in inches to cm
// @author       You
// @match        https://www.thegamecrafter.com/*
// @grant        none
// ==/UserScript==
/* jshint -W097 */
'use strict';

// Inches to cm ratio
var inchToCm = 2.54;

$(function() {
    
    // Find all table cells with the text 'inches' or 'in' or '"' in it
    $('td:contains( inches), td:contains( in), td:contains(")').each(function(){
        
        // Get the text content of the table cell
        var text = $(this).text()
        
        // Remove 'inches' or 'in' or '"' from the text and split the remaining text (should be '.. x .. x ..') on 'x'
        var textArray = text.replace(/(inches|in|")/, '').split('x');
        
        // Convert each value to cm
        var converted = $.map(textArray, function(textNum, i){
            
            // Parse a float from the numerical text
            var num = parseFloat(textNum);
            
            // Return if this isn't a number
            if(isNaN(num)){
                if(console !== undefined){
                    console.log('Could not parse to number:', textNum);
                }
                return false;
            }
            
            // Convert to cm
            num = num * inchToCm
            
            // Format number with a maximum of 4 digits after the decimal point
            var numText = num.toString();
            var numArray = numText.split(numText.indexOf('.') === -1 ? ',' : '.'); // Decimal point should be either '.' or ',' depending on browser locale settings
            if(numArray.length > 1){
                if(numArray[1].length > 4){
                    numText = num.toFixed(4);
                }
            }
            
            return numText;
        });
        
        // Return if there is only one converted number. Dimensions contain at least 2 numbers.
        if(converted.length < 2){
            return false;
        }
        
        // Format the numbers array into a '.. x .. x .. cm' string
        var cmText = converted.join(' x ') + ' cm'
        
        // Write the converted value in the table cell
        $(this).text(cmText);
        
        // Append an additional element showing the original value of the table cell
        var converted = $('<div />');
        converted.css({fontSize: 11});
        converted.text('(' + text + ')');
        converted.appendTo($(this));
    });
});