將製作ejcees(中國象棋打譜程式)

分享和讨论Javascript相关的话题
回复
头像
ejsoon
圈圈精英
圈圈精英
帖子: 4985
注册时间: 2022年 11月 18日 17:36
为圈友点赞: 173 次
被圈友点赞: 202 次
联系:

Re: 將製作ejcees(中國象棋打譜程式)

帖子 ejsoon »

ejceesExpSvg不能轉json。
https://ejsoon.vip/
弈趣極光:享受思維樂趣
头像
ejsoon
圈圈精英
圈圈精英
帖子: 4985
注册时间: 2022年 11月 18日 17:36
为圈友点赞: 173 次
被圈友点赞: 202 次
联系:

Re: 將製作ejcees(中國象棋打譜程式)

帖子 ejsoon »

代码: 全选

(function() {
    const currentHash = window.location.hash;
    if (currentHash) {
        const targetLink = document.querySelector('a.ejcees');
        if (targetLink && targetLink.href) {
            const newUrl = targetLink.href + currentHash;
            window.location.href = newUrl;
        }
    }
})();
改進以上代碼,在加上hash前,要先加上?url=當前地址的encodeUricomponer轉碼。
https://ejsoon.vip/
弈趣極光:享受思維樂趣
头像
ejsoon
圈圈精英
圈圈精英
帖子: 4985
注册时间: 2022年 11月 18日 17:36
为圈友点赞: 173 次
被圈友点赞: 202 次
联系:

Re: 將製作ejcees(中國象棋打譜程式)

帖子 ejsoon »

gemini fast

代码: 全选

(function() {
    const currentHash = window.location.hash;
    // 獲取當前完整 URL 並進行編碼
    const encodedCurrentUrl = encodeURIComponent(window.location.href);

    if (currentHash) {
        const targetLink = document.querySelector('a.ejcees');
        
        if (targetLink && targetLink.href) {
            let baseUrl = targetLink.href;

            // 判斷原連結是否已經帶有參數,決定用 '?' 還是 '&' 拼接
            const separator = baseUrl.includes('?') ? '&' : '?';
            
            // 拼接順序:原連結 + ?url=當前地址 + #Hash
            const newUrl = `${baseUrl}${separator}url=${encodedCurrentUrl}${currentHash}`;
            
            window.location.href = newUrl;
        }
    }
})();
https://ejsoon.vip/
弈趣極光:享受思維樂趣
头像
ejsoon
圈圈精英
圈圈精英
帖子: 4985
注册时间: 2022年 11月 18日 17:36
为圈友点赞: 173 次
被圈友点赞: 202 次
联系:

Re: 將製作ejcees(中國象棋打譜程式)

帖子 ejsoon »

url點第二次會出現中文,而不再編碼。
https://ejsoon.vip/
弈趣極光:享受思維樂趣
头像
ejsoon
圈圈精英
圈圈精英
帖子: 4985
注册时间: 2022年 11月 18日 17:36
为圈友点赞: 173 次
被圈友点赞: 202 次
联系:

Re: 將製作ejcees(中國象棋打譜程式)

帖子 ejsoon »

當程式boot the app,拿到hash時,順帶也拿到?url=...(如果有),之後把地址欄中?後面的以及#後面的字符都去掉。
https://ejsoon.vip/
弈趣極光:享受思維樂趣
头像
ejsoon
圈圈精英
圈圈精英
帖子: 4985
注册时间: 2022年 11月 18日 17:36
为圈友点赞: 173 次
被圈友点赞: 202 次
联系:

Re: 將製作ejcees(中國象棋打譜程式)

帖子 ejsoon »

找到程式Boot the app部分,在最下面的地方。有一段代碼是拿到hash,現在順帶也拿到?url=...(如果有),之後把地址欄中?後面的以及#後面的字符都去掉。

只需給出所要修改的地方,所有的代碼和注釋都要使用英文。

代码: 全选

根據您的要求,我找到了位於程式碼最下方的「Boot the app」部分。

您需要修改 `window.addEventListener('DOMContentLoaded', ...)` 回調函數中處理 `window.location.hash` 的邏輯。

以下是修改後的程式碼片段(所有程式碼與註釋均使用英文):

```javascript
        // 1. Resolve initial state from localStorage or URL hash
        const savedState = localStorage.getItem(STORAGE_KEY);
        let initialHistory = null;
        
        // --- Modified Section: Handle URL parameters and Hash ---
        const urlParams = new URLSearchParams(window.location.search);
        if (urlParams.has('url')) {
            baseUrl = decodeURIComponent(urlParams.get('url'));
        }

        if (window.location.hash) {
            const hash = window.location.hash.substring(1);
            if (hash) {
                try {
                    initialHistory = parseUrlHash(hash);
                } catch (e) {
                    console.error("Failed to parse hash:", e);
                }
            }
        }

        // Clean up the address bar: remove everything after '?' and '#'
        if (window.location.search || window.location.hash) {
            const cleanUrl = window.location.protocol + "//" + window.location.host + window.location.pathname;
            window.history.replaceState({}, document.title, cleanUrl);
        }
        // --- End of Modified Section ---

        if (!initialHistory && savedState) {
            // ... (rest of the existing logic to load from savedState)
```

### 修改說明:
1.  **獲取 `?url=`**:使用 `URLSearchParams` 從 `window.location.search` 中提取 `url` 參數並解碼賦值給 `baseUrl`。
2.  **獲取 Hash**:保留原有的邏輯,從 `window.location.hash` 中提取並解析棋局數據。
3.  **清除地址欄**:使用 `window.history.replaceState` 將地址欄重置為僅包含 `protocol`、`host` 和 `pathname` 的路徑,從而去掉所有的查詢參數(`?` 後面)和片段標識符(`#` 後面)。
https://ejsoon.vip/
弈趣極光:享受思維樂趣
头像
ejsoon
圈圈精英
圈圈精英
帖子: 4985
注册时间: 2022年 11月 18日 17:36
为圈友点赞: 173 次
被圈友点赞: 202 次
联系:

Re: 將製作ejcees(中國象棋打譜程式)

帖子 ejsoon »

附件
ejcees20260406url9997775.7z
(45.58 KiB) 已下载 1 次
https://ejsoon.vip/
弈趣極光:享受思維樂趣
回复

在线用户

正浏览此版面之用户: 没有注册用户 和 1 访客