找到1个回复 (用户: 咯叽)
  • 纯前端获取本地已安装字体测试
    1231点击 / 11-07 09:38发布 / 11-07 17:06回复 / /
    导入网页插件:html所有预览功能新窗口打开(当前用户:1,总安装次数:1)
    <script>
    document.addEventListener('DOMContentLoaded', function() {
        // 获取目标 <a> 元素
        const targetLink = document.querySelector('a.useriframelink');
        if (!targetLink) return;
    
        // 创建按钮元素
        const previewBtn = document.createElement('button');
        previewBtn.textContent = '新窗口预览';
        previewBtn.style.cssText = 'margin-left: 10px; cursor: pointer;';
    
        // 在 <a> 元素后面插入按钮
        targetLink.insertAdjacentElement('afterend', previewBtn);
    
        // 解码 HTML 实体的函数
        const decodeEntities = (html) => {
            const txt = document.createElement('textarea');
            txt.innerHTML = html;
            return txt.value;
        };
    
        // 点击事件处理
        previewBtn.addEventListener('click', () => {
            const iframe = document.querySelector('.useriframe');
            if (!iframe) return;
    
            // 解码 srcdoc 内容
            const decodedContent = decodeEntities(iframe.srcdoc);
    
            // 创建新窗口
            const newWindow = window.open('', 'Preview', 'width=800,height=600');
            if (!newWindow) {
                alert('请允许弹出窗口');
                return;
            }
    
            // 写入解码后的内容
            newWindow.document.open();
            newWindow.document.write(`
                <!DOCTYPE html>
                <html>
                <head>
                    <title>预览</title>
                    <base target="_blank">
                    <style>body { margin: 10px }</style>
                </head>
                <body>${decodedContent}</body>
                </html>
            `);
            newWindow.document.close();
        });
    });
    </script>

    视频链接:20251107_170254.mp4(2.98 MB)

    可以加个是否打开新窗口二次手动确认