Google chrome extension 更改a<;IMG>;在加载映像之前,在chrome扩展中使用SRC

Google chrome extension 更改a<;IMG>;在加载映像之前,在chrome扩展中使用SRC,google-chrome-extension,Google Chrome Extension,我想编写一个Chrome扩展,在加载图像之前更改img元素的src=值 我有更改图像的代码,但它总是在img加载后执行。在加载图像之前,如何在SRC中更改url 看看或。我不久前遇到过这个问题,我的解决方案基本上是相同的(用'blah='替换html字符串中的src属性,然后添加我自己的src)。正如链接中所说,如果使用jquery,请不要使用$(html),因为它一经解析就会获取图像。查看或。我不久前遇到过这个问题,我的解决方案基本上是相同的(用'blah='替换html字符串中的src属性,

我想编写一个Chrome扩展,在加载图像之前更改img元素的src=值

我有更改图像的代码,但它总是在img加载后执行。在加载图像之前,如何在SRC中更改url

看看或。我不久前遇到过这个问题,我的解决方案基本上是相同的(用'blah='替换html字符串中的src属性,然后添加我自己的src)。正如链接中所说,如果使用jquery,请不要使用
$(html)
,因为它一经解析就会获取图像。

查看或。我不久前遇到过这个问题,我的解决方案基本上是相同的(用'blah='替换html字符串中的src属性,然后添加我自己的src)。正如链接中所说,如果您使用jquery,请不要使用
$(html)
,因为它一经解析就会获取图像。

您应该:

  • 确保内容脚本的
    run\u at
    属性为
    document start
  • 将图像重写代码放在一个文件夹中
  • 您的脚本应该如下所示:

    String.prototype.endsWith = function(str) {
        return this.substr(str.length*-1) === str;;
    };
    
    function doBeforeLoad(event) {
        // Check to see if its what you want to redirect
        // You can check against different things about the element, such as its type (tagName), id, class, whatever
        // Be aware that if your checking the src attribute and it was a relative path in the html then the src you recieve will be resolved....
        // so if the html was <img src="/bunyip.png"> and the base url is www.google.com then the event.srcElement.src will be www.google.com/bunyip.png
        // this is why I use the ends with...so you dont have to deal with things like http://www.google.com, https://www.gooogle.com, http://google.com 
        // We also check for a data attribute that we set, so we know its allready been redirected
        // we do this because if you redirect a source it will fire another beforeload event for its new url
        if (!event.srcElement.dataset['redirected'] && event.srcElement.tagName == "IMG" && event.srcElement.src.endsWith('/test.png')) {
            // If it is something we want to redirect then set a data attribute so we know its allready been changed
            // Set that attribute to it original src in case we need to know what it was later
            event.srcElement.dataset['redirected'] = event.srcElement.src;
            // Set the source to the new url you want the element to point to
            event.srcElement.src = "replacement.png";
        }
    }
    
    document.addEventListener('beforeload', doBeforeLoad, true);
    
    String.prototype.endsWith=函数(str){
    返回这个.substr(str.length*-1)==str;;
    };
    函数dobeforload(事件){
    //检查是否是您想要重定向的内容
    //您可以检查元素的不同方面,例如它的类型(标记名)、id、类等等
    //请注意,如果您检查src属性并且它是html中的一个相对路径,那么您收到的src将被解析。。。。
    //因此,如果html是,基本url是www.google.com,那么event.srcement.src将是www.google.com/bunyip.png
    //这就是为什么我用…结尾,所以你不必处理像这样的事情http://www.google.com, https://www.gooogle.com, http://google.com 
    //我们还检查设置的数据属性,以便知道其allready已被重定向
    //我们这样做是因为如果重定向一个源,它将为其新url触发另一个beforeload事件
    如果(!event.srcielement.dataset['redirected']&&event.srcielement.tagName==“IMG”&&event.srcielement.src.endsWith('/test.png')){
    //如果它是我们想要重定向的东西,那么设置一个数据属性,这样我们就知道它已经被更改了
    //将该属性设置为原始src,以防我们以后需要知道它是什么
    event.srcElement.dataset['redirected']=event.srcElement.src;
    //将源设置为希望元素指向的新url
    event.srcmelement.src=“replacement.png”;
    }
    }
    document.addEventListener('beforeload',doBeforeLoad,true);
    
    您应该:

  • 确保内容脚本的
    run\u at
    属性为
    document start
  • 将图像重写代码放在一个文件夹中
  • 您的脚本应该如下所示:

    String.prototype.endsWith = function(str) {
        return this.substr(str.length*-1) === str;;
    };
    
    function doBeforeLoad(event) {
        // Check to see if its what you want to redirect
        // You can check against different things about the element, such as its type (tagName), id, class, whatever
        // Be aware that if your checking the src attribute and it was a relative path in the html then the src you recieve will be resolved....
        // so if the html was <img src="/bunyip.png"> and the base url is www.google.com then the event.srcElement.src will be www.google.com/bunyip.png
        // this is why I use the ends with...so you dont have to deal with things like http://www.google.com, https://www.gooogle.com, http://google.com 
        // We also check for a data attribute that we set, so we know its allready been redirected
        // we do this because if you redirect a source it will fire another beforeload event for its new url
        if (!event.srcElement.dataset['redirected'] && event.srcElement.tagName == "IMG" && event.srcElement.src.endsWith('/test.png')) {
            // If it is something we want to redirect then set a data attribute so we know its allready been changed
            // Set that attribute to it original src in case we need to know what it was later
            event.srcElement.dataset['redirected'] = event.srcElement.src;
            // Set the source to the new url you want the element to point to
            event.srcElement.src = "replacement.png";
        }
    }
    
    document.addEventListener('beforeload', doBeforeLoad, true);
    
    String.prototype.endsWith=函数(str){
    返回这个.substr(str.length*-1)==str;;
    };
    函数dobeforload(事件){
    //检查是否是您想要重定向的内容
    //您可以检查元素的不同方面,例如它的类型(标记名)、id、类等等
    //请注意,如果您检查src属性并且它是html中的一个相对路径,那么您收到的src将被解析。。。。
    //因此,如果html是,基本url是www.google.com,那么event.srcement.src将是www.google.com/bunyip.png
    //这就是为什么我用…结尾,所以你不必处理像这样的事情http://www.google.com, https://www.gooogle.com, http://google.com 
    //我们还检查设置的数据属性,以便知道其allready已被重定向
    //我们这样做是因为如果重定向一个源,它将为其新url触发另一个beforeload事件
    如果(!event.srcielement.dataset['redirected']&&event.srcielement.tagName==“IMG”&&event.srcielement.src.endsWith('/test.png')){
    //如果它是我们想要重定向的东西,那么设置一个数据属性,这样我们就知道它已经被更改了
    //将该属性设置为原始src,以防我们以后需要知道它是什么
    event.srcElement.dataset['redirected']=event.srcElement.src;
    //将源设置为希望元素指向的新url
    event.srcmelement.src=“replacement.png”;
    }
    }
    document.addEventListener('beforeload',doBeforeLoad,true);
    
    如果知道url模式,可以使用WebRequestAPI-来阻止和重定向图像调用。但是这不会改变src,您可以稍后再做。这将阻止对该图像源的GET调用


    最初来自-

    如果您知道url模式,您可以使用WebRequestAPI-来阻止和重定向图像调用。但是这不会改变src,您可以稍后再做。这将阻止对该图像源的GET调用


    最初来自-

    如果不起作用,另一个选项是使用beforeload事件。@PAEz此任务应使用
    beforeload
    事件。如果我没有发现重复的问题,请将其作为答案发布。@RobW我已经更正了我的答案,但事实上,它是由PAEz回答的。复制是关于脚本而不是图像,但答案是相同的。@Rob W希望可以,但我更改了源代码以处理一些事情。主要的事实是,更改src将在加载新url之前触发另一个。添加了一堆注释来解释这一切。看起来不错。另一个合适的替代方法(不使用
    beforeload
    事件)是API(重定向图像)。这必须在后台页面中实现,如果某些图像始终必须重定向,则首选
    beforeload
    。如果这不起作用,另一个选项是使用beforeload事件。@PAEz此任务应使用
    beforeload
    事件。如果我没有发现重复的问题,请将其作为答案发布。@RobW我已经更正了我的问题