You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

如何在访问Roblox随机游戏链接前校验有效性并筛选指定游戏?

解决方案

核心问题说明

直接fetch Roblox游戏页面会触发CORS跨域限制,这是浏览器的安全策略,Roblox前端页面不允许跨域请求。正确的做法是用Roblox官方提供的公开API来校验游戏有效性,既避免跨域问题,又能拿到准确的游戏信息。

实现步骤

  1. 使用Roblox的/games/v1/games/multiget-place-details API批量校验游戏ID的有效性
  2. 循环生成随机ID并校验,直到找到有效且不在排除列表中的游戏
  3. 增加加载状态提示,避免重复触发请求

完整代码

const connect = document.getElementById("connection");
const idsel = document.getElementById("gameid");
const findBtn = document.querySelector("button"); // 假设触发按钮是页面上的<button>
// 要排除的游戏ID列表
const excludedGameIds = [123456, 789012]; // 替换成你要排除的ID

async function findRandomValidGame() {
    // 禁用按钮,防止重复点击
    findBtn.disabled = true;
    idsel.textContent = "正在查找有效游戏...";

    let validGame = null;
    while (!validGame) {
        // 生成随机游戏ID(Roblox的Place ID范围可按需调整)
        const randomPlaceId = Math.floor(Math.random() * 1000000000);
        
        // 跳过排除列表中的ID
        if (excludedGameIds.includes(randomPlaceId)) continue;

        try {
            // 调用Roblox官方API校验游戏有效性
            const response = await fetch(`https://api.roblox.com/games/v1/games/multiget-place-details?placeIds=${randomPlaceId}`);
            const data = await response.json();

            // 检查返回结果:有效游戏会包含name字段,无效则返回空数组或无name
            if (data.length > 0 && data[0].name) {
                validGame = {
                    id: randomPlaceId,
                    name: data[0].name,
                    url: `https://www.roblox.com/games/${randomPlaceId}/`
                };
            }
        } catch (error) {
            // 网络请求失败时跳过当前ID,继续循环
            console.log(`校验ID ${randomPlaceId} 失败:`, error);
        }
    }

    // 更新页面内容
    connect.setAttribute("href", validGame.url);
    idsel.textContent = `找到有效游戏: ${validGame.name} (ID: ${validGame.id})`;
    // 恢复按钮可用状态
    findBtn.disabled = false;
}

// 绑定按钮点击事件
findBtn.addEventListener("click", findRandomValidGame);

关键细节说明

  • API选择:multiget-place-details API支持批量查询多个Place ID,单次请求即可完成校验,效率更高。返回结果包含游戏名称、创建者等信息,可扩展更多筛选规则(比如排除特定作者的游戏)。
  • 排除机制:通过excludedGameIds数组维护需要跳过的游戏ID,生成随机ID后先判断是否在列表中,直接跳过无需校验。
  • 加载状态:添加按钮禁用和文本提示,避免用户在查找过程中重复点击,提升体验。
  • 错误处理:捕获网络请求异常,确保循环不会因单次请求失败而终止。

内容的提问来源于stack exchange,提问作者user20423495

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.06.27 04:16:22