如何在Greasemonkey中替换链接的目标? [英] How can you replace a link's target in Greasemonkey?

查看:88
本文介绍了如何在Greasemonkey中替换链接的目标?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试在Greasemonkey中编写一个脚本,用其他东西替换链接的目标,但是由于我有限的Javascript知识,我真的不知道如何做到这一点。

I'm trying to write a script in Greasemonkey that will replace a link's target with something else, but with my limited Javascript knowledge I don't really know how to do this.

基本上我正在尝试查找包含特定字符串的所有链接(例如:// a [contains(@ href,'xx')]),以及要么用另一个链接替换它们,要么添加一些东西(用'zyx987.com'替换'abc123.com'或用'abc123.com/folder'替换'abc123.com')。

Basically I'm trying to find all links containing a certain string of characters (ex: //a[contains(@href, 'xx')] ), and either replace them with another link, or append something to them (replacing 'abc123.com' with 'zyx987.com' or 'abc123.com' with 'abc123.com/folder').

如果你能指出我正确的道路,我会非常感激。

If you could point me on the right path I'd greatly appreciate it.

编辑:这是工作代码以防将来某人有同样的问题:

edit: This is working code in case someone has the same question in the future:


var links,thisLink;
links = document.evaluate("//a[contains(@href, 'roarrr')]",
    document,
    null,
    XPathResult.UNORDERED_NODE_SNAPSHOT_TYPE,
    null);
for (var i=0;i<links.snapshotLength;i++) {
    var thisLink = links.snapshotItem(i);
    thisLink.href += 'test.html';
}


推荐答案

var links = document.evaluate("//a[contains(@href, 'roarrr')]", document, null, 
XPathResult.UNORDERED_NODE_SNAPSHOT_TYPE, null); 
for (var i=0; i < links.snapshotLength; i++) 
{ 
  var thisLink = links.snapshotItem(i); 
  thisLink.href += 'test.html'; 
} 

这篇关于如何在Greasemonkey中替换链接的目标?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