Question

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!

Was it helpful?

Solution

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.
    }
}

OTHER TIPS

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

Url = $(this).attr ('href')
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top