我正在使用Greasemonkey将一些Javascript代码注入我想要自动刷新并自动从中下载有趣文件的网页。除了下载文件之外,我还有其他工作。
我找到了包含下载链接的有趣的表格单元格。如何按照该链接下载文件? (我将FF设置为始终将文件类型下载到我的驱动器上的目录中)。
表格单元格内容如下所示:
<td class="rowhead" align="center">
<a href="download.php/576537/O%26A%205-16-14.bin?passkey=5bb50ef2d99baebc29190291157a8b43">
[
<b>DL</b>
]
</a>
</td>
我无法编辑网页,因为它是一个公共论坛。
由于
几乎有效的ADDED代码:
// 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);
}
}
答案 0 :(得分:1)
您只需要从href中获取url并将其发送到位置对象
var link = document.querySelector('td a'); // set this selector to whatever you need
var href = link.getAttribute('href');
location.href = href;
对于任何/多个链接
var link = document.querySelectorAll('td a');
link.addEventListener("click",function(e){
var href = this.getAttribute('href');
location.href = href;
},false);