feat: add right-click restorer for current site

- Add RightClickRestorer page with active unlock trigger
- Content script with dual-world strategy (isolated + main world)
- Monkey patch preventDefault/stopPropagation via background executeScript
- Overlay penetration for pointer-events blocking layers
- Delegated main-world injection to avoid CSP restrictions
- Async message handling for reliable state updates
- i18n translations (zh/en) and unit tests
This commit is contained in:
雨霖铃
2026-05-23 12:39:14 +08:00
parent 2d7bb8fc2c
commit fa0f1bdcad
16 changed files with 570 additions and 13 deletions
@@ -0,0 +1,62 @@
import { renderHook, act, waitFor } from '@testing-library/react';
import { useRightClickRestorer } from '../useRightClickRestorer';
import { sendMessageToContent } from '@/utils/messages';
const mockTabsQuery = vi.fn();
vi.mock('@/utils/messages', () => ({
MessageAction: {
RESTORE_RIGHT_CLICK: 'restoreRightClick',
QUERY_RIGHT_CLICK_STATUS: 'queryRightClickStatus',
},
sendMessageToContent: vi.fn(),
}));
beforeEach(() => {
vi.clearAllMocks();
mockTabsQuery.mockResolvedValue([{ url: 'https://example.com/path' }]);
chrome.tabs.query = mockTabsQuery;
vi.mocked(sendMessageToContent).mockResolvedValue({ success: true, restored: false });
});
describe('useRightClickRestorer', () => {
it('should load domain and query status', async () => {
const { result } = renderHook(() => useRightClickRestorer());
await waitFor(() => expect(result.current.isLoading).toBe(false));
expect(result.current.domain).toBe('example.com');
expect(result.current.isUnlocked).toBe(false);
expect(sendMessageToContent).toHaveBeenCalledWith('queryRightClickStatus');
});
it('should unlock right click', async () => {
vi.mocked(sendMessageToContent).mockResolvedValueOnce({ success: true, restored: false });
vi.mocked(sendMessageToContent).mockResolvedValueOnce({ success: true, restored: true });
const { result } = renderHook(() => useRightClickRestorer());
await waitFor(() => expect(result.current.isLoading).toBe(false));
await act(async () => {
await result.current.unlock();
});
expect(result.current.isUnlocked).toBe(true);
expect(sendMessageToContent).toHaveBeenLastCalledWith('restoreRightClick');
});
it('should handle sendMessage failure gracefully', async () => {
vi.mocked(sendMessageToContent).mockRejectedValue(new Error('Connection failed'));
const { result } = renderHook(() => useRightClickRestorer());
await waitFor(() => expect(result.current.isLoading).toBe(false));
await act(async () => {
await result.current.unlock();
});
expect(result.current.isUnlocked).toBe(false);
});
});