views:

56

answers:

3

I need a javascript bookmarklet which can click on a button. The thing is, there are 100+ buttons on the page all with the same value. The name is unique but quite long.
The full name of the element is something like :

actions[http://apps.facebook.com/frontierville/giftaccept.php?next=giftaccept.php&senderId=1%3A1325206719&gh=3a8bfdace76051752a9127d1f9b43872&gift=nails&timestamp=1285598414&ref=tab&key=29b15e06ed9d7c00a8870c955ab938cf%24%24cfH1PUUZ%217bZYhg8M-o-XQc%218HHRMcvvyhuf4d%21.64qEvlQe&src=request&aff=gift&crt=nails&signature=6dd3fa03fe88f98b6dcab4faf4c7da94]

The value of every button is Accept and Play.

So. Is there a way to have it click on the button with a specific URL in the name?

Here is the source of the info for one of the buttons (got this from chrome's inspect element feature):

<input value="Accept and Play" type="submit" name="actions[http://apps.facebook.com/onthefarm/giftaccept.php?senderId=1259413693&amp;amp;gift=mysterygift&amp;amp;timestamp=1285599906&amp;amp;ref=gift_accept_tab&amp;amp;key=78fcc7de3b36b8f9564262fab506893f%24%24ceK5RVRY61bZYhg8M-o-XQcyL%2CzHccEwEeuj4e-%21-dh0AD0A2AgyScd&amp;amp;signature=32db959ce43f8330cf8fd992fbd53a51&amp;amp;srcapp=FarmVille]"&gt;
A: 

Here's a rough example of what you might want to do.

var url = 'http://reallylong.facebook.url.from.your.example';
var searchName = 'actions[' + url + ']';

var items = document.getElementsByName(searchName);

if (items.length > 0) {
    var myButton = items[0];    // assuming the first item is the correct one
    myButton.click();   // programmatically click it
}

if the url is going to change every time, you can find someway to fill the url variable, and use that to generate the element name. This example assumes that element is the only one on the page with that exact name attribute.

This example is pretty rigid and may not work as your bookmarklet if you need to interact with it. What do the other elements look like? Would it be better to look for an element pointing to the giftaccept url or something like that? The script would have a lot more flexibility in a situation like that.

lincolnk
the url does change every time but I can make my script insert that url into this if it works. Here's what I tried...javascript:(function(){var url = 'REALLY LONG URL HERE'; var searchName - 'actions[' + url + ']';var items = document.getElementsByName(searchName);if (items.length > 0){var myButton = items[0];myButton.click();And that did nto work. anything you can see that I'm doing wrong?
Seatbelt99
A: 

This should do it...

javascript:var nam=prompt("Give me a URL to look for"); nam="actions["+nam.replace(/\&amp;/g, "&")+"]"; var els=document.getElementsByName(nam); if(els.length == 0) alert("Button not found"); else els[0].click();

It's based on getElementsByName, here it is all spelled out...

var nam = prompt("Give me a URL to look for");
nam = "actions[" + nam.replace(/\&amp;/g, "&") + "]";
var els = document.getElementsByName(nam);

if(els.length == 0)
  alert("Button not found");
else
  els[0].click();
Josh Stodola
then in the prompt I paste in the entire name attribute? I did that and it said not found even though I pasted it right from the source of the page.
Seatbelt99
Josh Stodola
@Seatbelt See latest update
Josh Stodola
Seatbelt99
@Seatbelt Hmmm. Try it without the call to `decodeURIComponent`.
Josh Stodola
That worked great!!! Now one more (hopefully minor) problem. Rather than doing a prompt can I just say name = 'URL' and then the rest of it?I tried this: javascript:var nam="LONG.URL.HERE"; nam="actions["+nam.replace(/\ var els=document.getElementsByName(nam); if(els.length == 0) alert("Button not found"); else els[0].click();and it just said button not found. But then I did exactly what you have above and pasted in the exact same URL and it worked fine. I can't tell you how much I appreciate all your help!! you are a life saver!
Seatbelt99
@Seatbelt Yes you can replace the prompt with whatever you'd like. Also, the way this community works: Vote up answers that are helpful (click up arrow) - anybody can do this. And to select an answer as the official "answer", click the checkmark below the down arrow - only the person asking the question can do this.
Josh Stodola
but when I put javascript:var nam="actualURL" at the beginning it does not find it. am I formatting it wrong somehow? Also, just selected this as the correct answer since it clearly is. I can't vote up yet due to not enough rep but I highly encourage everyone else to as Josh has been VERY helpful!
Seatbelt99
@Seatbelt Doing that instead of prompt should be the exact same thing, just make sure you have your semi-colon there to terminate the statement `var nam "ACTUAL_URL";`
Josh Stodola
I agree that it should be the exact same thing. lol. Its just not working for some reason. I do have the ; after var nam="Actual URL";This is just driving me crazy. lol.
Seatbelt99
@Seatbelt It is working here; you must be doing something else wrong
Josh Stodola
Seatbelt99
@Seatbelt Perhaps this is a browser URL length restriction at work?
Josh Stodola
I tried it in IE, Chrome and Firefox. The length of the whole javascript was a little under 600 characters.
Seatbelt99
A: 

For convenience and compatibility use JQuery selectors:

nameVal = 'actions[http://apps.facebook.com/onthefarm/giftaccept.php?senderId=1259413693&amp;amp;gift=mysterygift&amp;amp;timestamp=1285599906&amp;amp;ref=gift_accept_tab&amp;amp;key=78fcc7de3b36b8f9564262fab506893f%24%24ceK5RVRY61bZYhg8M-o-XQcyL%2CzHccEwEeuj4e-%21-dh0AD0A2AgyScd&amp;amp;signature=32db959ce43f8330cf8fd992fbd53a51&amp;amp;srcapp=FarmVille]'

$("input[value='Accept and Play'][name='" + nameVal + "']").click()
For details or you want to implement a similar one, just go through JQuery source code and you will find many useful hints for web developing using Javascript.
He did not say anything about jQuery. Bookmarklets are supposed to be minimalistic, not bloated.
Josh Stodola
I have never done anything with JQuery so I'm not sure what I would do with this information. Can it be put into a similar thing as a javascript bookmarklet?
Seatbelt99
@Seatbelt It is a third-party library; using it with a bookmarklet would be serious overkill
Josh Stodola