문제

I really need to know how I can autofill text boxes on a web page.

What I really want to achieve is the following:

1) Go to http://show.websudoku.com

2) Replace all the empty cells with a 0 (zero).

Is that possible?

도움이 되었습니까?

해결책

To fill the empty spaces of the Sudoku grid at http://show.websudoku.com with zero's, here is some JavaScript to do that. It is formatted for use as a "Bookmarklet":

javascript:(function(){var x,k,f,j,r;x=document.forms;for(k=0;k<x.length;++k){f=x[k];for(j=0;j<f.length;++j){r=(f[j].className.toLowerCase()+f[j].type.toLowerCase()+f[j].value);if(r=="d0text"){f[j].value="0";}else if(r=="d0text0"){f[j].value="";}}}})();

The setup:

  1. Create a new Bookmark/Favorite. For now, the URL for the favorite can be anything. An easy way to do this is to drag ANY link/url from the browser address bar, or any web-page link, to the "Favorites Bar" or to the Bookmarks/Favorites sidebar.
  2. Select the new favorite, and rename it to any name you like.
  3. Copy the JavaScript code from above to the clipboard. It must remain as 1 continuous single line, and it must begin with "javascript:(" and end with ")();"
  4. Edit the properties of the new favorite.
  5. Remove the "URL" that is currently in the favorite and replace it by pasting in the JavaScript code from above, into the "URL" text field for the favorite, then save the changes.

To use the bookmarklet:

  1. From the browser, navigate to http://show.websudoku.com as you normally would.
  2. Click the new favorite (Bookmarklet) that you just edited.

All empty spaces in the Sudoku grid will be filled with 0's. Click the new favorite (Bookmarklet) again, and the 0's will be removed leaving empty spaces once again.

Here is what the Javascript code looks like expanded, with indents:

javascript:(function(){
  var x,k,f,j,r;
  x=document.forms;
  for(k=0;k<x.length;++k){
    f=x[k];
    for(j=0;j<f.length;++j){
      r=(f[j].className.toLowerCase()+f[j].type.toLowerCase()+f[j].value);
      if(r=="d0text"){
        f[j].value="0";
      }
      else if(r=="d0text0"){
        f[j].value="";
      }
    }
  }
}
)();


* Spoiler alert *

In case you want to "cheat", the JavaScript here will "solve" the Sudoku:

javascript:(function(){var x,k,f,j,ecl,etl,en,ev,s,e,c,d,dl,dr,n;x=document.forms;for(k=0;k<x.length;++k){f=x[k];for(j=0;j<f.length;++j){e=f[j];r=(e.name.toLowerCase());if(r=="cheat"){c=e.value;break;}}for(j=0;j<f.length;++j){e=f[j];ecl=e.className.toLowerCase();etl=e.type.toLowerCase();en=e.name;ev=e.value;if(etl=="text"){if(ecl=="d0"){dr=en.substr(en.length-1,1);dl=en.substr(en.length-2,1);d=(((Number(dr)-1)*9)+Number(dl))-1;n=c.substr(d,1);if(ev.length==0){e.value=n;}else{e.value="";}}}}}})();

Setup and use is the same as described above.

While it's not much fun to solve it like that (OK, maybe it's a little fun the first couple times), and definitely not challenging, if you are in a real-real-real hurry, you can solve it in 1 click.


Note: I have only tested these 2 bookmarklets with IE9.

라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top