【问题标题】:Script to remove part of img href on a site删除网站上部分 img href 的脚本
【发布时间】:2019-06-02 05:12:44
【问题描述】:

我在 PrimaGames.com 上启动了游戏指南。文字一切都很好,但图像已经破坏了src。 这是一个例子:

<img src="/media/files/eguide_assets/final-fantasy-xii-the-zodiac-age-eguide/085-105_walkthrough-web-resources/image/wi01.png/PRIMAP/resize/700x-1>">  

在 src URL 的末尾,您会注意到这个奇怪的字符串:
/PRIMAP/resize/700x-1>

我希望设置一个脚本(Stylish、Tampermonkey 等),我可以将其应用于 PrimaGames.com,以便它自动删除 src URL 的该部分,从而显示相关图像。

【问题讨论】:

    标签: javascript jquery tampermonkey stylish


    【解决方案1】:

    书签?

    javascript:(function() { [...document.images].forEach(img => { 
      const src = img.src.split("/PRIMAP"); 
      if (src.length >=2) img.src=src[0]; })})()
    

    文件扩展名后未知内容的替代方案 - 假设您只对 pngs 感兴趣

    javascript:(function() { [...document.images].forEach(img => { 
      const end = img.src.lastIndexOf(".png/");
      if (end) img.src=img.src.substring(0,end+4); })})()
    

    【讨论】:

    • 我试过了,它作为书签很好用!感谢您的想法!
    【解决方案2】:

    这是一个相当标准的src重写任务。

    这是一个完整的 Tampermonkey / Violentmonkey 脚本,它应该在该网站上适用于静态和动态图像

    // ==UserScript==
    // @name     _Remove post file junk from image sources
    // @match    *://primagames.com/*
    // @noframes
    // @require  https://ajax.googleapis.com/ajax/libs/jquery/2.1.0/jquery.min.js
    // @require  https://gist.github.com/raw/2625891/waitForKeyElements.js
    // @grant    GM_addStyle
    // @grant    GM.getValue
    // ==/UserScript==
    // @grant    none
    //- The @grant directives are needed to restore the proper sandbox.
    /* global waitForKeyElements */
    
    waitForKeyElements ("img", fixImgSource);
    
    function fixImgSource (jNode) {
        var oldSrc = jNode.attr ("src");
        //-- Does the src have junk after the file suffix?
        if (/\.(png|jpg|gif)./i.test (oldSrc) ) {
            let newSrc = oldSrc.replace (/\.(png|jpg|gif).+$/i, ".$1");
            jNode.attr ("src", newSrc);
        }
    }
    

    【讨论】:

    • 谢谢!这在 Tampermonkey 中效果很好!
    【解决方案3】:

    您需要找到实际图像结束的索引并创建一个子字符串直到该索引。

    function removeTag(image) {
        var src = image.src.toString();
        var imgFormats = ['jpg', 'jpeg', 'gif', 'png'];
        var idx;
        imgFormats.forEach(fmt => {
            if (src.lastIndexOf(fmt) > -1) {
                idx = src.lastIndexOf(fmt) + fmt.length;
            }
        })
        if (typeof idx !== 'undefined') {
            image.src = src.substr(0, idx + 1);
        }
    }
    

    如果你知道每个额外的字符串都以'/PRIMAP'开头,那么你可以使用上面提供的解决方案。

    【讨论】:

    • 这是不必要的image.src.toString();typeof idx !== 'undefined' 也是矫枉过正。 if (idx) 就够了。我看不出这是如何以任何优雅的方式回答这个问题的。
    【解决方案4】:

    你只需要执行下面的代码,这里我只是用图片url替换图片src,替换src后

    (function(){
      var a = document.getElementsByTagName('img');
      for(e of a){
        let t = e.src;
        e.src = t.replace(/(http|https|file:\/\/)?(\/.*\.(?:png|jpg))(.*)/i, '$2');
    }
    }())
    

    我正在改变:

    /media/files/eguide_assets/final-fantasy-xii-the-zodiac-age-eguide/085-105_walkthrough-web-resources/image/wi01.png/PRIMAP/resize/700x-1%3E

    /media/files/eguide_assets/final-fantasy-xii-the-zodiac-age-eguide/085-105_walkthrough-web-resources/image/wi01.png

    然后 url 从相对路径获取文件,这就是我使用文件路径的原因,

    如果此图像在您的相对文件夹中,那么您将获得图像,否则您需要添加带有相对路径的基本 url

    https://primagames.com/ 则相对路径意味着代码将更改为:

    (function(){
          var a = document.getElementsByTagName('img');
          for(e of a){
            let t = e.src;
            e.src = `https://primagames.com${t.replace(/(http|https|file:\/\/)?(\/.*\.(?:png|jpg))(.*)/i, '$2')}`;
        }
        }());
    

    它会为你工作。

    对于仅从图像 url 中删除额外的东西,即“/PRIMAP/resize/700x-1%3E”,您可以执行以下代码

    (function(){
        var a = document.getElementsByTagName('img');
        for(e of a){
           let t = e.src;
           e.src = t.replace(/([\w-]+\.(jpg|png|txt))(.*)/i, '$1');
        }
    }())
    

    【讨论】:

    • 为什么?为什么复杂的正则表达式在他想要的只是删除图像网址之后的内容时删除和添加内容。你的正则表达式对我没有意义
    • 我这样做只是为了获取图像的精确 url,这里我只获取图像的相对 url,如果图像不在文件夹中,那么他可以添加带有相对路径的基本 url,以及额外的部分已经删除,因为我在这里不拿 3 美元,所以它的问题是什么。
    • 因为我没有项目中的图像或从外部资源获取这是这样做的主要原因。
    • 请查看最后的代码,即仅删除图片 url 多余的部分。
    • 您正在使用“PRIMAP”为拆分网址编写代码,不要为任何单词给出单词,所以如果有其他单词,这将不起作用。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2023-03-28
    • 1970-01-01
    • 2019-02-16
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-10-02
    相关资源
    最近更新 更多