简体   繁体   中英

Retrieving element with no name or id

I am trying to write some code to accomplish something very simple. I have never worked with a PayPal button before, which is causing some difficulty.

I have been able to simulate a button click before, using document.getElementById or document.getElementsByName(), however, the form I am trying to submit above does not have a name, or an id, so I do not know how to refer to it in my code.

I am trying to write a short chrome extension using Javascript that will find the paypal form/button on the page (that I do not own or have control over), and submit it without me having to click it. Any guidance/links would be much appreciated.

Looks like your button has a name, so you can use .getElementsByName()

var btn = document.getElementsByname('submit')[0]

If you have access to jQuery or you can add jQuery to your project then

var btn = jQuery('input[name="submit"]')

This would be MUCH easier with jQuery, but with native js, you could do:

function getPaypalForms()
{
  var matchingElements = [];
  var allForms = document.getElementsByTagName('form');
  for (var i = 0; i < allForms.length; i++)
  {
    if (allForms[i].getAttribute('action') == 'https://www.paypal.com/cgi-bin/webscr')
    {
      // Element exists with attribute. Add to array.
      matchingElements.push(allElements[i]);
    }
  }
  return matchingElements;
}

There are many ways to edit this function above to suit your purposes even better. You could edit it to collect just the submit buttons from these forms, or refactor it to accept a string to match an attr, and value. However, like I said, this would be much easier using jquery:

$('form[action="https://www.paypal.com/cgi-bin/webscr"]');

Based on your update, first off: don't do this, unless you're trying to swindle people out of money. In which case, don't do this =) (I'm not a lawyer, but I'm pretty sure paypal can go "well you didn't click the button, you automated it. And now someone's site made you spend $500 that you didn't want to. tough"). That said: document.getElementsByTagName("form"), then filter on getAttribute("action").

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM