Skip to content

Instantly share code, notes, and snippets.

@elmarcoh
Created November 24, 2012 01:25
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save elmarcoh/4137961 to your computer and use it in GitHub Desktop.
Save elmarcoh/4137961 to your computer and use it in GitHub Desktop.
Form fill Bookmarklet with Chilean RUT
//Caution when minifiyng this file: the modulus operator in getRut must have spaces around it, otherwise
//Chrome throws an error, and the damn thing won't work
var auto =
{
names: 'Steve Buscemi Catherine Keener Dermot Mulroney Danielle Zerneck James LeGros Rica Martens Peter Dinklage Kevin Corrigan Hilary Gilford Robert Wightman Tom Jarmusch Michael Griffiths Matthew Grace Ryan Bowker Francesca DiMauro',blurb: 'phpBB is a free, open source Internet community application, with outstanding discussion forums and membership management. Written in the PHP scripting language, and making use of the popular MySQL database, phpBB is a standard among web hosting companies throughout the world, and is one of the most widely-used bulletin board packages in the world. phpBB short-circuits the need for you to be a web development master in order to create and manage massive online communities',password: 'secret',fillerup: function()
{
var all_inputs = document.getElementsByTagName('input');var all_selects = document.getElementsByTagName('select');var all_textareas = document.getElementsByTagName('textarea');for (var i = 0, max = all_selects.length; i < max; i++)
{
var sel = all_selects[i]; if (sel.selectedIndex != -1&& sel.options[sel.selectedIndex].value)
{
continue;
}
var howmany = 1; if (sel.type == 'select-multiple')
{
var howmany = 1 + this.getRand(sel.options.length - 1);
}
for (var j = 0; j < howmany; j++)
{
var index = this.getRand(sel.options.length - 1);sel.options[index].selected = 'selected';
}
}
for (var i = 0, max = all_textareas.length; i < max; i++)
{
var ta = all_textareas[i];if (!ta.value)
{
ta.value = this.getRandomString(10)+ '\n\n'+ this.getRandomString(10);
}
}
for (var i = 0, max = all_inputs.length; i < max; i++)
{
var inp = all_inputs[i];var type = inp.getAttribute('type');if (!type)
{
type = 'text';
}
if (type == 'checkbox')
{
inp.setAttribute('checked', 'checked');
}
if (type == 'radio')
{
var to_update = true;var name = inp.name;var input_array = inp.form.elements[inp.name];for (var j = 0; j < input_array.length; j++)
{
if (input_array[j].checked)
{
to_update = false;continue;
}
}
if (to_update)
{
var index = this.getRand(input_array.length - 1);input_array[index].setAttribute('checked', 'checked');
}
}
if (type == 'password')
{
if (!inp.value)
{
inp.value = this.getPassword();
}
}
if (type == 'text')
{
if (!inp.value)
{
if (this.hasTerm(inp.name, 'name', 'nombre'))
{
inp.value = this.getRandomName() + ' ' + this.getRandomName();
}
else if (inp.name.indexOf('email') != -1)
{
inp.value = this.getRandomString(1) + '@example.org';
}
else if(inp.name.indexOf('rut')!=-1)
{
inp.value=this.getRut()
}
else if(this.hasTerm(inp.name, 'fono', 'phone'))
{
inp.value=this.getRandomNumber(7);
}
else
{
inp.value = this.getRandomString(1);
}
}
}
}
}
,hasTerm: function()
{
var name = arguments[0];
for (var i = 1; i < arguments.length; i++) {
if(name.indexOf(arguments[i]) != -1) {
return true;
}
}
return false;
}
,getRandomNumber: function (length)
{
var max = Math.pow(10, length+1);
return Math.round(Math.random()*max)+1;
}
,getRandomString: function (how_many_words)
{
if (!how_many_words)
{
how_many_words = 5;
}
if (!this.words)
{
this.words = this.blurb.split(' ');
}
var retval = '';for (var i = 0; i < how_many_words; i++)
{
retval += this.words[this.getRand(this.words.length) - 1];retval += (i < how_many_words - 1) ? ' ' : '';
}
return retval;
}
,getRandomName: function ()
{
if (!this.split_names)
{
this.split_names = this.names.split(' ');
}
return this.split_names[this.getRand(this.split_names.length) - 1];
}
,getPassword: function ()
{
if (!this.password)
{
this.password = 'secret';
}
return this.password;
}
,getRut: function()
{
var rut=Math.round(Math.random()*(25000000-5000000))+5000000;
var dvr='0';var suma=0;var mul=2;
rut+='';
for(i=rut.length-1;i>=0;i--)
{
suma=suma+rut.charAt(i)*mul;if(mul==7)
{
mul=2;
}
else
{
mul++;
}
}
var res=(suma%11);
if(res==1)
{
dvr='k';
}
else if(res==0)
{
dvr='0';
}
else
{
dvr=11-res;
}
return rut+'-'+dvr;
}
,getRand: function (count)
{
return Math.round(count * Math.random());
}
}
; auto.fillerup()
//Create a bookmark with this content
javascript:var auto={names:'Steve Buscemi Catherine Keener Dermot Mulroney Danielle Zerneck James LeGros Rica Martens Peter Dinklage Kevin Corrigan Hilary Gilford Robert Wightman Tom Jarmusch Michael Griffiths Matthew Grace Ryan Bowker Francesca DiMauro',blurb:'phpBB is a free, open source Internet community application, with outstanding discussion forums and membership management. Written in the PHP scripting language, and making use of the popular MySQL database, phpBB is a standard among web hosting companies throughout the world, and is one of the most widely-used bulletin board packages in the world. phpBB short-circuits the need for you to be a web development master in order to create and manage massive online communities',password:'secret',fillerup:function(){var all_inputs=document.getElementsByTagName('input');var all_selects=document.getElementsByTagName('select');var all_textareas=document.getElementsByTagName('textarea');for(var i=0,max=all_selects.length;i<max;i++){var sel=all_selects[i];if(sel.selectedIndex!=-1&&sel.options[sel.selectedIndex].value){continue}var howmany=1;if(sel.type=='select-multiple'){var howmany=1+this.getRand(sel.options.length-1)}for(var j=0;j<howmany;j++){var index=this.getRand(sel.options.length-1);sel.options[index].selected='selected'}}for(var i=0,max=all_textareas.length;i<max;i++){var ta=all_textareas[i];if(!ta.value){ta.value=this.getRandomString(10)+'\n\n'+this.getRandomString(10)}}for(var i=0,max=all_inputs.length;i<max;i++){var inp=all_inputs[i];var type=inp.getAttribute('type');if(!type){type='text'}if(type=='checkbox'){inp.setAttribute('checked','checked')}if(type=='radio'){var to_update=true;var name=inp.name;var input_array=inp.form.elements[inp.name];for(var j=0;j<input_array.length;j++){if(input_array[j].checked){to_update=false;continue}}if(to_update){var index=this.getRand(input_array.length-1);input_array[index].setAttribute('checked','checked')}}if(type=='password'){if(!inp.value){inp.value=this.getPassword()}}if(type=='text'){if(!inp.value){if(inp.name.indexOf('name')!=-1){inp.value=this.getRandomName()+' '+this.getRandomName()}else if(inp.name.indexOf('email')!=-1){inp.value=this.getRandomString(1)+'@example.org'}else if(inp.name.indexOf('rut')!=-1){inp.value=this.getRut()}else{inp.value=this.getRandomString(1)}}}}},getRandomString:function(how_many_words){if(!how_many_words){how_many_words=5}if(!this.words){this.words=this.blurb.split(' ')}var retval='';for(var i=0;i<how_many_words;i++){retval+=this.words[this.getRand(this.words.length)-1];retval+=(i<how_many_words-1)?' ':''}return retval},getRandomName:function(){if(!this.split_names){this.split_names=this.names.split(' ')}return this.split_names[this.getRand(this.split_names.length)-1]},getPassword:function(){if(!this.password){this.password='secret'}return this.password},getRut:function(){var rut=Math.round(Math.random()*(25000000-5000000))+5000000;var dvr='0';var suma=0;var mul=2;rut+='';for(i=rut.length-1;i>=0;i--){suma=suma+rut.charAt(i)*mul;if(mul==7){mul=2}else{mul++}}var res=(suma % 11);if(res==1){dvr='k'}else if(res==0){dvr='0'}else{dvr=11-res}return rut+'-'+dvr},getRand:function(count){return Math.round(count*Math.random())}};auto.fillerup()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment