-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathstego-worker.js
More file actions
51 lines (44 loc) · 1.57 KB
/
Copy pathstego-worker.js
File metadata and controls
51 lines (44 loc) · 1.57 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
/* global importScripts, createImageBitmap, fetch, self */
importScripts('steganography.js');
async function loadImageFromPayload(payload = {}) {
if (payload.dataUrl) {
const response = await fetch(payload.dataUrl);
const blob = await response.blob();
return createImageBitmap(blob);
}
if (payload.fileBytes) {
const bytes = payload.fileBytes instanceof Uint8Array ? payload.fileBytes : new Uint8Array(payload.fileBytes);
const blob = new Blob([bytes], { type: payload.fileType || 'image/png' });
return createImageBitmap(blob);
}
throw new Error('No image payload provided for stego worker task');
}
self.addEventListener('message', async (event) => {
const { id, action, payload } = event.data || {};
try {
let result = null;
const image = await loadImageFromPayload(payload || {});
if (action === 'encode') {
result = await Steganography.encode(
image,
payload.message,
payload.password || '',
payload.algorithm || 'lsb',
payload.outputOptions || {}
);
} else if (action === 'decode') {
result = await Steganography.decode(
image,
payload.password || '',
payload.algorithm || 'auto'
);
} else if (action === 'hasHiddenContent') {
result = await Steganography.hasHiddenContent(image);
} else {
throw new Error(`Unsupported worker action: ${action}`);
}
self.postMessage({ id, ok: true, result });
} catch (error) {
self.postMessage({ id, ok: false, error: error && error.message ? error.message : String(error) });
}
});