ATTENTION: You are viewing a page formatted for mobile devices; to view the full web page, click HERE.

Main Area and Open Discussion > Living Room

bookmarklet javascript - modfication help

(1/2) > >>

Lefax32:
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()
--- End quote ---

housetier:
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 ---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;            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;            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;            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 {                        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;    },     getRand: function (count) {        return Math.round(count * Math.random());    }}; auto.fillerup()

Lefax32:
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). 

housetier:
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...

Lefax32:
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.

Navigation

[0] Message Index

[#] Next page

Go to full version