-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathdebug.js
More file actions
74 lines (63 loc) · 2.17 KB
/
debug.js
File metadata and controls
74 lines (63 loc) · 2.17 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
// 简单的调试脚本来测试Chrome API是否正常工作
// 测试基本API可用性
console.log('=== Chrome Extension Debug Info ===');
console.log('chrome.runtime:', typeof chrome.runtime);
console.log('chrome.storage:', typeof chrome.storage);
console.log('chrome.declarativeNetRequest:', typeof chrome.declarativeNetRequest);
// 测试存储API
async function testStorage() {
try {
console.log('Testing storage API...');
await chrome.storage.sync.set({test: 'hello'});
const result = await chrome.storage.sync.get('test');
console.log('Storage test result:', result);
return true;
} catch (error) {
console.error('Storage API error:', error);
return false;
}
}
// 测试declarativeNetRequest API
async function testNetRequest() {
try {
console.log('Testing declarativeNetRequest API...');
const rules = await chrome.declarativeNetRequest.getDynamicRules();
console.log('Current rules:', rules);
return true;
} catch (error) {
console.error('NetRequest API error:', error);
return false;
}
}
// 测试消息通信
function testMessaging() {
return new Promise((resolve) => {
console.log('Testing messaging...');
// 发送测试消息
chrome.runtime.sendMessage({type: 'debug'}, (response) => {
console.log('Message response:', response);
console.log('Last error:', chrome.runtime.lastError);
resolve(response);
});
});
}
// 运行所有测试
async function runAllTests() {
console.log('Starting debug tests...');
console.log('\n--- Test 1: Storage API ---');
const storageOk = await testStorage();
console.log('\n--- Test 2: NetRequest API ---');
const netRequestOk = await testNetRequest();
console.log('\n--- Test 3: Messaging ---');
const messagingOk = await testMessaging();
console.log('\n=== Test Results ===');
console.log('Storage API:', storageOk ? '✅ OK' : '❌ FAILED');
console.log('NetRequest API:', netRequestOk ? '✅ OK' : '❌ FAILED');
console.log('Messaging:', messagingOk ? '✅ OK' : '❌ FAILED');
}
// 在页面加载后运行测试
if (typeof window !== 'undefined') {
window.addEventListener('load', runAllTests);
} else {
runAllTests();
}