修改油脂猴子中URL的脚本 [英] Script to modify a URL in greasemonkey
问题描述
我正在尝试更改以下网址:
I'm trying to change the following url:
http://www.example.net/?image=full&action=view&imageid=1361
进入
http://www.anothersite.com/download&id=1361&thumb=0
保留ID(在示例中为1361
)
while preserving the id (1361
in the example)
(将"example.net/?image=full&action=view&image"更改为"anothersite.com/download&",并在网址末尾添加& thum = 0")
(change 'example.net/?image=full&action=view&image' to 'anothersite.com/download&' and add '&thumb=0' at the end of the url)
如何为此编写GreaseMonkey脚本?
How do I write a GreaseMonkey script for that?
ps.我已经用谷歌搜索并复制了下面的代码.它可以正常工作,但问题是它也向其他链接添加了& thum = 0"(不仅是已替换"的链接)
ps. I've already googled it and copied the code below. It's working, but the problem is it adds '&thumb=0' to the other link too (not just the 'replaced' link(s))
// ==UserScript==
// @name whatever
// @namespace lii
// @description redirect to anothersite
// @include http://www.example.net/?image=full&action=view*
// @version 1
// @grant none
// ==/UserScript==
var links,thisLink;
links = document.evaluate("//a[@href]",
document,
null,
XPathResult.UNORDERED_NODE_SNAPSHOT_TYPE,
null);
for (var i=0;i<links.snapshotLength;i++) {
var thisLink = links.snapshotItem(i);
thisLink.href = thisLink.href.replace('http://www.example.net/?image=full&action=view&image',
'http://www.anothersite.com/download&') + "&thumb=0";
}
推荐答案
尝试一下:
thisLink.href = thisLink.href.replace(RegExp('http://www\\.example\\.net/\\?image=full&action=view&image(.*)'),
'http://www.anothersite.com/download&$1&thumb=0');
它应用正则表达式来匹配URL,并提取ID,并将其作为$1
插入到结果字符串中.
It applies regular expression to match the URL and extract the id and inserts it as $1
into the resulting string.
更多信息,请参见 MDN
这篇关于修改油脂猴子中URL的脚本的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!