手动加载 <iframe>

不是 lazyload;点击按钮而不是点击 <iframe> 元素,以免误触;会忽略大小较小的 <iframe> 元素。

目前为 2020-10-21 提交的版本。查看 最新版本

  1. // ==UserScript==
  2. // @name <iframe> click to load
  3. // @name:zh-CN 手动加载 <iframe>
  4. // @description NOT lazyload; click a button instead anywhere in the whole <iframe> element so one won't mis-click; will ignore small / narrow <iframe>s.
  5. // @description:zh-CN 不是 lazyload;点击按钮而不是点击 <iframe> 元素,以免误触;会忽略大小较小的 <iframe> 元素。
  6. // @namespace RainSlide
  7. // @author RainSlide
  8. // @version 1.1
  9. // @match *://*/*
  10. // @grant none
  11. // @inject-into content
  12. // @run-at document-end
  13. // ==/UserScript==
  14.  
  15. "use strict";
  16.  
  17. document.querySelectorAll('iframe').forEach(
  18.  
  19. iframe =>
  20.  
  21. // attribute
  22. iframe.getAttribute("src") !== null &&
  23. iframe.getAttribute("srcdoc") === null &&
  24.  
  25. // content size (padding of <iframe> included by now)
  26. iframe.clientWidth >= 72 &&
  27. iframe.clientHeight >= 72 &&
  28. ( iframe.clientWidth + iframe.clientHeight ) >= 256 &&
  29.  
  30. iframe.setAttribute("srcdoc",
  31.  
  32. `<style>
  33. html, body {
  34. display: flex;
  35. flex-direction: column;
  36. justify-content: center;
  37. align-items: center;
  38. }
  39. html {
  40. min-width: 5em;
  41. min-height: 100%;
  42. border: 2px dashed currentColor;
  43. box-sizing: border-box;
  44. }
  45. a {
  46. margin-bottom: 1ex;
  47. word-break: break-all;
  48. font-family: monospace;
  49. }
  50. </style>
  51. <a href="${ iframe.src }" target="_blank" rel="noreferrer noopener">${ iframe.src }</a>
  52. <button onclick="window.frameElement.removeAttribute('srcdoc')">${
  53. new Map([
  54. ["en-US", "Load"],
  55. ["zh-CN", "加载"],
  56. ["zh-TW", "加載"],
  57. ["zh-HK", "加載"]
  58. ]).get( navigator.language ) || "Load"
  59. }</button>`
  60.  
  61. )
  62.  
  63. );