🐛 停止改写共享前端模块

This commit is contained in:
orion committed 2026-08-07 11:33:27 +08:00
1 parent bf68e8dded
commit 5427c187d4
2 files changed
+12 -29

No files matched your search

+6 -16
View File
@@ -26,22 +26,12 @@ def patch_file_manage(path: Path) -> bool:
def patch_clipboard(path: Path) -> bool: def patch_clipboard(path: Path) -> bool:
text = path.read_text(encoding='utf-8') """Never rewrite Vite's shared clipboard chunk after compilation.
old = text
pattern = re.compile( The source utility already implements Clipboard API, DOM and manual-copy
r'const (?P<name>[A-Za-z_$][\w$]*)=async\(a,e=\{\}\)=>\{.*?\},' fallbacks. Rewriting a minified ESM module can invalidate every page that
r'(?=[A-Za-z_$][\w$]*=async\(a,e=\{\}\)=>\{)', imports it, including FileManageView.
re.S, """
)
match = pattern.search(text)
if not match:
return False
name = match.group('name')
replacement = f'''const {name}=async(a,e={{}})=>{{const{{successMsg:t="复制成功",errorMsg:n="复制失败,请手动复制",showMsg:s=!0,notify:c}}=e,l=(r,d)=>{{s&&c?.(r,d)}},q=()=>{{try{{window.prompt("复制下面内容",a)}}catch{{}}}};try{{if(navigator.clipboard&&window.isSecureContext)try{{await navigator.clipboard.writeText(a);return l(t,"success"),!0}}catch{{}}const r=document.createElement("textarea");r.value=a,r.setAttribute("readonly","");r.className="fcbx-copy-fallback";r.style.cssText="position:fixed;left:0;top:0;width:1px;height:1px;opacity:0;z-index:-1";document.body.appendChild(r);r.focus();r.select();r.setSelectionRange(0,r.value.length);let d=!1;try{{d=document.execCommand("copy")}}finally{{document.body.removeChild(r)}}if(d)return l(t,"success"),!0;q();l("已打开手动复制窗口","warning");return!1}}catch(r){{return console.error("复制失败:",r),q(),l(n,"error"),!1}}}},'''
text = text[:match.start()] + replacement + text[match.end():]
if text != old:
rewrite(path, text)
return True
return False return False
+6 -13
View File
@@ -1,8 +1,7 @@
import tempfile
import unittest
from pathlib import Path from pathlib import Path
import unittest
from scripts.patch_admin_timeline_ui import build_lifecycle_script, patch_clipboard from scripts.patch_admin_timeline_ui import build_lifecycle_script, patch_clipboard, patch_file_manage
class AdminTimelinePatchTests(unittest.TestCase): class AdminTimelinePatchTests(unittest.TestCase):
@@ -13,16 +12,10 @@ class AdminTimelinePatchTests(unittest.TestCase):
self.assertIn("展开全部取件记录", script) self.assertIn("展开全部取件记录", script)
self.assertIn("row.style.overflow = 'visible'", script) self.assertIn("row.style.overflow = 'visible'", script)
def test_clipboard_rewrite_keeps_module_declaration_separator(self): def test_shared_compiled_modules_are_not_rewritten(self):
source = '''const m=async(a,e={})=>{try{return!0}catch(r){return!1}},R=async(a,e={})=>{return a};export{m};''' placeholder = Path("unused.js")
with tempfile.TemporaryDirectory() as temp_dir: self.assertFalse(patch_file_manage(placeholder))
path = Path(temp_dir) / "clipboard-test.js" self.assertFalse(patch_clipboard(placeholder))
path.write_text(source, encoding="utf-8")
self.assertTrue(patch_clipboard(path))
patched = path.read_text(encoding="utf-8")
self.assertIn("}},R=async", patched)
if __name__ == "__main__": if __name__ == "__main__":