19b7ab141a
- 集成 Chrome 调试器 API 支持 attach/detach 功能 - 新增 tabUtils 工具模块用于获取活动标签页 ID - 在背景脚本中添加标签页激活和更新事件监听 - 使用 Material-UI 容器和堆叠组件重构页面布局 - 移除 Chromium 启动参数中的自动打开开发者工具选项 - 更新权限配置添加调试器相关权限 - 在测试页面添加多个调试功能按钮和状态管理
87 lines
2.3 KiB
TypeScript
87 lines
2.3 KiB
TypeScript
import { sendMessage } from '@/utils/messages';
|
|
import { Button } from '@mui/material';
|
|
import { useState, useEffect } from 'react';
|
|
|
|
const TestPage = () => {
|
|
const [tabId, setTabId] = useState(-1);
|
|
|
|
useEffect(() => {
|
|
(async () => {
|
|
const activeTabs = await chrome.tabs.query({
|
|
active: true,
|
|
currentWindow: true,
|
|
});
|
|
console.log(`activeTabs:`, activeTabs[0].id);
|
|
if (activeTabs[0].id && activeTabs[0].id > 0) setTabId(activeTabs[0].id);
|
|
else throw new Error('no active tab');
|
|
})();
|
|
});
|
|
|
|
const handleSeedMessage = async () => {
|
|
const status = await sendMessage('popup:check-status');
|
|
console.log(`[popup]status: ${status}`);
|
|
};
|
|
|
|
const handleAttach = () => {
|
|
try {
|
|
chrome.debugger.attach({ tabId: tabId }, '1.2', () => {
|
|
console.log('[debugger] attached');
|
|
});
|
|
} catch (err) {
|
|
console.error(err);
|
|
}
|
|
};
|
|
|
|
const handleDetach = async () => {
|
|
try {
|
|
chrome.debugger.detach({ tabId: tabId }, () => {
|
|
console.log('[debugger] detached');
|
|
});
|
|
} catch (err) {
|
|
console.error(err);
|
|
}
|
|
};
|
|
|
|
const handleGetTarget = () => {
|
|
chrome.debugger.getTargets((targets) => {
|
|
console.log('[debugger] targets:', targets);
|
|
});
|
|
};
|
|
|
|
const enableRuntime = () => {
|
|
chrome.debugger.sendCommand({ tabId: tabId }, 'Runtime.enable', {}, (res) => {
|
|
console.log('[debugger] Runtime.enable:', res);
|
|
});
|
|
};
|
|
|
|
const disableRuntime = () => {
|
|
chrome.debugger.sendCommand({ tabId: tabId }, 'Runtime.disable', {}, (res) => {
|
|
console.log('[debugger] Runtime.disable:', res);
|
|
});
|
|
};
|
|
return (
|
|
<div>
|
|
<Button variant="contained" color="primary" onClick={handleSeedMessage}>
|
|
发送消息
|
|
</Button>
|
|
<Button variant="contained" color="primary" onClick={handleAttach}>
|
|
attach
|
|
</Button>
|
|
<Button variant="contained" color="primary" onClick={handleDetach}>
|
|
detach
|
|
</Button>
|
|
<Button variant="contained" color="primary" onClick={handleGetTarget}>
|
|
getTargets
|
|
</Button>
|
|
<Button variant="contained" color="primary" onClick={enableRuntime}>
|
|
enableRuntime
|
|
</Button>
|
|
<Button variant="contained" color="primary" onClick={disableRuntime}>
|
|
disableRuntime
|
|
</Button>
|
|
</div>
|
|
);
|
|
};
|
|
|
|
export default TestPage;
|