替换Greasemonkey中URL的一部分

约翰

我正在尝试使用Greasemonkey脚本替换url的一部分,但是却很难实现我想做的事情。

原始Urls就像:

http://x1.example.to/images/thumb/50/157/1571552600.jpg
http://x2.example.to/images/thumb/50/120/1201859695.jpg
http://x3.example.to/images/thumb/50/210/2109983330.jpg

我要实现的是:

http://example.to/images/full/50/157/1571552600.jpg
http://example.to/images/full/50/120/1201859695.jpg
http://example.to/images/full/50/210/2109983330.jpg

我只想完全替换thumb从原始URL完全切下x1 .example.to,x2 .example.to,x3 .example.to,x4 .example.to等。因此新的URL将开始喜欢example.to/images/full/

我该如何实现?

我从此答案中找到了Greasemonkey脚本,并且确实尝试过解决但失败了。

这是我到目前为止所做的。

// ==UserScript==
// @name           Example Images Fixer
// @namespace      Example
// @description    Fixes image galleries
// @include        http://*.example.to/*
// ==/UserScript==

var links = document.getElementsByTagName("a"); //array
var regex = /^(http:\/\/)([^\.]+)(\.example\.to\/images\/thumb/\)(.+)$/i;
for (var i=0,imax=links.length; i<imax; i++) {
   links[i].href = links[i].href.replace(regex,"$4full/$5");
}

有什么帮助吗?

hjpotter92

您忘记将http://零件放入替换URL中:

/^(https?:\/\/)[^.]+\.(example\.to\/images\/)thumb\/(.+)$/i

接着:

.replace(regex, "$1$2full/$3");

您可以在此处查看结果

本文收集自互联网,转载请注明来源。

如有侵权,请联系 [email protected] 删除。

编辑于
0

我来说两句

0 条评论
登录 后参与评论

相关文章