Created
February 28, 2017 10:37
-
-
Save soulflyman/6c5a501796e3dcaa8fd61f4463947269 to your computer and use it in GitHub Desktop.
The fixAssetLoading.js from @edave64 modified for the use in your "about" field in your soup.io profile
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<script type="text/javascript"> | |
/** | |
* Replaces assets that fail to load with working alternatives. | |
* | |
* Licence: Public domain | |
*/ | |
/** | |
* @param {Element} ele | |
*/ | |
function fixElement (ele) { | |
var attempt = ele.getAttribute("data-attempt") + 1 || 1; | |
if (ele.tagName === "IMG") { | |
fixImage(ele, attempt); | |
} | |
ele.setAttribute("data-attempt", attempt); | |
} | |
/** | |
* @param {HTMLImageElement} img | |
* @param attempt | |
*/ | |
function fixImage (img, attempt) { | |
switch (attempt) { | |
case 1: | |
var src = img.src; | |
img.setAttribute("data-orig-src", src); | |
var srcNew = src.replace(/_\d+(.\w+)$/, "$1"); | |
if (src === srcNew) return fixImage(img, 2); | |
console.log("trying to repair image: " + img.src); | |
img.src = srcNew; | |
break; | |
default: | |
console.warn("image repair failed"); | |
// Give up; | |
break; | |
} | |
} | |
function init () { | |
document.addEventListener("error", function (e) { | |
if (e && e.target) fixElement(e.target); | |
}, true); | |
var images = document.getElementsByTagName("img"); | |
for (var i = 0; i < images.length; ++i) { | |
var image = images[i]; | |
if (image.complete && image.naturalWidth === 0) { | |
fixElement(image) | |
} | |
} | |
} | |
document.addEventListener("DOMContentLoaded",function(){init();}); | |
</script> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment