0

I am using Greasemonkey to inject some Javascript code into a webpage that I want to auto-refresh and automatically download interesting files from. I have everything working other than the downloading of the file.

I have found the interesting table cell that contains the download link. How do I follow that link to download the file? (I have FF set to always download the type of file to a directory on my drive).

The table cell contents look like:

<td class="rowhead" align="center">
<a href="download.php/576537/O%26A%205-16-14.bin?passkey=5bb50ef2d99baebc29190291157a8b43">
[
<b>DL</b>
]  
</a>
</td>

I have no way of editing the webpage as it is a public forum.

Thanks

ADDED Code which almost works:

// skip first as it isnt valid
var rows = mainTable[0].tBodies[0].rows;
for (var row = 1; row < rows.length; row++)
{
    var cells = rows[row].cells;
    var Title = cells[1].innerHTML.toLowerCase();

    if (IsTitleMatchAnyRule(Rules, Title))
    {
        // this shows me the link which it will attempt to download
        alert(cells[3].querySelector('a '));

        //This works for a single link in the whole page
        //location.href = cells[3].querySelector('a ');

        cells[3].addEventListener("click",function(e){
            var link = this.querySelector('a ');

            // I never see this alert - commenting out doesnt download the link either
            alert(link);

            location.href = link;
        },false); 
    }
}
allanmb
  • 321
  • 3
  • 14

1 Answers1

1

You just need to grab the url out of the href and send it to the location object

var link = document.querySelector('td a'); // set this selector to whatever you need
var href = link.getAttribute('href');
location.href = href;

For any/multiple links

var link = document.querySelectorAll('td a');
link.addEventListener("click",function(e){
    var href = this.getAttribute('href');
    location.href = href;
},false);
Shan Robertson
  • 2,742
  • 3
  • 25
  • 43
  • Thanks, that seems to work if there is one link in the page I want to download, however it doesn't seem to work if there are more than one :-( Any ideas how to make it work with more than one link? – allanmb May 16 '14 at 19:25
  • @allanmb I have updated my answer. the `link` var now selects all of the matching elements... in this case, all `a` tags within a `td`. then on click, it grabs that link's `href` and sends the location like the first example. – Shan Robertson May 16 '14 at 21:47
  • I'm not sure how to make this work. I have shown my code below. Basically I have to parse each row individually to see if matches my rules. If so, then I want to download the link in the 4th cell. – allanmb May 17 '14 at 08:11
  • Code added to original question as I can't seem to add code to a comment – allanmb May 17 '14 at 08:13
  • Can you tell me how to edit my code to work for multiple links? Thanks – allanmb May 20 '14 at 12:55
  • I got around the multiple links issue by remembering the last location parsed and refreshing the page. This way only requires one download per page load which works a charm. – allanmb May 25 '14 at 16:24