【问题标题】:Greasemonkey script to modify automatically an URL with a specific id valueGreasemonkey 脚本自动修改具有特定 id 值的 URL
【发布时间】:2012-04-22 11:10:14
【问题描述】:

我想将网址从“http://example.com/products/description/product123/index.html”更改为“http://example.com/f123456/products/description/product123/index.html”。 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 + "\/";

谢谢!

【问题讨论】:

  • 您知道这只会影响您的浏览器吗?其他任何人都不会受到它的影响。
  • 是的,我意识到这一点,但我只想能够复制修改后的 URL 并将其粘贴到某人或某处,如果可能的话,而不是手动修改每个链接。谢谢你的回答。

标签: url replace greasemonkey


【解决方案1】:

stackoverflow上有类似的问题,例如How can you replace a link's target in 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/');
}

【讨论】:

  • XPath 是要走的路,虽然它的开发似乎没有太多考虑......
【解决方案2】:

如果是固定数字

// ==UserScript==
// @name           TESTE 2
// @namespace      TESTE 2
// @description    TESTE 2
// @include        http*://*stackoverflow*
// ==/UserScript==

for (var i=0,link; (link=document.links[i]); i++) {
    linkID = '/f444618'
    link.href = link.href.replace(location.hostname, location.hostname+linkID);
}

如果数字从链接到链接发生变化,只需获取当前链接的ID并替换linkID = 'f444618'

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2018-09-14
    • 2015-10-24
    • 1970-01-01
    • 1970-01-01
    • 2011-07-30
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多