DonationCoder.com Forum

Main Area and Open Discussion => Living Room => Topic started by: Lefax32 on December 17, 2009, 04:30 PM

Title: bookmarklet javascript - modfication help
Post by: Lefax32 on December 17, 2009, 04:30 PM
Hi, can anyone help with modifying this bookmarklet code?

What it does is fill in the words randomly on any field. But I want it to do it in order as they are listed.

I tried a few guesses, like removing words 'random', but I really don't have any idea!

Thanks if you can help.  :D


javascript:var%20auto%20={names:%20'Steve%20Buscemi%20Catherine%20Keener%20Dermot%20Mulroney%20Danielle%20Zerneck%20James%20LeGros%20Rica%20Martens%20Peter%20Dinklage%20Kevin%20Corrigan%20Hilary%20Gilford%20Robert%20Wightman%20Tom%20Jarmusch%20Michael%20Griffiths%20Matthew%20Grace%20Ryan%20Bowker%20Francesca%20DiMauro',blurb:%20'phpBB%20is%20a%20free,%20open%20source%20Internet%20community%20application,%20with%20outstanding%20discussion%20forums%20and%20membership%20management.%20Written%20in%20the%20PHP%20scripting%20language,%20and%20making%20use%20of%20the%20popular%20MySQL%20database,%20phpBB%20is%20a%20standard%20among%20web%20hosting%20companies%20throughout%20the%20world,%20and%20is%20one%20of%20the%20most%20widely-used%20bulletin%20board%20packages%20in%20the%20world.%20phpBB%20short-circuits%20the%20need%20for%20you%20to%20be%20a%20web%20development%20master%20in%20order%20to%20create%20and%20manage%20massive%20online%20communities',password:%20'secret',fillerup:%20function()%20{var%20all_inputs%20=%20document.getElementsByTagName('input');var%20all_selects%20=%20document.getElementsByTagName('select');var%20all_textareas%20=%20document.getElementsByTagName('textarea');for%20(var%20i%20=%200,%20max%20=%20all_selects.length;%20i%20<%20max;%20i++)%20{var%20sel%20=%20all_selects;%20if%20(sel.selectedIndex%20!=%20-1&&%20sel.options[sel.selectedIndex].value)%20{continue;%20}var%20howmany%20=%201;%20if%20(sel.type%20==%20'select-multiple')%20{%20var%20howmany%20=%201%20+%20this.getRand(sel.options.length%20-%201);}for%20(var%20j%20=%200;%20j%20<%20howmany;%20j++)%20{var%20index%20=%20this.getRand(sel.options.length%20-%201);sel.options[index].selected%20=%20'selected';}}for%20(var%20i%20=%200,%20max%20=%20all_textareas.length;%20i%20<%20max;%20i++)%20{var%20ta%20=%20all_textareas;if%20(!ta.value)%20{ta.value%20=%20this.getRandomString(10)+%20'\n\n'+%20this.getRandomString(10);}}for%20(var%20i%20=%200,%20max%20=%20all_inputs.length;%20i%20<%20max;%20i++)%20{var%20inp%20=%20all_inputs;var%20type%20=%20inp.getAttribute('type');if%20(!type)%20{type%20=%20'text';%20}if%20(type%20==%20'checkbox')%20{inp.setAttribute('checked',%20'checked');}if%20(type%20==%20'radio')%20{var%20to_update%20=%20true;var%20name%20=%20inp.name;var%20input_array%20=%20inp.form.elements[inp.name];for%20(var%20j%20=%200;%20j%20<%20input_array.length;%20j++)%20{if%20(input_array[j].checked)%20{to_update%20=%20false;continue;}}if%20(to_update)%20{var%20index%20=%20this.getRand(input_array.length%20-%201);input_array[index].setAttribute('checked',%20'checked');}}if%20(type%20==%20'password')%20{if%20(!inp.value)%20{inp.value%20=%20this.getPassword();}}if%20(type%20==%20'text')%20{if%20(!inp.value)%20{if%20(inp.name.indexOf('name')%20!=%20-1)%20{inp.value%20=%20this.getRandomName()%20+%20'%20'%20+%20this.getRandomName();}%20else%20if%20(inp.name.indexOf('email')%20!=%20-1)%20{inp.value%20=%20this.getRandomString(1)%20+%20'@example.org';}%20else%20{inp.value%20=%20this.getRandomString(1);}}}}},getRandomString:%20function%20(how_many_words)%20{if%20(!how_many_words)%20{how_many_words%20=%205;}if%20(!this.words)%20{this.words%20=%20this.blurb.split('%20');}var%20retval%20=%20'';for%20(var%20i%20=%200;%20i%20<%20how_many_words;%20i++)%20{retval%20+=%20this.words[this.getRand(this.words.length)%20-%201];retval%20+=%20(i%20<%20how_many_words%20-%201)%20?%20'%20'%20:%20'';}return%20retval;},getRandomName:%20function%20()%20{if%20(!this.split_names)%20{this.split_names%20=%20this.names.split('%20');}return%20this.split_names[this.getRand(this.split_names.length)%20-%201];},getPassword:%20function%20()%20{if%20(!this.password)%20{this.password%20=%20'secret';}return%20this.password;},getRand:%20function%20(count)%20{return%20Math.round(count%20*%20Math.random());}};%20auto.fillerup()
Title: Re: bookmarklet javascript - modfication help
Post by: housetier on December 17, 2009, 11:40 PM
While I didn't answer your question I at least made it look pretty. Others might have it easier now to change the script. :)


