Greasemonkey脚本自动修改具有特定id值的URL

Rec*_*ent 2 url greasemonkey replace

我想将网址从"http://example.com/products/description/product123/index.html"更改为"http://example.com/f123456/products/description/product123/index.html".对于来自特定站点(只有一个)的每个链接,我需要像在示例中那样更改,在链接的基础之后添加代码,之后继续使用普通链接.

而更具体的,这里是我想用我的脚本来修改链接:http://www.f64.ro/http://www.f64.ro/f444618/的每一个环节,产品等.

我尝试了一些我找到的东西,但它不起作用,我不知道出了什么问题.

    // ==UserScript==
    // @name     F64.ro
    // @include  http://www.f64.ro/*
    // ==/UserScript==

    var targID      = "f444618/";
    var targLink    = document.querySelector ("#" + targID);
    targLink.href  += targID + "\/";
Run Code Online (Sandbox Code Playgroud)

谢谢!

wim*_*ica 6

stackoverflow上有类似的问题,例如如何在Greasemonkey中替换链接的目标?.

基于此,这是一个示例,它修改此站点上指向stackoverflow.com到www.stackoverflow.com的所有链接.

// ==UserScript==
// @name           changeurl test
// @namespace      https://stackoverflow.com/
// @description    test for https://stackoverflow.com/q/10267423/33499
// @include        https://stackoverflow.com/*
// ==/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('https://stackoverflow.com/',
                                          'http://www.stackoverflow.com/');
}
Run Code Online (Sandbox Code Playgroud)