删除网站上部分img href的脚本

时间:2019-06-02 05:12:44

标签: 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的末尾,您会注意到以下奇数字符串:
/ PRIMAP / resize / 700x-1>

我正在寻找一个脚本(Stylish,Tampermonkey等),我可以向PrimaGames.com申请该脚本,以便它自动删除src URL的该部分,从而依次显示相关的图像。

4 个答案:

答案 0 :(得分:0)

书签?

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

答案 1 :(得分:0)

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

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

答案 2 :(得分:0)

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

答案 3 :(得分:0)

您只需要执行以下代码,在这里,在替换src之后,我只用图像url替换了图像src

namespace ConsoleApp1
{
    internal class Program
    {
        private static void Main(string[] args)
        {
            S1 newItem = new S1()
            {
                Age = 11,
                Name = "John"
            };

            D1 oldItem = new D1()
            {
                Age = 10
            };

            //there is an item in a database which is of D1 type. This convertor receives an object S1 in order to update the D1 item.
            // the rule is that Sx updatates Dx (where x is 1,2,3,4,5...)
            Convertor<S1, D1> convertor = new Convertor<S1, D1>(newItem, oldItem);

            S2 newItem2 = new S2()
            {
                City = "London",
                Name = "Lynda"
            };

            D2 oldItem2 = new D2()
            {
                City = "Paris"
            };

            Convertor<S2, D2> convertor2 = new Convertor<S2, D2>(newItem2, oldItem2);
            Console.ReadKey();
        }
    }

    public interface ICopyable<T>
    {
        void CopyFrom(T other);
    }

    public abstract class SourceDomain
    {
        public string Name { get; set; }
    }


    public class S1 : SourceDomain
    {
        public int Age { get; set; }
    }

    public class S2 : SourceDomain
    {
        public string City { get; set; }
    }

    public class DestinationDomain { }
    public class D1 : DestinationDomain, ICopyable<S1>
    {
        public int Age { get; set; }

        public void CopyFrom(S1 other)
        {
            Console.WriteLine("oldItem.Age " + Age + " new Age; = " + other.Age);
            Age = other.Age;
            Console.WriteLine("oldItem.Age " + Age + " new Age; = " + other.Age);
        }
    }
    public class D2 : DestinationDomain, ICopyable<S2>
    {
        public string City { get; set; }

        public void CopyFrom(S2 other)
        {
            City = other.City;
            Console.WriteLine(" oldItem.City = City;");
        }
    }

    public class Convertor<X, Y> where X : SourceDomain  where Y : DestinationDomain, ICopyable<X>
    {
        protected X item;
        protected Y oldItem;

        public Convertor(X newObject, Y oldObject)
        {
            item = newObject;
            oldItem = oldObject;

            //here I want to call, depending of X type, the proper method, not the base one.
            oldItem.CopyFrom(item);
            Console.WriteLine(item);
        }
    }
}

我在这里更改:

/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

然后从相对路径获取File的url,这就是为什么我使用文件路径

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

https://primagames.com/,则相对路径均值代码将更改为:

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

它将为您工作。

仅从图像URL中删除多余的内容,即“ / PRIMAP / resize / 700x-1%3E”,您可以执行以下代码

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