1

这是一个特殊的 GreaseKit 脚本,它成功地更改了页面上的某些 URL。它非常棒,我喜欢这里写它的人。但我想对其进行调整,以便在单击链接时在新窗口中打开

// the new base url
var base = ' https://www.example.co.uk/wine/order?ie=UTF8&asin=';
// all the links with className 'PrmryBtnMed'
var links  = document.getElementsByTagName('a');

for(var i = 0;i < links.length;i++){
// check each link for the 'asin' value
var result = /asin=([\d\w]+)/.exec(links[i].getAttribute('href'));
if(result){
    // make a new url using the 'base' and the 'asin' value
    links[i].setAttribute('href', base+result[1]);
}
}

我对如何解决这个问题的猜测:

有人可以告诉我如何实现这一目标吗?我的猜测是在link.target="_blank"某个地方添加,但我不确定确切的位置。

4

2 回答 2

2

在这里面:

if(result){
    // make a new url using the 'base' and the 'asin' value
    links[i].setAttribute('href', base+result[1]);
}

只需添加另一个属性:

links[i].setAttribute('target', '_blank');
于 2013-05-22T19:23:38.303 回答
1

那是正确的。只需在设置其他属性的位置设置:

if(result){
    // make a new url using the 'base' and the 'asin' value
    links[i].setAttribute('href', base+result[1]);
    links[i].setAttribute('target', '_blank'); // open in a new window
}
于 2013-05-22T19:24:40.287 回答