根据一组规则(Greasekit / Javascript)对链接进行即时修改 [英] On-the-fly modifications of the links according to a set of rules (Greasekit / Javascript)

查看:85
本文介绍了根据一组规则(Greasekit / Javascript)对链接进行即时修改的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个包含大量条目的HTML页面:

I have an HTML page with loads of entries like this:

<a href="https://www.example.co.uk/gp/wine/product?ie=UTF8&amp;asin=123456789&amp;tab=UK_Default" class="PrmryBtnMed"

我想替换所有这些链接,所以它们是:

I want to replace all this links so they instead are:

https://www.example.co.uk/gp/wine/order?ie=UTF8&asin=1233456789

所以,这是一个非常复杂的搜索和替换。这些是人类的说明:

So, it's quite a complicated search and replace. These are the instructions for a human:


  1. 查看URL。只记下'asin ='后面的数字。 (忘记之前的一切以及之后的所有事情)

  2. 然后,使用此ASIN形成一个新的URL。它总是这样开始:
    https://www.example.co.uk/gp/wine/order?ie=UTF8&asin=

  1. Look at the URL. Only make a note of the number after 'asin='. (Forget everything before that and everything after that)
  2. Then, form a new URL, using this ASIN. It will ALWAYS start like this: https://www.example.co.uk/gp/wine/order?ie=UTF8&asin=

数字卡在最后形成:

https://www.example.co.uk/gp/wine/order?ie=UTF8&asin=123456789

请注意


  • 而不是修改现有按钮,添加新按钮也是可以接受的原始按钮附近的[或链接]

  • 原始链接和新链接都指向同一个域

  • 我在SSB上使用Greasekit FluidApp,但我可以在FireFox上切换到Greasemonkey。

我刚观看了40个JavaScript教程视频 - 这个语言很难!这似乎非常困难。我非常感谢任何帮助/指针。

I've just watched 40 JavaScript tutorial videos - man this language is hard! This seems extremely difficult. I would hugely appreciate any help/pointers.

推荐答案

这样的事可能有效:

// the new base url
var base = ' https://www.example.co.uk/gp/wine/order?ie=UTF8&asin=';
// all the links with className 'PrmryBtnMed'
var links  = document.getElementsByTagName('a');

for(var i = 0;i < links.length;i++){
    // check each link for the 'asin' value
    var result = /asin=([\d\w]+)/.exec(links[i].getAttribute('href'));
    if(result){
        // make a new url using the 'base' and the 'asin' value
        links[i].setAttribute('href', base+result[1]);
    }
}

演示: http://jsfiddle.net/louisbros/L8ePL/

这篇关于根据一组规则(Greasekit / Javascript)对链接进行即时修改的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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