Code: Javascript [Select]
  1. var auto = {
  2.     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',
  3.  
  4.     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',
  5.  
  6.     password: 'secret',
  7.  
  8.     fillerup: function() {
  9.         var all_inputs = document.getElementsByTagName('input');
  10.         var all_selects = document.getElementsByTagName('select');
  11.         var all_textareas = document.getElementsByTagName('textarea');
  12.  
  13.         for (var i = 0, max = all_selects.length; i < max; i++) {
  14.             var sel = all_selects;
  15.             if (sel.selectedIndex != -1&& sel.options[sel.selectedIndex].value) {
  16.                 continue;
  17.             }
  18.  
  19.             var howmany = 1;
  20.             if (sel.type == 'select-multiple') {
  21.                 var howmany = 1 + this.getRand(sel.options.length - 1);
  22.             }
  23.  
  24.             for (var j = 0; j < howmany; j++) {
  25.                 var index = this.getRand(sel.options.length - 1);
  26.                 sel.options[index].selected = 'selected';
  27.             }
  28.         }
  29.  
  30.         for (var i = 0, max = all_textareas.length; i < max; i++) {
  31.             var ta = all_textareas;
  32.             if (!ta.value) {
  33.                 ta.value = this.getRandomString(10)+ '\n\n'+ this.getRandomString(10);
  34.             }
  35.         }
  36.  
  37.         for (var i = 0, max = all_inputs.length; i < max; i++) {
  38.             var inp = all_inputs;
  39.             var type = inp.getAttribute('type');
  40.  
  41.             if (!type) {
  42.                 type = 'text';
  43.             }
  44.  
  45.             if (type == 'checkbox') {
  46.                 inp.setAttribute('checked', 'checked');
  47.             }
  48.  
  49.             if (type == 'radio') {
  50.                 var to_update = true;
  51.                 var name = inp.name;
  52.                 var input_array = inp.form.elements[inp.name];
  53.  
  54.                 for (var j = 0; j < input_array.length; j++) {
  55.                     if (input_array[j].checked) {
  56.                         to_update = false;
  57.                         continue;
  58.                     }
  59.                 }
  60.  
  61.                 if (to_update) {
  62.                     var index = this.getRand(input_array.length - 1);
  63.                     input_array[index].setAttribute('checked', 'checked');
  64.                 }
  65.             }
  66.  
  67.             if (type == 'password') {
  68.                 if (!inp.value) {
  69.                     inp.value = this.getPassword();
  70.                 }
  71.             }
  72.  
  73.             if (type == 'text') {
  74.                 if (!inp.value) {
  75.                     if (inp.name.indexOf('name') != -1) {
  76.                         inp.value = this.getRandomName() + ' ' + this.getRandomName();
  77.                     }
  78.                     else if (inp.name.indexOf('email') != -1) {
  79.                         inp.value = this.getRandomString(1) + '@example.org';
  80.                     }
  81.                     else {
  82.                         inp.value = this.getRandomString(1);
  83.                     }
  84.                 }
  85.             }
  86.         }
  87.     },
  88.  
  89.     getRandomString: function (how_many_words) {
  90.         if (!how_many_words) {
  91.             how_many_words = 5;
  92.         }
  93.  
  94.         if (!this.words) {
  95.             this.words = this.blurb.split(' ');
  96.         }
  97.  
  98.         var retval = '';
  99.  
  100.         for (var i = 0; i < how_many_words; i++) {
  101.             retval += this.words[this.getRand(this.words.length) - 1];
  102.             retval += (i < how_many_words - 1) ? ' ' : '';
  103.         }
  104.  
  105.         return retval;
  106.     },
  107.  
  108.     getRandomName: function () {
  109.         if (!this.split_names) {
  110.             this.split_names = this.names.split(' ');
  111.         }
  112.  
  113.         return this.split_names[this.getRand(this.split_names.length) - 1];
  114.     },
  115.  
  116.     getPassword: function () {
  117.         if (!this.password) {
  118.             this.password = 'secret';
  119.         }
  120.  
  121.         return this.password;
  122.     },
  123.  
  124.     getRand: function (count) {
  125.         return Math.round(count * Math.random());
  126.     }
  127. };
  128.  
  129. auto.fillerup()
