镜像自地址
https://github.com/lhly/search-syntax-pro.git
已同步 2026-10-20 14:27:47 +08:00
fix(TriggerIcon): 添加扩展上下文检查避免无效操作
在恢复和保存位置前检查扩展上下文是否有效 添加处理扩展上下文失效错误的逻辑
这个提交包含在:
父节点
2e5f50251d
当前提交
e0bf4e5f35
@ -72,7 +72,6 @@
|
|||||||
"floating-panel.css",
|
"floating-panel.css",
|
||||||
"useTheme.js",
|
"useTheme.js",
|
||||||
"useTheme.css",
|
"useTheme.css",
|
||||||
"QueryPreview.js",
|
|
||||||
"search-engine-selectors.js"
|
"search-engine-selectors.js"
|
||||||
],
|
],
|
||||||
"matches": [
|
"matches": [
|
||||||
|
|||||||
@ -85,6 +85,12 @@ export class TriggerIcon {
|
|||||||
*/
|
*/
|
||||||
private async restorePosition(): Promise<void> {
|
private async restorePosition(): Promise<void> {
|
||||||
try {
|
try {
|
||||||
|
if (!this.isExtensionContextValid()) {
|
||||||
|
console.warn('[SSP] Skip restoring position: extension context unavailable');
|
||||||
|
this.setDefaultPosition();
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
const result = await chrome.storage.local.get('trigger_button_position');
|
const result = await chrome.storage.local.get('trigger_button_position');
|
||||||
const saved = result.trigger_button_position as TriggerButtonPosition | undefined;
|
const saved = result.trigger_button_position as TriggerButtonPosition | undefined;
|
||||||
|
|
||||||
@ -101,6 +107,11 @@ export class TriggerIcon {
|
|||||||
this.setDefaultPosition();
|
this.setDefaultPosition();
|
||||||
}
|
}
|
||||||
} catch (error) {
|
} catch (error) {
|
||||||
|
if (this.isContextInvalidationError(error)) {
|
||||||
|
console.warn('[SSP] Skip restoring position: extension context invalidated');
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
console.error('[SSP] Failed to restore position:', error);
|
console.error('[SSP] Failed to restore position:', error);
|
||||||
this.setDefaultPosition();
|
this.setDefaultPosition();
|
||||||
}
|
}
|
||||||
@ -253,9 +264,19 @@ export class TriggerIcon {
|
|||||||
};
|
};
|
||||||
|
|
||||||
try {
|
try {
|
||||||
|
if (!this.isExtensionContextValid()) {
|
||||||
|
console.warn('[SSP] Skip saving position: extension context unavailable');
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
await chrome.storage.local.set({ trigger_button_position: position });
|
await chrome.storage.local.set({ trigger_button_position: position });
|
||||||
console.log('[SSP] Position saved:', position);
|
console.log('[SSP] Position saved:', position);
|
||||||
} catch (error) {
|
} catch (error) {
|
||||||
|
if (this.isContextInvalidationError(error)) {
|
||||||
|
console.warn('[SSP] Skip saving position: extension context invalidated');
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
console.error('[SSP] Failed to save position:', error);
|
console.error('[SSP] Failed to save position:', error);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@ -321,4 +342,29 @@ export class TriggerIcon {
|
|||||||
this.element = null;
|
this.element = null;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 判断扩展上下文是否可用
|
||||||
|
*/
|
||||||
|
private isExtensionContextValid(): boolean {
|
||||||
|
return typeof chrome !== 'undefined' && !!chrome.runtime?.id;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 判断是否为扩展上下文失效错误
|
||||||
|
*/
|
||||||
|
private isContextInvalidationError(error: unknown): boolean {
|
||||||
|
if (!error) return false;
|
||||||
|
|
||||||
|
const message =
|
||||||
|
typeof error === 'string'
|
||||||
|
? error
|
||||||
|
: error instanceof Error
|
||||||
|
? error.message
|
||||||
|
: typeof (error as { message?: unknown }).message === 'string'
|
||||||
|
? String((error as { message?: unknown }).message)
|
||||||
|
: null;
|
||||||
|
|
||||||
|
return typeof message === 'string' && message.includes('Extension context invalidated');
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
正在加载...
在新工单中引用
屏蔽一个用户