Skip to content

修改 messageFlag 格式避免与其他页面代码衝突 #561

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Jul 21, 2025

Conversation

cyfung1031
Copy link
Contributor

@cyfung1031 cyfung1031 commented Jul 19, 2025

現在使用

export function randomString(e = 32): string {
  const t = "ABCDEFGHJKMNPQRSTWXYZabcdefhijkmnprstwxyz";
  const a = t.length;
  const n = new Array(e);
  for (let i = 0; i < e; i++) {
    n[i] = t[(Math.random() * a) | 0];
  }
  return n.join("");
}

randomString(16)
這個雖然達到 41^16 (6.376 * 10^25) 個組合,但鍵都是英文字,而且執行時要運用new Array loop 等方式,效率低

function randNum(a: number, b: number) {
  return Math.floor(Math.random() * (b - a + 1)) + a;
}

export function randomMessageFlag(): string {
  // parseInt('a0000000', 36) = 783641640960;
  // parseInt('zzzzzzzz', 36) = 2821109907455;
  return `-${Date.now().toString(36)}.${randNum(8e11, 2e12).toString(36)}`;
}

新寫法是利用 .toString(36) 直接轉換,效率高
採用 "-xxxxxxxx.xxxxxxxx" 格式,不容易引起衝突(例如頁面本身也使用 XXXXXXXXXXXXXXXX 這種鍵)
而且沙盒內的全域變數生成/刪除也不能改 "-" 開首的

Date.now() 是递增特性。後面的 randNum(8e11, 2e12)能產生一個不易重覆的8位字串 (1.2 x 10^12 組合)。

  • 後面的可以改成 randNum(2e14, 9e15) -> 8.8 * 10^15 組合

@CodFrm CodFrm merged commit 182a631 into scriptscat:main Jul 21, 2025
1 of 2 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants