How to relink/delink image URLs to point to the full image?

后端 未结 2 1192
广开言路
广开言路 2020-12-22 04:58

I\'m trying to change the URL\'s of a webpage on the fly using Greasemonkey.

The target page has links like:



        
相关标签:
2条回答
  • 2020-12-22 05:06

    I think you want something like this,

    > var s = '<a name="217258323" href="http://foobar.net/photo/217258323/?pgid=&amp;gid=4933418&amp;page=0">\n   <img style="border:1px solid #fff;padding:5px;background:#fff;"\n     height="120" width="160" alt="Gallery pic 1 of 20 pics" border="0"\n     src="http://i.foo.net/images/thumb/52/217/217258323.jpg">\n </a>';
    undefined
    > var f = s.replace(/(href=")[^"]*([\s\S]*?)(src=")([^"]*)([\S\s]+?<\/a>)/g, '$1$4$2$3$4$5');
    undefined
    > var result = f.replace(/thumb(?=.*thumb)/, 'full');
    undefined
    > result
    '<a name="217258323" href="http://i.foo.net/images/thumb/52/217/217258323.jpg">\n   <img style="border:1px solid #fff;padding:5px;background:#fff;"\n     height="120" width="160" alt="Gallery pic 1 of 20 pics" border="0"\n     src="http://i.foo.net/images/thumb/52/217/217258323.jpg">\n </a>'
    
    0 讨论(0)
  • 2020-12-22 05:16

    This is a standard image relink/delink problem, and you can probably find several premade userscripts for just about any site.

    But don't try to do this with just regex, that way lies madness (and broken scripts).

    Here's how to relink your example using DOM methods:

    var thumbImgs = document.querySelectorAll ("a > img[src*='/thumb/']");
    
    for (var J = thumbImgs.length-1;  J >= 0;  --J) {
        var img     = thumbImgs[J];
        var lnkTarg = img.src.replace (/\/thumb\//, "/full/");
        var link    = img.parentNode;
        link.href   = lnkTarg;
    }
    
    0 讨论(0)
提交回复
热议问题