@@ -593,13 +593,7 @@ export class PopupService {
593593 this . dealBackgroundScriptInstall ( ) ;
594594
595595 // 监听tab开关
596- chrome . tabs . onRemoved . addListener ( ( tabId ) => {
597- const lastError = chrome . runtime . lastError ;
598- if ( lastError ) {
599- console . error ( "chrome.runtime.lastError in chrome.tabs.onRemoved:" , lastError ) ;
600- // 没有 tabId 资讯,无法释放数据
601- return ;
602- }
596+ const clearData = async ( tabId : number ) => {
603597 runCountMap . delete ( tabId ) ;
604598 scriptCountMap . delete ( tabId ) ;
605599 const list = this . updateMenuCommands . get ( tabId ) ;
@@ -609,8 +603,9 @@ export class PopupService {
609603 this . updateMenuCommands . delete ( tabId ) ;
610604 }
611605 // 清理数据tab关闭需要释放的数据
612- cacheInstance . tx ( `${ CACHE_KEY_TAB_SCRIPT } ${ tabId } ` , ( scripts : ScriptMenu [ ] | undefined ) => {
606+ cacheInstance . tx ( `${ CACHE_KEY_TAB_SCRIPT } ${ tabId } ` , ( scripts : ScriptMenu [ ] | undefined , tx ) => {
613607 if ( scripts ) {
608+ tx . del ( ) ;
614609 return Promise . all (
615610 scripts . map ( ( { uuid } ) => {
616611 // 处理GM_saveTab关闭事件, 由于需要用到tab相关的脚本数据,所以需要在这里处理
@@ -625,6 +620,15 @@ export class PopupService {
625620 ) ;
626621 }
627622 } ) ;
623+ } ;
624+ chrome . tabs . onRemoved . addListener ( ( tabId ) => {
625+ const lastError = chrome . runtime . lastError ;
626+ if ( lastError ) {
627+ console . error ( "chrome.runtime.lastError in chrome.tabs.onRemoved:" , lastError ) ;
628+ // 没有 tabId 资讯,无法释放数据
629+ return ;
630+ }
631+ clearData ( tabId ) ;
628632 } ) ;
629633 // 监听页面切换加载菜单
630634 // 进程启动时可能尚未触发 onActivated:补一次初始化以建立当前 tab 的菜单与 badge。
@@ -649,24 +653,20 @@ export class PopupService {
649653 this . genScriptMenu ( ) ;
650654 this . updateBadgeIcon ( ) ;
651655 } ) ;
652- // chrome.tabs.onUpdated.addListener((tabId, _changeInfo, _tab) => {
653- // const lastError = chrome.runtime.lastError;
654- // if (lastError) {
655- // console.error("chrome.runtime.lastError in chrome.tabs.onUpdated:", lastError);
656- // // 没有 tabId 资讯,无法加载菜单
657- // return;
658- // }
659- // this.updateBadgeIcon(tabId);
660- // });
661- // chrome.windows.onFocusChanged.addListener((_windowId) => {
662- // const lastError = chrome.runtime.lastError;
663- // if (lastError) {
664- // console.error("chrome.runtime.lastError in chrome.windows.onFocusChanged:", lastError);
665- // // 没有 tabId 资讯,无法加载菜单
666- // return;
667- // }
668- // this.updateBadgeIcon(-1);
669- // });
656+
657+ chrome . webNavigation . onBeforeNavigate . addListener ( ( details ) => {
658+ const lastError = chrome . runtime . lastError ;
659+ if ( lastError ) {
660+ console . error ( "chrome.runtime.lastError in chrome.webNavigation.onBeforeNavigate:" , lastError ) ;
661+ return ;
662+ }
663+ // 没有开启脚本时不会触发pageLoad更新数据
664+ // 这里做一次清理
665+ if ( runCountMap . has ( details . tabId ) && ! this . runtime . isLoadScripts ) {
666+ clearData ( details . tabId ) ;
667+ }
668+ } ) ;
669+
670670 // 处理chrome菜单点击
671671 chrome . contextMenus . onClicked . addListener ( async ( info , tab ) => {
672672 const lastError = chrome . runtime . lastError ;
@@ -704,9 +704,6 @@ export class PopupService {
704704 }
705705 } ) ;
706706
707- // scriptCountMap.clear();
708- // runCountMap.clear();
709-
710707 // 监听运行次数
711708 // 监听页面载入事件以更新脚本执行计数;若为当前活动 tab,同步刷新 badge。
712709 this . mq . subscribe (
0 commit comments