Javascript 删除站点上部分img href的脚本

Javascript 删除站点上部分img href的脚本,javascript,jquery,tampermonkey,stylish,Javascript,Jquery,Tampermonkey,Stylish,我在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的末尾,您会注意到这个奇怪的字符串: /PRI

我在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>

我正在寻找一个脚本(Style、Tampermonkey等),我可以将其应用到PrimaGames.com,以便它自动删除src URL的该部分,从而显示相关的图像。

Bookmarklet

javascript:(function() { [...document.images].forEach(img => { 
  const src = img.src.split("/PRIMAP"); 
  if (src.length >=2) img.src=src[0]; })})()
文件扩展名后未知内容的替代方案-这里假设您只对PNG感兴趣

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

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

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”开头,那么您可以使用上面提供的解决方案。

这是一个相当标准的
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);
    }
}

您只需要执行下面的代码,这里我只是用图像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');
}
}())
我在这里改变:

/媒体/文件/eguide_资产/最终幻想十二生肖时代eguide/085-105_演练-web-resources/image/wi01.png/PRIMAP/resize/700x-1%3E

/媒体/文件/eguide_资产/最终幻想十二生肖时代eguide/085-105_演练-web-resources/image/wi01.png

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

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

此时,相对路径平均值代码将更改为:

(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');
    }
}())

谢谢!这在Tampermonkey中运行得很好!我试过了,它作为一个Bookmarklet运行得很好!谢谢你的想法!这是不必要的
image.src.toString();
typeof idx!=='undefined'
也太过分了。
if(idx)
就足够了。我看不出这是如何以优雅的方式回答这个问题的。为什么?为什么复杂的正则表达式要删除和添加东西,而他只想删除图像url后面的内容。你的正则表达式对meI来说毫无意义,我只是为了获取图像的extact url,这里我只获取图像的相对url,如果图像不在t中的话在这里文件夹,然后他可以添加基本url和相对路径,额外的部分已经删除,因为我没有在这里接受$3,所以它的问题是什么。因为我没有在那里的项目中的图像或从外部资源,这是主要的原因做这件事。请查看上一个代码,这是只删除图像url额外的部分。你做的代码使用“PRIMAP”拆分url,不为任何单词指定单词,所以如果有其他单词,则该操作将不起作用。