File tree Expand file tree Collapse file tree
packages/chrome-extension-mock
app/service/service_worker Expand file tree Collapse file tree Original file line number Diff line number Diff line change 1+ export default class Action {
2+ getBadgeText ( ) {
3+ // 简单返回key作为测试值
4+ return "" ;
5+ }
6+ }
Original file line number Diff line number Diff line change @@ -10,6 +10,7 @@ import DeclarativeNetRequest from "./declarativ_net_request";
1010import Permissions from "./permissions" ;
1111import Extension from "./extension" ;
1212import MockUserScripts from "./user_scripts" ;
13+ import Action from "./action" ;
1314
1415const chromeMock = {
1516 tabs : new MockTab ( ) ,
@@ -24,6 +25,7 @@ const chromeMock = {
2425 permissions : new Permissions ( ) ,
2526 extension : new Extension ( ) ,
2627 userScripts : new MockUserScripts ( ) ,
28+ action : new Action ( ) ,
2729 init ( ) { } ,
2830} ;
2931
Original file line number Diff line number Diff line change @@ -337,6 +337,8 @@ export class RuntimeService {
337337 try {
338338 const res = await chrome . userScripts ?. getScripts ( { ids : [ "scriptcat-content" , "scriptcat-inject" ] } ) ;
339339 registered = res ?. length === 2 ;
340+ } catch {
341+ // 该错误为预期内情况,无需记录 debug 日志
340342 } finally {
341343 // 考虑 UserScripts API 不可使用等情况
342344 runtimeGlobal . registered = registered ;
Original file line number Diff line number Diff line change @@ -10,20 +10,28 @@ interface PopupWarningsProps {
1010
1111function PopupWarnings ( { isBlacklist } : PopupWarningsProps ) {
1212 const { t } = useTranslation ( ) ;
13- const [ isUserScriptsAvailableState , setIsUserScriptsAvailableState ] = useState ( false ) ;
13+ const [ isUserScriptsAvailableState , setIsUserScriptsAvailableState ] = useState < boolean | null > ( null ) ;
1414 const [ showRequestButton , setShowRequestButton ] = useState ( false ) ;
1515 const [ permissionReqResult , setPermissionReqResult ] = useState ( "" ) ;
1616
1717 const updateIsUserScriptsAvailableState = async ( ) => {
18- const flag = await checkUserScriptsAvailable ( ) ;
19- setIsUserScriptsAvailableState ( flag ) ;
18+ const badgeText = await chrome . action . getBadgeText ( { } ) ;
19+ let displayState ;
20+ if ( badgeText === "!" ) {
21+ // 要求用户重启扩展/浏览器,会重置badge状态的
22+ displayState = false ;
23+ } else {
24+ displayState = await checkUserScriptsAvailable ( ) ;
25+ }
26+ setIsUserScriptsAvailableState ( displayState ) ;
2027 } ;
2128
2229 useEffect ( ( ) => {
2330 updateIsUserScriptsAvailableState ( ) ;
2431 } , [ ] ) ;
2532
2633 const warningMessageHTML = useMemo ( ( ) => {
34+ if ( isUserScriptsAvailableState === null ) return "" ;
2735 // 可使用UserScript的话,不查browserType
2836 const browserType = ! isUserScriptsAvailableState ? getBrowserType ( ) : null ;
2937
Original file line number Diff line number Diff line change @@ -112,6 +112,7 @@ beforeEach(() => {
112112 callback ?.( mockTabs ) ;
113113 return Promise . resolve ( mockTabs ) ;
114114 } ) ;
115+ vi . spyOn ( chrome . action , "getBadgeText" ) . mockImplementation ( ( ) => Promise . resolve ( "" ) ) ;
115116} ) ;
116117
117118afterEach ( ( ) => {
You can’t perform that action at this time.
0 commit comments