Title: Re: bookmarklet javascript - modfication help
Post by: Lefax32 on December 18, 2009, 12:31 PM
Thankyou, that is much easier to look at. I didn't realize that bookmarklets could be made from that format. I thought they were actually written as they are (1 line). 
Title: Re: bookmarklet javascript - modfication help
Post by: housetier on December 18, 2009, 12:46 PM
I think they indeed have to written on one line. Prettying up made the code easier to read and change. Afterwards it has to be 'translated' into one line again I believe.

Since I have never used or changed a bookmarklet, I do not know for sure. It might be worth to try the big version...
Title: Re: bookmarklet javascript - modfication help
Post by: Lefax32 on December 18, 2009, 07:33 PM
Okay thanks for the info. Maybe there's a program that can translate it for you? I always thought that writing those bookmarklets in one line looks like a very confusing and tricky job.


Also, I think I was probably a bit vague about what I wanted to do.

What I want is for the words from this part below, to be used in order. So if there are 10 text box fields, I want the first 10 words to be used to fill those fields and in the order they are listed. ATM it will get 10 words randomly to fill those 10 text box fields.

4.  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',


I also tried changing "getRand" to "getOrder" as well as removing all the words Random. Didn't work either.
Title: Re: bookmarklet javascript - modfication help
Post by: Lefax32 on December 19, 2009, 11:38 PM
Actually, I found another one that will fill in the text fields, but only with one word for every field: testing. It also fills in an email addy but I don't need that.

Because I don't really need all the other functions in the previous one I posted is there a way to expand on this one, and add more words after testing, and use each word in order listed to fill in text fields.

One other thing, I'd like to fill in only the fields I have highlighted/selected (otherwise the lot).

javascript:function%20ROIoiW(){var%20i=0,j,A='testing',D,E,F=document.forms;while(i<F.length){E=F.elements;for(j=0;j<E.length;j++){D=E[j];if(D.type=='text'){D.value=(D.name.toUpperCase().indexOf('MAIL')!=-1)?A+'@example.com':A}}i++}}ROIoiW();void(null)

http://www.minezone.org/blog/2005/10/03/form-testing-bookmarklet/


Anyway, thanks if anyone can help me.