1- import { getLarkConfig } from "./store" ;
1+ import { getLarkConfig , getLarkConfigSync } from "./store" ;
2+ import { checkCondition , LARK_DOMAIN_HOST } from "./utils" ;
3+ import { MSG_EVENT } from "./event" ;
24
35main ( ) ;
46
57async function main ( ) {
68 if ( ! checkCondition ( ) ) return ;
9+
10+ // {
11+ // [tid]: {
12+ // locker: Boolean,
13+ // error: Boolean,
14+ // data: Object,
15+ // }
16+ // }
17+ let cacheMap = new Map ( ) ;
718 const POPOVER_STYLE_ID = "lark-popover-link-style" ;
819 let dom_lark_popover = null ;
920
10- const LARK_DOMAIN_HOST = "https://project.feishu.cn" ;
11-
12- let LarkConfig = null ;
21+ await getLarkConfig ( ) ;
1322 const nodeMap = new Map ( ) ;
1423
24+ chrome . runtime . onMessage . addListener ( function ( e ) {
25+ const { message, data } = e ;
26+ switch ( message ) {
27+ case MSG_EVENT . GET_LARK_PROJECT_INFO :
28+ cacheMap . set ( data . tid , {
29+ locker : true ,
30+ error : data . error ,
31+ data : data . data ,
32+ } ) ;
33+ break ;
34+ }
35+ } ) ;
36+
1537 initPopover ( ) ;
1638 initPageListener ( ) ;
1739
18- // 检查是否满足条件
19- async function checkCondition ( ) {
20- const config = await getLarkConfig ( ) ;
21- if ( ! config ) return ;
22- LarkConfig = config ;
23- const domains = config . domain . split ( "," ) ;
24- if ( domains . length === 0 ) return false ;
25- return domains . some ( ( domain ) => {
26- return window . location . host . includes ( domain ) ;
27- } )
28- ? true
29- : false ;
30- }
31-
3240 // 初始化 popover 节点
3341 function initPopover ( ) {
3442 if ( document . getElementById ( POPOVER_STYLE_ID ) ) return ;
@@ -112,7 +120,17 @@ async function main() {
112120 dom_lark_popover . style . setProperty ( "top" , `${ rect . y } px` ) ;
113121 dom_lark_popover . style . setProperty ( "left" , `${ rect . x - 8 } px` ) ;
114122 dom_lark_popover . style . setProperty ( "transform" , `translate(0%, -102%)` ) ;
115- dom_lark_popover . innerHTML = "Lark Link" ;
123+ const tid = e . target . dataset . tid ;
124+ const cache = cacheMap . get ( tid ) ;
125+ let innerHTML = "飞书链接" ;
126+ if ( cache ) {
127+ if ( cache . data ) {
128+ innerHTML = cache . data . name ;
129+ } else if ( cache . error ) {
130+ innerHTML = "未找到相关信息" ;
131+ }
132+ }
133+ dom_lark_popover . innerHTML = innerHTML ;
116134 }
117135
118136 // 鼠标移出事件
@@ -128,11 +146,30 @@ async function main() {
128146
129147 // 获取 Lark 项目链接
130148 function getLarkProjectLink ( projectId , type = "m" ) {
149+ const LarkConfig = getLarkConfigSync ( ) ;
131150 if ( type === "f" )
132151 return `${ LARK_DOMAIN_HOST } /${ LarkConfig . app } /issue/detail/${ projectId } ` ;
133152 return `${ LARK_DOMAIN_HOST } /${ LarkConfig . app } /story/detail/${ projectId } ` ;
134153 }
135154
155+ function fetchLarkProjectInfo ( data ) {
156+ const { app, tid } = data ;
157+ if ( cacheMap . has ( tid ) && cacheMap . get ( tid ) . locker ) return ;
158+
159+ cacheMap . set ( tid , {
160+ locker : true ,
161+ error : false ,
162+ data : null ,
163+ } ) ;
164+ chrome . runtime . sendMessage ( {
165+ message : MSG_EVENT . GET_LARK_PROJECT_INFO ,
166+ data : {
167+ app,
168+ tid,
169+ } ,
170+ } ) ;
171+ }
172+
136173 // 替换项目 ID 为 Lark 项目链接
137174 function replaceProjectIdToLarkProjectLink ( dom , className ) {
138175 const reg = / ( m | f ) - \d + / g;
@@ -141,12 +178,17 @@ async function main() {
141178 const projectId = $1 . split ( "-" ) [ 1 ] ;
142179 const type = $1 . split ( "-" ) [ 0 ] ;
143180 isFind = true ;
181+ const LarkConfig = getLarkConfigSync ( ) ;
182+ const url = getLarkProjectLink ( projectId , type ) ;
183+ if ( $1 ) {
184+ fetchLarkProjectInfo ( {
185+ tid : $1 ,
186+ app : LarkConfig . app ,
187+ } ) ;
188+ }
144189 return `<a class='lark-project-link ${
145190 className ? className : ""
146- } ' href='${ getLarkProjectLink (
147- projectId ,
148- type
149- ) } ' target='_blank' data-project-id="${ projectId } " >${ $1 } </a>`;
191+ } ' href='${ url } ' target='_blank' data-tid="${ $1 } " >${ $1 } </a>`;
150192 } ) ;
151193 return [ isFind , content ] ;
152194 }
0 commit comments