Javascript 书签,用于单击按唯一名称找到的按钮

发布于 2024-09-24 20:12:08 字数 1093 浏览 4 评论 0原文

我需要一个可以单击按钮的 JavaScript 书签。问题是,页面上有 100 多个按钮,它们的值都相同。这个名字很独特,但很长。
该元素的全名类似于:

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]

每个按钮的值都是 Accept 和 Play。

所以。有没有办法让它点击名称中带有特定 URL 的按钮?

以下是其中一个按钮的信息来源(从 chrome 的检查元素功能中获取):

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

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&gift=mysterygift&timestamp=1285599906&ref=gift_accept_tab&key=78fcc7de3b36b8f9564262fab506893f%24%24ceK5RVRY61bZYhg8M-o-XQcyL%2CzHccEwEeuj4e-%21-dh0AD0A2AgyScd&signature=32db959ce43f8330cf8fd992fbd53a51&srcapp=FarmVille]">

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

原来是傀儡 2024-10-01 20:12:08

这应该可以做到...

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

它基于 getElementsByName,这里已经全部写出来了...

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

if(els.length == 0)
  alert("Button not found");
else
  els[0].click();

This should do it...

javascript:var nam=prompt("Give me a URL to look for"); nam="actions["+nam.replace(/\&/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(/\&/g, "&") + "]";
var els = document.getElementsByName(nam);

if(els.length == 0)
  alert("Button not found");
else
  els[0].click();
挥剑断情 2024-10-01 20:12:08

这是您可能想要执行的操作的粗略示例。

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
}

如果 url 每次都会更改,您可以找到某种方法来填充 url 变量,并使用它来生成元素名称。此示例假设该元素是页面上唯一具有该确切 name 属性的元素。

这个示例非常严格,如果您需要与之交互,可能无法用作您的小书签。其他元素是什么样子的?寻找指向giftaccept url 或类似内容的元素会更好吗?在这种情况下,脚本会有更大的灵活性。

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.

只有一腔孤勇 2024-10-01 20:12:08

为了方便和兼容性,使用 JQuery 选择器:

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

$("input[value='Accept and Play'][name='" + nameVal + "']").click()

For convenience and compatibility use JQuery selectors:

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

$("input[value='Accept and Play'][name='" + nameVal + "']").click()
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文