calculateHtmlDocDimensions property

String calculateHtmlDocDimensions
getter/setter pair

Implementation

static String calculateHtmlDocDimensions = '''
    (function() {
      const body = document.body;
      const html = document.documentElement;

      html.style.margin = '0';
      html.style.padding = '0';
      html.style.overflow = 'hidden';
      body.style.margin = '0';
      body.style.padding = '0';
      body.style.overflow = 'hidden';

      // Ensure all images loaded
      const imgs = document.images;
      for (let i = 0; i < imgs.length; i++) {
        if (!imgs[i].complete) return -1;
      }

      let height = Math.max(
        body.scrollHeight, body.offsetHeight,
        html.clientHeight, html.scrollHeight, html.offsetHeight
      );
      let width = Math.max(
        body.scrollWidth, body.offsetWidth,
        html.clientWidth, html.scrollWidth, html.offsetWidth
      );


      // Consider iframes
      const iframes = document.getElementsByTagName('iframe');
      for (let i = 0; i < iframes.length; i++) {
        const iframe = iframes[i];
        let iframeHeight = 0;
        let iframeWidth = 0;

        try {
          // Try reading internal body height (same-origin only)
          const iframeDoc = iframe.contentDocument || iframe.contentWindow.document;
          if (iframeDoc && iframeDoc.readyState === 'complete') {
            const ib = iframeDoc.body;
            if (ib) {
              iframeHeight = Math.max(ib.scrollHeight, ib.offsetHeight);
              iframeWidth = Math.max(ib.scrollWidth, ib.offsetWidth);
            }
          }
        } catch (e) {
          // Cross-origin: fallback to visible/computed height
          iframeHeight =
            parseInt(iframe.getAttribute('height')) ||
            iframe.offsetHeight ||
            parseInt(window.getComputedStyle(iframe).height) ||
            0;
          iframeWidth =
            parseInt(iframe.getAttribute('width')) ||
            iframe?.offsetWidth ||
            parseInt((window.getComputedStyle(iframe) || {}).width)  || 0;
        }

        // Add based on bottom edge position, not sum
        height=height+(iframeHeight);
        if(iframeWidth){
          width = iframeWidth;
        }
      }

      return JSON.stringify({ height: height.toString(), width: width.toString() });
    })();
  ''';