|
| 1 | +import os |
| 2 | +import sys |
| 3 | + |
| 4 | + |
| 5 | +def encrypt_file(file_path, password): |
| 6 | + print(f"🔒 Encrypting {file_path}...") |
| 7 | + try: |
| 8 | + with open(file_path, 'r', encoding='utf-8') as f: |
| 9 | + content = f.read() |
| 10 | + |
| 11 | + # 建立一個簡單但有效的加密保護頁面 |
| 12 | + # 內容被放在隱藏區塊中,輸入正確密碼後才會顯示 |
| 13 | + protected_html = f""" |
| 14 | +<!DOCTYPE html> |
| 15 | +<html> |
| 16 | +<head> |
| 17 | + <meta charset="UTF-8"> |
| 18 | + <title>受保護的內容</title> |
| 19 | + <style> |
| 20 | + body {{ font-family: sans-serif; display: flex; justify-content: center; align-items: center; height: 100vh; background: #f5f5f5; }} |
| 21 | + .box {{ background: white; padding: 2rem; border-radius: 8px; box-shadow: 0 4px 6px rgba(0,0,0,0.1); text-align: center; }} |
| 22 | + input {{ padding: 10px; width: 200px; border: 1px solid #ccc; border-radius: 4px; margin-bottom: 10px; }} |
| 23 | + button {{ padding: 10px 20px; background: #3f51b5; color: white; border: none; border-radius: 4px; cursor: pointer; }} |
| 24 | + button:hover {{ background: #303f9f; }} |
| 25 | + #content {{ display: none; }} |
| 26 | + </style> |
| 27 | +</head> |
| 28 | +<body> |
| 29 | + <div class="box" id="login-box"> |
| 30 | + <h2>🔒 受保護的解法</h2> |
| 31 | + <p>請輸入密碼以查看解題思路</p> |
| 32 | + <input type="password" id="pass" placeholder="輸入密碼..." onpython="if(event.key==='Enter') verify()"> |
| 33 | + <br> |
| 34 | + <button onclick="verify()">確認</button> |
| 35 | + </div> |
| 36 | + <div id="content">{content}</div> |
| 37 | +
|
| 38 | + <script> |
| 39 | + function verify() {{ |
| 40 | + const p = document.getElementById('pass').value; |
| 41 | + if (p === "{password}") {{ |
| 42 | + document.getElementById('login-box').style.display = 'none'; |
| 43 | + const content = document.getElementById('content'); |
| 44 | + content.style.display = 'block'; |
| 45 | + // 重新載入 Mermaid 或其他腳本(如果有的話) |
| 46 | + if (window.mermaid) mermaid.init(); |
| 47 | + }} else {{ |
| 48 | + alert('密碼錯誤!'); |
| 49 | + }} |
| 50 | + }} |
| 51 | + // 支援 Enter 鍵 |
| 52 | + document.getElementById('pass').addEventListener('keypress', function (e) {{ |
| 53 | + if (e.key === 'Enter') verify(); |
| 54 | + }}); |
| 55 | + </script> |
| 56 | +</body> |
| 57 | +</html> |
| 58 | +""" |
| 59 | + with open(file_path, 'w', encoding='utf-8') as f: |
| 60 | + f.write(protected_html) |
| 61 | + return True |
| 62 | + except Exception as e: |
| 63 | + print(f"❌ Error: {e}") |
| 64 | + return False |
| 65 | + |
| 66 | + |
| 67 | +if __name__ == "__main__": |
| 68 | + site_dir = "site" |
| 69 | + # 優先從環境變數讀取密碼,本地測試若沒設定則預設為 "111" |
| 70 | + password = os.environ.get("SOLUTION_PASSWORD", "111") |
| 71 | + |
| 72 | + count = 0 |
| 73 | + for root, dirs, files in os.walk(site_dir): |
| 74 | + if "README_solution" in root and "index.html" in files: |
| 75 | + file_path = os.path.join(root, "index.html") |
| 76 | + if encrypt_file(file_path, password): |
| 77 | + count += 1 |
| 78 | + |
| 79 | + print(f"✅ Successfully protected {count} files.") |
0 commit comments