embedded-popup.js 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114
  1. /* global $ $create $remove getEventKeyName */// dom.js
  2. /* global CodeMirror */
  3. /* global baseInit */// base.js
  4. /* global prefs */
  5. /* global t */// localization.js
  6. 'use strict';
  7. (() => {
  8. const ID = 'popup-iframe';
  9. const SEL = '#' + ID;
  10. const URL = chrome.runtime.getManifest().browser_action.default_popup;
  11. const POPUP_HOTKEY = 'Shift-Ctrl-Alt-S';
  12. /** @type {HTMLIFrameElement} */
  13. let frame;
  14. let isLoaded;
  15. let scrollbarWidth;
  16. const btn = $create('img', {
  17. id: 'popup-button',
  18. title: t('optionsCustomizePopup') + '\n' + POPUP_HOTKEY,
  19. onclick: embedPopup,
  20. });
  21. document.documentElement.appendChild(btn);
  22. baseInit.domReady.then(() => {
  23. document.body.appendChild(btn);
  24. // Adding a dummy command to show in keymap help popup
  25. CodeMirror.defaults.extraKeys[POPUP_HOTKEY] = 'openStylusPopup';
  26. });
  27. prefs.subscribe('iconset', (_, val) => {
  28. const prefix = `images/icon/${val ? 'light/' : ''}`;
  29. btn.srcset = `${prefix}16.png 1x,${prefix}32.png 2x`;
  30. }, {runNow: true});
  31. window.on('keydown', e => {
  32. if (getEventKeyName(e) === POPUP_HOTKEY) {
  33. embedPopup();
  34. }
  35. });
  36. function embedPopup() {
  37. if ($(SEL)) return;
  38. isLoaded = false;
  39. scrollbarWidth = 0;
  40. frame = $create('iframe', {
  41. id: ID,
  42. src: URL,
  43. height: 600,
  44. width: prefs.get('popupWidth'),
  45. onload: initFrame,
  46. });
  47. window.on('mousedown', removePopup);
  48. document.body.appendChild(frame);
  49. }
  50. function initFrame() {
  51. frame = this;
  52. frame.focus();
  53. const pw = frame.contentWindow;
  54. const body = pw.document.body;
  55. pw.on('keydown', removePopupOnEsc);
  56. pw.close = removePopup;
  57. if (pw.IntersectionObserver) {
  58. new pw.IntersectionObserver(onIntersect).observe(body.appendChild(
  59. $create('div', {style: {height: '1px', marginTop: '-1px'}})
  60. ));
  61. } else {
  62. frame.dataset.loaded = '';
  63. frame.height = body.scrollHeight;
  64. }
  65. new pw.MutationObserver(onMutation).observe(body, {
  66. attributes: true,
  67. attributeFilter: ['style'],
  68. });
  69. }
  70. function onMutation() {
  71. const body = frame.contentDocument.body;
  72. const bs = body.style;
  73. const w = parseFloat(bs.minWidth || bs.width) + (scrollbarWidth || 0);
  74. const h = parseFloat(bs.minHeight || body.offsetHeight);
  75. if (frame.width - w) frame.width = w;
  76. if (frame.height - h) frame.height = h;
  77. }
  78. function onIntersect([e]) {
  79. const pw = frame.contentWindow;
  80. const el = pw.document.scrollingElement;
  81. const h = e.isIntersecting && !pw.scrollY ? el.offsetHeight : el.scrollHeight;
  82. const hasSB = h > el.offsetHeight;
  83. const {width} = e.boundingClientRect;
  84. frame.height = h;
  85. if (!hasSB !== !scrollbarWidth || frame.width - width) {
  86. scrollbarWidth = hasSB ? width - el.offsetWidth : 0;
  87. frame.width = width + scrollbarWidth;
  88. }
  89. if (!isLoaded) {
  90. isLoaded = true;
  91. frame.dataset.loaded = '';
  92. }
  93. }
  94. function removePopup() {
  95. frame = null;
  96. $remove(SEL);
  97. window.off('mousedown', removePopup);
  98. }
  99. function removePopupOnEsc(e) {
  100. if (getEventKeyName(e) === 'Escape') {
  101. removePopup();
  102. }
  103. }
  104. })();