最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - Chrome Extension - Fix img tag onerror infinite loop - Stack Overflow

programmeradmin5浏览0评论

I would like to fix a particular bug of a website with an extension I'm making.

(going to explain the site in simple terms)

The website has an album and the way it works is that each album has an x amount images and each image has its own page for example: 1.html. On each page is a single image of which i assume are dynamically generated. For some reason sometimes on the last page of an album the img tag looks like this:

<img id="image" src="mainsite/album1/images/" onerror="this.src='mainsite/album1/images/'">

So this results in chrome being stuck in an endless loop logging the following:

Resource interpreted as Image but transferred with MIME type text/html: "mainsite/album1/images/".

This causes my puter and I'm sure anyone else's who use the site to lag real bad as a result of this bug.

I wish to fix this issue with an extension so my question is, is it possible to fix this problem with an extension? if so, how? I've tried setting the onerror and src attribute to an empty string and it seems the src of the img is constantly being updated somehow which results in the error still going off.

Here's an example of the problem (warning! it may lag your chrome if you stay on page too long)

I would like to fix a particular bug of a website with an extension I'm making.

(going to explain the site in simple terms)

The website has an album and the way it works is that each album has an x amount images and each image has its own page for example: 1.html. On each page is a single image of which i assume are dynamically generated. For some reason sometimes on the last page of an album the img tag looks like this:

<img id="image" src="mainsite./album1/images/" onerror="this.src='mainsite./album1/images/'">

So this results in chrome being stuck in an endless loop logging the following:

Resource interpreted as Image but transferred with MIME type text/html: "mainsite./album1/images/".

This causes my puter and I'm sure anyone else's who use the site to lag real bad as a result of this bug.

I wish to fix this issue with an extension so my question is, is it possible to fix this problem with an extension? if so, how? I've tried setting the onerror and src attribute to an empty string and it seems the src of the img is constantly being updated somehow which results in the error still going off.

Here's an example of the problem (warning! it may lag your chrome if you stay on page too long)

Share Improve this question asked Dec 12, 2014 at 8:37 Norman VNorman V 851 silver badge6 bronze badges
Add a ment  | 

2 Answers 2

Reset to default 6

As an alternative, here's a jQuery-free solution:

<img src="image.png" onerror="this.src='image.png'; this.onerror = null" />

Remove the onerror attribute:

document.getElementById('image').removeAttribute('onerror');
// or if you use jQuery: $('#image').removeAttr('onerror');

Using img.onerror = null within a content script does not work, because of the isolation of JavaScript execution contexts: the page's onerror property is invisible to your content script.

发布评论

评论列表(0)

  1. 暂无评论