-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathoffscreen.js
More file actions
49 lines (43 loc) · 1.77 KB
/
offscreen.js
File metadata and controls
49 lines (43 loc) · 1.77 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
let engine = null;
function initEngine(threads, hash) {
console.log('[Neural Knight Offscreen] Initializing engine with threads:', threads, 'hash:', hash);
if (engine) {
console.log('[Neural Knight Offscreen] Terminating existing engine');
engine.terminate();
engine = null;
}
console.log('[Neural Knight Offscreen] Creating Stockfish worker...');
engine = new Worker('stockfish-engine.js');
engine.onmessage = (e) => {
if (typeof e.data === 'string') {
console.log('[Neural Knight Offscreen] Engine output:', e.data);
chrome.runtime.sendMessage({ type: 'engineOutput', line: e.data });
}
};
engine.onerror = (err) => {
console.error('[Neural Knight Offscreen] Worker error:', err.message || err);
};
console.log('[Neural Knight Offscreen] Sending UCI commands...');
engine.postMessage('uci');
engine.postMessage('setoption name Threads value ' + (threads || 4));
engine.postMessage('setoption name Hash value ' + (hash || 256));
engine.postMessage('isready');
}
chrome.runtime.onMessage.addListener((msg, sender, sendResponse) => {
console.log('[Neural Knight Offscreen] Received message:', msg.type);
if (msg.type === 'engineInit') {
initEngine(msg.threads, msg.hash);
sendResponse({ ok: true });
return true;
} else if (msg.type === 'engineCommand') {
console.log('[Neural Knight Offscreen] Engine command:', msg.command);
if (engine) {
engine.postMessage(msg.command);
} else {
console.error('[Neural Knight Offscreen] Engine not initialized!');
}
sendResponse({ ok: true });
return true;
}
return false;
});