export-static-preview.mjs 2.0 KB

1234567891011
  1. import fs from 'node:fs';
  2. import {connectRealAccountWebView} from '../../scripts/lib/real-account-webview.mjs';
  3. const {cdp}=await connectRealAccountWebView();
  4. try{
  5. const snapshot=await cdp.evaluate(async()=>{if(location.pathname!=='/login'||!document.querySelector('#login-refinement-preview'))throw Error('不在登录原型页');const clone=document.querySelector('app-mb-login-page').cloneNode(true);clone.querySelectorAll('input').forEach(x=>{x.removeAttribute('value');x.value=''});clone.querySelectorAll('script').forEach(x=>x.remove());clone.querySelectorAll('*').forEach(x=>[...x.attributes].filter(a=>/^on/i.test(a.name)).forEach(a=>x.removeAttribute(a.name)));const styles=[];for(const sheet of [...document.styleSheets]){if(sheet.ownerNode?.tagName==='STYLE')styles.push(sheet.ownerNode.textContent);else if(sheet.href&&new URL(sheet.href).origin===location.origin)styles.push(await(await fetch(sheet.href)).text())}return{markup:clone.outerHTML,css:styles.join('\n'),htmlAttributes:[...document.documentElement.attributes].filter(a=>['class','style','data-palette','data-accent'].includes(a.name)).map(a=>[a.name,a.value])}});
  6. const base='file:///E:/work/fmode-app/test-results/real-account-20261130/build-r2/project/www/browser/';
  7. const css=snapshot.css.replace(/url\((['"]?)\/(?!\/)/g,(_,q)=>'url('+q+base);
  8. const attrs=snapshot.htmlAttributes.map(([k,v])=>k+'="'+v.replaceAll('&','&').replaceAll('"','"')+'"').join(' ');
  9. const file='prototypes/login-refinement-20261130/index.html';
  10. fs.writeFileSync(file,'<!doctype html><html lang="zh-CN" '+attrs+'><head><meta charset="utf-8"><meta name="viewport" content="width=device-width,initial-scale=1,viewport-fit=cover"><meta name="description" content="初始登录页静态布局原型,不发送验证码"><base href="'+base+'"><title>飞马SI · 初始登录布局预览</title><style>'+css+'</style></head><body>'+snapshot.markup+'</body></html>');console.log('已保存无脚本、无登录请求的静态布局原型');
  11. }finally{cdp.close()}