| 1 |
/* Remove Broken Images by Room 34 Creative Services, LLC - https://room34.com */ |
| 2 |
|
| 3 |
function r34rbi(elem) { |
| 4 |
|
| 5 |
// Hook to allow custom behavior when a missing image is found |
| 6 |
jQuery(document).trigger('r34rbi_missing_image'); |
| 7 |
|
| 8 |
// Identify parent 'a' and ancestor '.wp-caption' elements |
| 9 |
var ancestor_caption = elem.closest('.wp-caption'); |
| 10 |
var parent_link = elem.parent('a'); |
| 11 |
|
| 12 |
// If ancestor caption exists, remove it entirely |
| 13 |
if (ancestor_caption.length > 0) { ancestor_caption.remove(); } |
| 14 |
|
| 15 |
// If parent link exists, remove it entirely |
| 16 |
else if (parent_link.length > 0) { parent_link.remove(); } |
| 17 |
|
| 18 |
// Just remove the image |
| 19 |
else if (elem.length > 0) { elem.remove(); } |
| 20 |
|
| 21 |
// Also remove meta tags pointing to image (only works with crawlers that evaluate JavaScript) |
| 22 |
jQuery(document).find('meta[property="og:image"][content="' + elem.attr('src') + '"], meta[property="twitter:image"][content="' + elem.attr('src') + '"]').remove(); |
| 23 |
|
| 24 |
} |
| 25 |
|
| 26 |
jQuery(function() { |
| 27 |
|
| 28 |
// Images in DOM on initial load |
| 29 |
jQuery('img').on('error', function() { r34rbi(jQuery(this)); }); |
| 30 |
|
| 31 |
// Images inserted into DOM by AJAX |
| 32 |
jQuery(document).ajaxComplete(function() { |
| 33 |
jQuery('img').on('error', function() { r34rbi(jQuery(this)); }); |
| 34 |
}); |
| 35 |
|
| 36 |
// Redirect on missing image |
| 37 |
if ( |
| 38 |
typeof r34rbi_redirect_on_missing_image != 'undefined' |
| 39 |
&& r34rbi_redirect_on_missing_image != '' |
| 40 |
&& r34rbi_redirect_on_missing_image != location.href |
| 41 |
) |
| 42 |
{ |
| 43 |
jQuery(document).on('r34rbi_missing_image', function() { |
| 44 |
location.replace(r34rbi_redirect_on_missing_image); |
| 45 |
}); |
| 46 |
} |
| 47 |
|
| 48 |
}); |
| 49 |
|