Skip to content

Commit cc3b827

Browse files
committed
feat: 新增 prefetch_list 数据源支持
1 parent fd7849c commit cc3b827

File tree

2 files changed

+88
-9
lines changed

2 files changed

+88
-9
lines changed

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
{
22
"name": "gitlab-link-to-lark",
33
"description": "一个浏览器插件,用于提供 GitLab 项目中关联的飞书项目 ID 转换为飞书链接。方便在 GitLab 项目中快速跳转查看飞书项目信息 --- build for cyy(づ ̄3 ̄)づ╭❤~",
4-
"version": "1.2.0",
4+
"version": "1.3.0",
55
"scripts": {
66
"build": "NODE_ENV=production gulp build",
77
"dev": "NODE_ENV=development gulp dev"

src/js/background.js

Lines changed: 87 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -29,12 +29,20 @@ async function main() {
2929
});
3030
}
3131

32-
async function getLarkProjectInfo({ tid, app }) {
33-
const [t, id] = tid.split("-");
34-
const type = t === "m" ? "story" : "issue";
35-
let url = `${LARK_DOMAIN_HOST}/${app}/${type}/detail/${id}`;
36-
const res = await fetch(url);
37-
const text = await res.text();
32+
const extractScriptContent = (text) => {
33+
// 使用正则表达式匹配 script 标签内的内容
34+
const scriptRegex = /<script[^>]*>([\s\S]*?)<\/script>/;
35+
const match = text.match(scriptRegex);
36+
37+
if (match && match[1]) {
38+
// 返回标签内的内容
39+
return match[1].trim();
40+
}
41+
42+
return ""; // 如果没有找到匹配,返回空字符串
43+
};
44+
45+
const getLarkProjectInfoByDetail = (text) => {
3846
const reg = /<script\b[^<]*(?:(?!<\/script>)<[^<]*)*<\/script>/gi;
3947
const scripts = text.match(reg);
4048
let content = "";
@@ -52,7 +60,6 @@ async function getLarkProjectInfo({ tid, app }) {
5260
if (!content)
5361
return {
5462
error: true,
55-
tid,
5663
data: null,
5764
};
5865
content = content.replace(/\n/g, "");
@@ -73,9 +80,81 @@ async function getLarkProjectInfo({ tid, app }) {
7380
data = obj[key].data;
7481
return {
7582
error: false,
76-
tid,
7783
data,
7884
};
85+
};
86+
87+
const getLarkProjectInfoByPrefetchList = (text) => {
88+
const reg = /<script\b[^<]*(?:(?!<\/script>)<[^<]*)*<\/script>/gi;
89+
const scripts = text.match(reg);
90+
let content = "";
91+
for (let i = 0; i < scripts.length; i++) {
92+
const text = scripts[i];
93+
const isContain =
94+
text.includes('...{"APIDemandFetchWorkItem":') &&
95+
text.includes(`window.prefetch_list`);
96+
97+
if (isContain) {
98+
content = text;
99+
break;
100+
}
101+
}
102+
if (!content)
103+
return {
104+
error: true,
105+
data: null,
106+
};
107+
content = content.replace(/\n/g, "");
108+
content = content.replace(new RegExp("\\\\x3C", "g"), "<");
109+
content = content.replace(new RegExp("\x3C", "g"), "<");
110+
content = content.replace(new RegExp("\\x3C", "g"), "<");
111+
const noScriptContent = extractScriptContent(content);
112+
if (noScriptContent) {
113+
content = noScriptContent;
114+
}
115+
content = content.replace("window.prefetch_list = {", "");
116+
content = content.replace("...window.prefetch_list,", "");
117+
content = content.replace("...{", "{");
118+
content = content.replace("} };", "}");
119+
content = content.replace(/\\'/g, "'");
120+
const obj = JSON.parse(content);
121+
const biz_data = obj.APIDemandFetchWorkItem.data.data.biz_data;
122+
const target = biz_data.find((item) => item.key === "workitem");
123+
if (!target) {
124+
return {
125+
error: true,
126+
data: null,
127+
};
128+
}
129+
return {
130+
error: false,
131+
data: {
132+
data: target.value,
133+
},
134+
};
135+
};
136+
137+
async function getLarkProjectInfo({ tid, app }) {
138+
const [t, id] = tid.split("-");
139+
const type = t === "m" ? "story" : "issue";
140+
let url = `${LARK_DOMAIN_HOST}/${app}/${type}/detail/${id}`;
141+
const res = await fetch(url);
142+
const text = await res.text();
143+
let info = { tid };
144+
const detailInfo = getLarkProjectInfoByDetail(text);
145+
if (detailInfo.data) {
146+
info = {
147+
...info,
148+
...detailInfo.data,
149+
};
150+
} else {
151+
const prefetchListInfo = getLarkProjectInfoByPrefetchList(text);
152+
info = {
153+
...info,
154+
...prefetchListInfo.data,
155+
};
156+
}
157+
return info;
79158
}
80159

81160
chrome.runtime.onInstalled.addListener((details) => {

0 commit comments

Comments
 (0)