0

I'm using Tampermonkey, and I can't seem to get jQuery to work on it so this has to be Javacript only.

I'm trying to get a script to open (in a new window) the link of an item on a webpage. I've got a list of items I need to open, here is an example of those items:

<a class="market_listings" href="http://blabla.com/item1">...</a>
<a class="market_listings" href="http://blabla.com/item2">...</a>
<a class="market_listings" href="http://blabla.com/item3">...</a>

   etc.

As you can see, the item is defined by a class and an href. The class is not unique, but the href is.

I'm new to programming but this is my idea on how to open specifically those links from the page:

  1. The script gets elements with class="market_listings", just to narrow down the search of hrefs on the page.

  2. The script looks if the href of those elements corresponds with "http://blabla.com/item*"

  3. The script opens a new window with that href.

I have pretty much 0 experience with coding, but this is how I'd start it, considering I only want to open items 1 and 3:

function gethrefandopenwindow() {
         var items = document.getElementsByClassName('market_listings')

//don't know how to code from here

         if (*a href of items corresponds to*: 'http://blabla.com/item1')

            {
             *open new window to href of item1*
             }

         if (*a href of items corresponds to*: 'http://blabla.com/item3')

            {
             *open new window to href of item3*
             }



         else {
              *refresh the page and start over*
              }
}

As I said, I have barely programming experience, so I don't know if this is possible, and if it is and you're willing to help, please explain it to me like I'm a TOTAL idiot/newbie. :)

The only other way I could think of is this one: Javascript getElement by href? ; Except I don't know how to apply it in my situation due to my noobiness (and how to open a specific href out of the elements).

Anyway, I hope you can help me,

Thank you!

Community
  • 1
  • 1
bram
  • 99
  • 2
  • 11

2 Answers2

0

It looks like you had the right idea. This function might get you moving in the right direction.

function OpenHrefsInNewWindow() {
    //Get reference to your elements
    var items = document.getElementsByClassName("market_listings");

    for (var i = 0; i < items.length; i++) //Loop through your elements
    {
        //Verify that the href starts with http://blabla.com/item
        if (items[i].href.indexOf("http://blabla.com/item") === 0)
        {
            //If it does, open that URL in a new window.
            window.open(items[i].href, "_blank");
        }
    }
}

Another way to write it:

function OpenHrefsInNewWindow() {
    //Get reference to your elements
    var items = document.getElementsByClassName("market_listings");
    var i = 0;

    while(i < items.length) //Loop through your elements
    {
        //Verify that the href starts with http://blabla.com/item
        if (items[i].href.indexOf("http://blabla.com/item") === 0)
        {
            //If it does, open that URL in a new window.
            window.open(items[i].href, "_blank");
        }

        i++; //Increment i here.
    }
}
Tom Faltesek
  • 2,768
  • 1
  • 19
  • 30
  • Thanks a lot for this, your answer is very clear and I appreciate the fact you took your time to help me out. Even though it doesn't seem to work yet, you definitely helped me forward! <3 – bram Feb 15 '14 at 19:10
  • I'm glad to hear it helped. Would you mind marking this as the correct answer? – Tom Faltesek Feb 15 '14 at 19:57
  • That's exactly what I was going to do, but I wanted to fix my problem first. I put the var i=0 above because tampermonkey wants it that way, but tampermonkey shows an error here: for (i – bram Feb 15 '14 at 21:04
  • Thanks! If you must declare the variable (i) outside of the scope of the for loop, then you'll have to move the i++ part so it's the last line inside the loop. Finally, change the _for_ to a _while_. – Tom Faltesek Feb 15 '14 at 21:12
  • That fixed the error, Thanks! It doesn't seem to do it's job on the webpage because it doesn't open the new window, but yea at least the script is clean, thanks for that :) – bram Feb 15 '14 at 21:37
  • Might be a pop-up blocker issue. Try turning off your pop-up blocker. – Tom Faltesek Feb 15 '14 at 21:38
-1

Can retrive the url of link taking value of atribute and do ankthing later

Url = $(this).attr ('href')
Julien Roncaglia
  • 17,397
  • 4
  • 57
  • 75