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

如何在UserScript中访问预加载的JSON变量

如何访问预加载的JSON内容并存入变量?

嘿,我来帮你搞定这个问题!你要做的其实很简单,分两步就能把预加载的JSON内容存到变量里:

  • 定位目标script标签:因为你的预加载脚本带有data-preload-id="time_zones"这个唯一标识,我们可以用它来精准定位元素:

    const preloadScript = document.querySelector('script[data-preload-id="time_zones"]');
    
  • 解析JSON内容:拿到这个script元素后,提取它的文本内容,再用JSON.parse()把字符串转换成JavaScript对象,就能存入变量啦:

    let timeZonesData;
    if (preloadScript) {
      try {
        timeZonesData = JSON.parse(preloadScript.textContent);
        // 现在你可以通过timeZonesData.time_zones访问时区数组
        console.log(timeZonesData.time_zones);
      } catch (error) {
        console.error('解析JSON失败:', error);
      }
    } else {
      console.error('找不到预加载的script标签');
    }
    

我加了一些容错处理:比如判断元素是否存在,用try-catch捕获解析错误,这样能避免脚本因为意外情况直接崩溃。

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.05.25 08:14:45