Show and reload broken images

The name explained it

  1. // ==UserScript==
  2. // @name Show and reload broken images
  3. // @version 3.0.0
  4. // @description The name explained it
  5. // @homepageURL https://github.com/eight04/show-and-reload-broken-images#readme
  6. // @supportURL https://github.com/eight04/show-and-reload-broken-images/issues
  7. // @license MIT
  8. // @author eight04 <eight04@gmail.com>
  9. // @namespace eight04.blogspot.com
  10. // @include *
  11. // @grant GM_addStyle
  12. // ==/UserScript==
  13.  
  14. /* eslint-env browser, greasemonkey */
  15.  
  16. function reloadImages() {
  17. for (const img of document.images) {
  18. if (!img.complete || img.matches("[src]:-moz-broken")) {
  19. img.src += "#";
  20. }
  21. }
  22. }
  23.  
  24. function broadcastEvent() {
  25. for (const win of window.frames) {
  26. win.postMessage("RELOAD_BROKEN_IMAGES", "*");
  27. }
  28. }
  29.  
  30. function init() {
  31. window.addEventListener("keyup", e => {
  32. if (e.keyCode === 82 && e.altKey) {
  33. reloadImages();
  34. broadcastEvent();
  35. }
  36. });
  37. window.addEventListener("message", e => {
  38. if (e.data === "RELOAD_BROKEN_IMAGES") {
  39. reloadImages();
  40. broadcastEvent();
  41. }
  42. });
  43. }
  44.  
  45. {
  46. init();
  47. }