fix(InputWaiter): guard against non-object e.data in postMessage (handles HMR strings); feat(ocr): add 'Remove line breaks' checkbox to return text without newlines

This commit is contained in:
Izai Alejandro Zalles Merino 2025-10-13 14:59:09 -04:00
parent a4f40405ef
commit eb684745fe
2 changed files with 19 additions and 4 deletions

View file

@ -44,6 +44,12 @@ class OpticalCharacterRecognition extends Operation {
type: "option",
value: OEM_MODES,
defaultIndex: 1
},
// New option appended to avoid breaking existing saved recipes
{
name: "Remove line breaks",
type: "boolean",
value: false
}
];
}
@ -54,7 +60,7 @@ class OpticalCharacterRecognition extends Operation {
* @returns {string}
*/
async run(input, args) {
const [showConfidence, oemChoice] = args;
const [showConfidence, oemChoice, removeLineBreaks = false] = args;
if (!isWorkerEnvironment()) throw new OperationError("This operation only works in a browser");
@ -81,11 +87,15 @@ class OpticalCharacterRecognition extends Operation {
});
self.sendStatusMessage("Finding text...");
const result = await worker.recognize(image);
let text = result?.data?.text ?? "";
if (removeLineBreaks) {
text = text.replace(/\r?\n/g, "");
}
if (showConfidence) {
return `Confidence: ${result.data.confidence}%\n\n${result.data.text}`;
return `Confidence: ${result.data.confidence}%\n\n${text}`;
} else {
return result.data.text;
return text;
}
} catch (err) {
throw new OperationError(`Error performing OCR on image. (${err})`);

View file

@ -1665,7 +1665,12 @@ class InputWaiter {
*/
handlePostMessage(e) {
log.debug(e);
if ("data" in e && "id" in e.data && "value" in e.data) {
// Guard against non-object events (e.g., HMR messages may set e.data to a string like 'webpackHotUpdate...')
if (
e && typeof e === "object" &&
"data" in e && e.data && typeof e.data === "object" &&
"id" in e.data && "value" in e.data
) {
if (e.data.id === "setInput") {
this.setInput(e.data.value);
}