1

I am using GreaseKit (so JavaScript)

Step #1 Look for string & Step #2 Click button

When this HTML page loads, if either of the following :

  • Only 3 Left or
  • Only 2 Left or
  • Only 1 Left

appears in a table cell AND a button with class="PrmryBtnMed" is present in that very same cell, then click the button using el.click();

Can we just look inside the <tr> tags?

If not, an alternative approach might be to just click the first button that appears immediately underneath, or after the desired string.

update: thanks to @Bergi, it seems we can just use: var LookInCells = document.getElementsByTagName('tr');

About that button: which attribute to look for?

the bit of html looks like: <a href="/gp/wine/taste-next-123/" class="PrmryBtnMed" id = "product-B00123456"><span>Send me this item</span></a>

So, thinking about attributes of the button

These will always be the same:

  • class="PrmryBtnMed"
  • <span>Send me this item</span></a>

These will always vary, so might as well ignore:

  • where the link points to (e.g. a href="/gp/wine/taste-next/")
  • the id will always be 'id = "product-B00123456"'
Community
  • 1
  • 1
Hiya
  • 175
  • 1
  • 2
  • 9
  • You appear to have a good idea of what's to be done. What is your problem? What's not working at the moment? What code have you written, what error messages are you facing? – David Hedlund Jun 20 '13 at 05:24
  • @DavidHedlund Hey, I'm stuck on the part about searching for a string, going about it cell by cell. Once the string is found, I want to click the button in that cell. How would I go about approaching that, using code, please? (Well, actually, the cells to be looked at will always be in [the last column](https://dl.dropboxusercontent.com/u/5546881/stack-overflow-hiya-alreet/only-3-2-1-left-send-me-item.html) but I figure it might be easier to approach it in a generic fashion. – Hiya Jun 20 '13 at 15:05
  • The other thing I'm stuck on, is, using code, how to look for the string. Should I use an array like this: ['Only 3 Left', 'Only 2 Left', 'Only 1 Left']. _Or, alternatively_, could I say look for 'Only x Left' where X is an integer that is more than zero, but less than four? – Hiya Jun 20 '13 at 15:08
  • And because it's [a very large HTML document](https://dl.dropboxusercontent.com/u/5546881/stack-overflow-hiya-alreet/only-3-2-1-left-send-me-item.html) perhaps I could tell the code to only bother looking inside `` and `` tags. Maybe it's not even necessary, but it might be more elegant/efficient. – Hiya Jun 20 '13 at 15:16
  • @John: You won't be "looking between tags". You have the HTML already parsed in a DOM, and you can just `document.getElementsByTagName("tr")` to get all `` elements – Bergi Jun 20 '13 at 17:28
  • @Bergi okay, **thanks**, so I could start with, say `var LookInCells = document.getElementsByTagName('tr');` ? and then use a `for` to search through each one? – Hiya Jun 20 '13 at 17:33
  • @John: Yes, exactly. Of course you could start right away with a more complex selector, like `document.getQuerySelectorAll("td:last-child a.PrmryBtnMed")` and then navigate the DOM a bit differently… – Bergi Jun 20 '13 at 17:37
  • @Bergi Ooh, that looks good. I shall start with that! When it comes to writing the first part of the `for`, i'm stuck. Could I use `for(var i = 0;i < LookInCells.length;i++)` – Hiya Jun 20 '13 at 17:41
  • @John: Try it! (hint: yes) Maybe `console.log(LookInCells)` will also help you to get a feeling of available properties… – Bergi Jun 20 '13 at 17:47
  • @Bergi Okay, I've tried to answer, on this page. Please could you take a look at it. – Hiya Jun 20 '13 at 18:01

1 Answers1

0

I'm having a go at this based on the kind guidance of @Bergi!

var LookInCells = document.getElementsByTagName('tr');
var MyString =  ['Only 3 Left', 'Only 2 Left', 'Only 1 Left']
for(var i = 0;i < LookInCells.length;i++){
}

var inPage = document.documentElement.innerHTML.indexOf(MyString) > 0,
// el = document.querySelector(".PrmryBtnMed");
el = document.getQuerySelectorAll("td:last-child a.PrmryBtnMed")    
if (inPage && el) el.click();
Hiya
  • 175
  • 1
  • 2
  • 9