@@ -799,40 +799,8 @@ <h5 class="modal-title" id="composeModalLabel"><i class="bi bi-pencil-square me-
799799 iframe . setAttribute ( 'scrolling' , 'no' ) ;
800800 iframe . style . overflow = 'hidden' ;
801801
802- const resizeScript = `
803- (function () {
804- const resize = () => {
805- const body = document.body;
806- const doc = document.documentElement;
807- const height = Math.max(
808- body ? body.scrollHeight : 0,
809- body ? body.offsetHeight : 0,
810- doc ? doc.scrollHeight : 0,
811- doc ? doc.offsetHeight : 0
812- );
813- if (window.frameElement) {
814- window.frameElement.style.height = (height + 20) + 'px';
815- }
816- };
817-
818- window.addEventListener('load', resize);
819- if (window.ResizeObserver && document.body) {
820- new ResizeObserver(resize).observe(document.body);
821- }
822- Array.from(document.images || []).forEach((img) => {
823- if (!img.complete) {
824- img.addEventListener('load', resize);
825- img.addEventListener('error', resize);
826- }
827- });
828- requestAnimationFrame(resize);
829- setTimeout(resize, 120);
830- setTimeout(resize, 500);
831- })();
832- ` ;
833-
834802 iframeDoc . open ( ) ;
835- iframeDoc . write ( `<!DOCTYPE html><html><head><meta charset="UTF-8"><base target="_blank"><style>html,body{margin:0;padding:0;overflow:hidden; background:transparent;}body{font-family:"Noto Sans SC",system-ui,sans-serif;font-size:14px;color:#1a2421;padding:12px;word-break:break-word;overflow:hidden; }img{max-width:100%;height:auto;}a{color:#4a7c9b;}</style></head><body>${ mailContent } <script> ${ resizeScript } <\/script> </body></html>` ) ;
803+ iframeDoc . write ( `<!DOCTYPE html><html><head><meta charset="UTF-8"><base target="_blank"><style>html,body{margin:0;padding:0;background:transparent;}body{font-family:"Noto Sans SC",system-ui,sans-serif;font-size:14px;color:#1a2421;padding:12px;word-break:break-word;}img{max-width:100%;height:auto;}a{color:#4a7c9b;}</style></head><body>${ mailContent } </body></html>` ) ;
836804 iframeDoc . close ( ) ;
837805
838806 // 强制所有链接在新标签页打开(覆盖邮件中显式设置的 target)
@@ -841,17 +809,32 @@ <h5 class="modal-title" id="composeModalLabel"><i class="bi bi-pencil-square me-
841809 a . setAttribute ( 'rel' , 'noopener noreferrer' ) ;
842810 } ) ;
843811
844- try {
845- const body = iframeDoc . body ;
846- const doc = iframeDoc . documentElement ;
847- const height = Math . max (
848- body ? body . scrollHeight : 0 ,
849- body ? body . offsetHeight : 0 ,
850- doc ? doc . scrollHeight : 0 ,
851- doc ? doc . offsetHeight : 0
852- ) ;
853- iframe . style . height = ( height + 20 ) + 'px' ;
854- } catch ( e ) { }
812+ // 从父页面计算 iframe 高度(sandbox 无 allow-scripts,脚本无法在 iframe 内执行)
813+ const recalcHeight = ( ) => {
814+ try {
815+ const body = iframeDoc . body ;
816+ const doc = iframeDoc . documentElement ;
817+ const height = Math . max (
818+ body ? body . scrollHeight : 0 ,
819+ body ? body . offsetHeight : 0 ,
820+ doc ? doc . scrollHeight : 0 ,
821+ doc ? doc . offsetHeight : 0
822+ ) ;
823+ iframe . style . height = ( height + 20 ) + 'px' ;
824+ } catch ( e ) { }
825+ } ;
826+
827+ // 监听 iframe 内图片加载完成后重新计算高度
828+ iframeDoc . querySelectorAll ( 'img' ) . forEach ( img => {
829+ if ( ! img . complete ) {
830+ img . addEventListener ( 'load' , recalcHeight ) ;
831+ img . addEventListener ( 'error' , recalcHeight ) ;
832+ }
833+ } ) ;
834+
835+ recalcHeight ( ) ;
836+ setTimeout ( recalcHeight , 200 ) ;
837+ setTimeout ( recalcHeight , 800 ) ;
855838 }
856839
857840 // 更新未读邮件计数 badge
0 commit comments