-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathoffscreen.js
More file actions
133 lines (112 loc) · 3.92 KB
/
offscreen.js
File metadata and controls
133 lines (112 loc) · 3.92 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
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
let stockfishWorker = null;
let currentResolve = null;
let currentAudio = null;
let latestEval = 0;
let latestMate = null;
let currentActiveColor = 'w';
function getStockfishWorker() {
if (stockfishWorker) return stockfishWorker;
const workerUrl = chrome.runtime.getURL('stockfish/stockfish.js');
stockfishWorker = new Worker(workerUrl);
stockfishWorker.onmessage = (event) => {
const line = event.data;
if (line.startsWith('info') && line.includes('score')) {
if (line.includes('score cp ')) {
const match = line.match(/score cp (-?\d+)/);
if (match) {
latestEval = parseInt(match[1]) / 100;
latestMate = null;
}
} else if (line.includes('score mate ')) {
const match = line.match(/score mate (-?\d+)/);
if (match) {
latestMate = parseInt(match[1]);
latestEval = 0;
}
}
}
if (line.startsWith('bestmove')) {
const parts = line.split(' ');
const bestMove = parts[1];
if (currentResolve) {
let finalEval = latestEval;
let finalMate = latestMate;
if (currentActiveColor === 'b') {
finalEval = -latestEval;
if (latestMate !== null) {
finalMate = -latestMate;
}
}
currentResolve({
best: bestMove,
eval: finalEval,
mate: finalMate
});
currentResolve = null;
}
}
};
stockfishWorker.postMessage('uci');
stockfishWorker.postMessage('isready');
stockfishWorker.postMessage('ucinewgame');
return stockfishWorker;
}
chrome.runtime.onMessage.addListener((message, sender, sendResponse) => {
if (message.target !== 'offscreen') return;
if (message.type === 'SET_VISIBILITY') {
if (currentAudio && currentAudio.src.includes('sf.mp3')) {
if (message.visible) {
currentAudio.play().catch(() => {});
} else {
currentAudio.pause();
}
}
return;
}
if (message.type === 'STOP_SOUND') {
if (currentAudio) {
currentAudio.pause();
currentAudio.currentTime = 0;
currentAudio = null;
}
return;
}
if (message.type === 'PLAY_SOUND') {
try {
if (currentAudio && !currentAudio.paused && !currentAudio.ended) {
return;
}
currentAudio = new Audio(chrome.runtime.getURL(`assets/${message.sound}`));
currentAudio.volume = 0.55;
currentAudio.play().catch(() => {});
} catch (_) {}
return;
}
if (message.type === 'ANALYZE') {
const { fen, depth, requestId, tabId } = message;
const worker = getStockfishWorker();
latestEval = 0;
latestMate = null;
currentActiveColor = fen.split(' ')[1] || 'w';
if (currentResolve) {
try {
worker.postMessage('stop');
} catch (_) {}
currentResolve({ eval: 0, mate: null, best: null });
}
const analysisPromise = new Promise((resolve) => {
currentResolve = resolve;
});
worker.postMessage(`position fen ${fen}`);
worker.postMessage(`go depth ${depth}`);
analysisPromise.then((result) => {
chrome.runtime.sendMessage({
target: 'background',
type: 'ANALYSIS_RESULT',
requestId,
tabId,
data: result
});
});
}
});