Skip to content

@christianp /changebase.bookmarklet.js
Last active

Embed URL

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Change the base of every number on the page
javascript:(function(){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;})()
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;
//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.