-
Notifications
You must be signed in to change notification settings - Fork 4
/
entities.js
59 lines (54 loc) · 9.58 KB
/
entities.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
const { encode, decode } = require("html-entities");
const escapedHtml = decode(
"<!doctype html ><html ><head ><!-- Recommended meta tags --><meta charset="UTF-8 "><meta name="viewport "content="width=device-width,initial-scale=1.0 "><!-- PyScript CSS --><link rel="stylesheet "href="https://pyscript.net/releases/2024.1.1/core.css "><!-- CSS for examples --><link rel="stylesheet "href="./assets/css/examples.css "/><!-- This script tag bootstraps PyScript --><script type="module "src="https://pyscript.net/releases/2024.1.1/core.js "></script ><!-- Custom CSS --><link href="https://fonts.googleapis.com/css?family=Indie+Flower "rel="stylesheet "><link rel="stylesheet "href="./assets/css/tictactoe.css "/><!-- for splashscreen --><style >#loading { outline: none; border: none; background: transparent } </style ><script type="module ">const loading = document.getElementById('loading '); addEventListener('py:ready ', () =>loading.close()); loading.showModal(); </script ><title >Tic Tac Toe </title ><link rel="icon "type="image/png "href="./assets/favicon.png "/><link rel="manifest "href="./psadc-manifest.json "></head ><body ><dialog id="loading "><h1 >Loading...</h1 ></dialog ><nav class="navbar "style="background-color: #000000 "><div class="app-header "><a href="/"><img src="./assets/logo.png "class="logo "/></a ><a class="title "href=""style="color: #f0ab3c ">Tic Tac Toe </a ></div ></nav ><section class="pyscript "><h1 >Tic-Tac-Toe </h1 ><script type="py "src="./main.py "config="./pyscript.toml "></script ><table id="board "><tr ><td ><div id="cell00 "data-x="0 "data-y="0 "class="cell "py-click="GAME.click "></div ></td ><td ><div id="cell01 "data-x="0 "data-y="1 "class="cell "py-click="GAME.click "></div ></td ><td ><div id="cell02 "data-x="0 "data-y="2 "class="cell "py-click="GAME.click "></div ></td ><tr ><td ><div id="cell10 "data-x="1 "data-y="0 "class="cell "py-click="GAME.click "></div ></td ><td ><div id="cell11 "data-x="1 "data-y="1 "class="cell "py-click="GAME.click "></div ></td ><td ><div id="cell12 "data-x="1 "data-y="2 "class="cell "py-click="GAME.click "></div ></td ></tr ><tr ><td ><div id="cell20 "data-x="2 "data-y="0 "class="cell "py-click="GAME.click "></div ></td ><td ><div id="cell21 "data-x="2 "data-y="1 "class="cell "py-click="GAME.click "></div ></td ><td ><div id="cell22 "data-x="2 "data-y="2 "class="cell "py-click="GAME.click "></div ></td ></tr ></table ><h2 id="status "></h2 ><button id="btn-new-game "py-click="GAME.new_game ">New game </button ><button id="btn-toggle-terminal "py-click="GAME.toggle_terminal ">Hide/show terminal </button ><div id="terminal "hidden="hidden "><script id="console "type="py "terminal ></script ></div ></section ><svg style="z-index:9997;position:fixed;bottom:0;right:0 "viewBox="0 0 315 27 "width="315 "height="27 "><path d="M143.905 0.043004L85.585 0.105C75.385 0.0870005 66.415 1.866 58.655 5.535C50.765 9.265 42.855 12.989 34.975 16.73C27.165 20.443 26.057 22.638 15.778 22.501C5.775 22.368 2.44 26.781 0 26.781L0.545006 26.693C22.005 26.672 43.455 26.676 64.915 26.676C90.655 26.676 116.395 26.665 142.135 26.699C143.115 26.699 143.915 26.259 143.915 25.707C143.855 11.729 143.855 14.026 143.915 0.0490007L143.905 0.043004Z "fill="#292929 "></path ><path d="M140.258 0L315 0.340524L314.966 27L140 26.9616L140.258 0Z "fill="#292929 "></path ></svg ><div style="z-index:9998;font-family:sans-serif;background-color:#292929;padding:2px 8px;position:fixed;bottom:0;right:0;left:0 "></div ><div style="z-index:9999;color:white;font-size: 0.7rem;font-family:sans-serif;padding: 5px;position:fixed;bottom:0;right:0 "><a href="https://pyscript.com/@wuyuan "id="made-on "target="_blank "style="color:#FFF;text-decoration:none;">Made on pyscript.com </a >&nbsp;│&nbsp;<a href="https://pyscript.com/@wuyuan/tic-tac-toe-copy/latest "id="view-code "target="_blank "style="color:#FFF;text-decoration:none;">View Code </a ><span id="install-pwa-section "hidden >&nbsp;│&nbsp; <a id="install-pwa "style="cursor:pointer; color:#FFF;">Install </a ></span >&nbsp;&nbsp </div ><script >let installPrompt = null; const installSection = document.querySelector("#install-pwa-section ") const installButton = document.querySelector("#install-pwa "); window.addEventListener("beforeinstallprompt ", (event) =>{ if ("onappinstalled "in window) { installSection.removeAttribute("hidden ") } event.preventDefault(); installPrompt = event; }); installButton.addEventListener("click ", async () =>{ if (!installPrompt) { return; } const result = await installPrompt.prompt(); disableInAppInstallPrompt(); }); function disableInAppInstallPrompt() { installPrompt = null; installButton.setAttribute("hidden ", ""); }; </script ><script >const heap_app_id = "758475466 "; // Only add analytics if we got an id if (heap_app_id) { // Analytics code window.heap=window.heap||[],heap.load=function(e,t){window.heap.appid=e,window.heap.config=t=t||{};var r=document.createElement("script ");r.type="text/javascript ",r.async=!0,r.src="https://cdn.heapanalytics.com/js/heap-"+e+".js ";var a=document.getElementsByTagName("script ")[0];a.parentNode.insertBefore(r,a);for(var n=function(e){return function(){heap.push([e].concat(Array.prototype.slice.call(arguments,0)))}},p=["addEventProperties ","addUserProperties ","clearEventProperties ","identify ","resetIdentity ","removeEventProperty ","setEventProperties ","track ","unsetEventProperty "],o=0;o <p.length;o++)heap[p[o]]=n(p[o])}; heap.load(heap_app_id); // Add event listener to the made-on link document.querySelector("#made-on ").addEventListener("click ", () =>{ heap.track("PSADC - Make on Pyscript "); }); // Add event listener to the view-code link document.querySelector("#view-code ").addEventListener("click ", () =>{ heap.track("PSADC - View Code "); }); // Add event listener to the install-pwa link document.querySelector("#install-pwa ").addEventListener("click ", () =>{ heap.track("PSADC - Install PWA "); }); } </script ></body ></html >"
);
console.log(escapedHtml);
function encodeHtmlInJson(jsonObj) {
// 检查是否为对象或数组
if (typeof jsonObj === "object" && jsonObj !== null) {
// 遍历对象的每个键
for (let key in jsonObj) {
if (jsonObj.hasOwnProperty(key)) {
// 递归调用处理嵌套的对象或数组
jsonObj[key] = encodeHtmlInJson(jsonObj[key]);
}
}
} else if (typeof jsonObj === "string" || typeof jsonObj === "number") {
// 将数值或字符串类型的值与指定的字符串连接
return encode(jsonObj.toString());
}
// 返回处理后的对象
return jsonObj;
}
function decodeHtmlInJson(jsonObj) {
// 检查是否为对象或数组
if (typeof jsonObj === "object" && jsonObj !== null) {
// 遍历对象的每个键
for (let key in jsonObj) {
if (jsonObj.hasOwnProperty(key)) {
// 递归调用处理嵌套的对象或数组
jsonObj[key] = decodeHtmlInJson(jsonObj[key]);
}
}
} else if (typeof jsonObj === "string" || typeof jsonObj === "number") {
// 将数值或字符串类型的值与指定的字符串连接
return decode(jsonObj.toString());
}
// 返回处理后的对象
return jsonObj;
}
// 示例使用
const inputJson = {
1: {
2: "3",
},
33: "44",
nested: {
level1: {
level2: "<>",
},
},
};
const appendString = "1";
const outputJson = encodeHtmlInJson(inputJson, appendString);
console.log(outputJson);