Last active

Embed URL

HTTPS clone URL

SSH clone URL

You can clone with HTTPS or SSH.

Download Gist

Change the base of every number on the page

View changebase.bookmarklet.js
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71
if(!window.currentBase) {
window.currentBase = 10;
}
 
var digits = '0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz';
var subdigits = '\u2080\u2081\u2082\u2083\u2084\u2085\u2086\u2087\u2088\u2089';
 
var base = undefined;
while(isNaN(base)) {
base = parseInt(prompt('What base would you like?'));
}
var sub_base = base==10 ? '' : (base+'').split('').map(function(d){return subdigits[d]}).join('');
 
function parseBaseInt(s,base) {
var n = 0;
var p = 1;
for(var i=s.length-1;i>=0;i--) {
n += digits.indexOf(s.charAt(i))*p;
p *= base;
}
return n;
}
 
function toBase(n,b) {
if(n===0) {
return '0';
}
var s = '';
while(n) {
var m = n%b;
s = digits[m]+s;
n = (n-m)/b;
}
return s;
}
 
function comma(n){
var re = new RegExp('(['+digits+']{3}(?!.*\\.|$))','g');
return n.toString().split('').reverse().join('').replace(re, '$1,').split('').reverse().join('');
}
 
function changeBase(s,n,commas,currentBase) {
currentBase = parseInt(currentBase) || window.currentBase;
if(commas) {
n = n.replace(/,/g,'');
n = toBase(parseBaseInt(n,window.currentBase),base);
n = comma(n);
} else {
var on = n;
n = toBase(parseBaseInt(n,window.currentBase),base);
}
return n+sub_base;
}
 
function fix(node) {
var matchDigits = digits.slice(0,window.currentBase);
var re = new RegExp('((['+matchDigits+']{1,3}(?:,['+matchDigits+']{3})+)|['+matchDigits+']+)([\u2080-\u2089]+)'+(window.done ? '' : '?'),'g');
node.textContent = node.textContent.replace(re,changeBase);
}
 
function textNodesUnder(el){
var n, a=[], walk=document.createTreeWalker(el,NodeFilter.SHOW_TEXT,null,false);
while(n=walk.nextNode()){ a.push(n);}
return a;
}
 
var nodes=textNodesUnder(document.body);
 
Array.prototype.map.call(nodes,fix);
window.currentBase = base;
window.done = true;
View changebase.bookmarklet.js
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70
//change this to the base you want to see
var base = 12;
 
if(!window.currentBase) {
window.currentBase = 10;
}
 
var digits = '0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz';
var subdigits = '\u2080\u2081\u2082\u2083\u2084\u2085\u2086\u2087\u2088\u2089';
 
var sub_base = base==10 ? '' : (base+'').split('').map(function(d){return subdigits[d]}).join('');
 
function parseBaseInt(s,base) {
var n = 0;
var p = 1;
for(var i=s.length-1;i>=0;i--) {
n += digits.indexOf(s.charAt(i))*p;
p *= base;
}
return n;
}
 
function toBase(n,b) {
if(n===0) {
return '0';
}
var s = '';
while(n) {
var m = n%b;
s = digits[m]+s;
n = (n-m)/b;
}
return s;
}
 
function comma(n){
var re = new RegExp('(['+digits+']{3}(?!.*\\.|$))','g');
return n.toString().split('').reverse().join('').replace(re, '$1,').split('').reverse().join('');
}
 
function changeBase(s,n,commas,currentBase) {
currentBase = parseInt(currentBase) || window.currentBase;
if(commas) {
n = n.replace(/,/g,'');
n = toBase(parseBaseInt(n,window.currentBase),base);
n = comma(n);
} else {
var on = n;
n = toBase(parseBaseInt(n,window.currentBase),base);
}
return n+sub_base;
}
 
function fix(node) {
var matchDigits = digits.slice(0,window.currentBase);
var re = new RegExp('((['+matchDigits+']{1,3}(?:,['+matchDigits+']{3})+)|['+matchDigits+']+)([\u2080-\u2089]+)'+(window.done ? '' : '?'),'g');
node.textContent = node.textContent.replace(re,changeBase);
}
 
function textNodesUnder(el){
var n, a=[], walk=document.createTreeWalker(el,NodeFilter.SHOW_TEXT,null,false);
while(n=walk.nextNode()){ a.push(n);}
return a;
}
 
var nodes=textNodesUnder(document.body);
 
Array.prototype.map.call(nodes,fix);
window.currentBase = base;
window.done = true;
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Something went wrong with that request. Please try again.