/ clis / jike / utils.js
utils.js
 1  /**
 2   * 即刻适配器公共定义
 3   *
 4   * JikePost 接口和 getPostData 函数在 feed.ts / search.ts 中复用,
 5   * 统一维护于此文件避免重复。
 6   */
 7  /**
 8   * 注入浏览器 evaluate 的 JS 函数字符串。
 9   * 从 React fiber 树中向上最多走 10 层,找到含 id 字段的 props.data。
10   */
11  export const getPostDataJs = `
12  function getPostData(element) {
13    for (const key of Object.keys(element)) {
14      if (key.startsWith('__reactFiber$') || key.startsWith('__reactInternalInstance$')) {
15        let fiber = element[key];
16        for (let i = 0; i < 10 && fiber; i++) {
17          const props = fiber.memoizedProps || fiber.pendingProps;
18          if (props && props.data && props.data.id) return props.data;
19          fiber = fiber.return;
20        }
21      }
22    }
23    return null;
24  }
25  `.trim();