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:
parent
a4f40405ef
commit
eb684745fe
@ -44,6 +44,12 @@ class OpticalCharacterRecognition extends Operation {
|
|||||||
type: "option",
|
type: "option",
|
||||||
value: OEM_MODES,
|
value: OEM_MODES,
|
||||||
defaultIndex: 1
|
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}
|
* @returns {string}
|
||||||
*/
|
*/
|
||||||
async run(input, args) {
|
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");
|
if (!isWorkerEnvironment()) throw new OperationError("This operation only works in a browser");
|
||||||
|
|
||||||
@ -81,11 +87,15 @@ class OpticalCharacterRecognition extends Operation {
|
|||||||
});
|
});
|
||||||
self.sendStatusMessage("Finding text...");
|
self.sendStatusMessage("Finding text...");
|
||||||
const result = await worker.recognize(image);
|
const result = await worker.recognize(image);
|
||||||
|
let text = result?.data?.text ?? "";
|
||||||
|
if (removeLineBreaks) {
|
||||||
|
text = text.replace(/\r?\n/g, "");
|
||||||
|
}
|
||||||
|
|
||||||
if (showConfidence) {
|
if (showConfidence) {
|
||||||
return `Confidence: ${result.data.confidence}%\n\n${result.data.text}`;
|
return `Confidence: ${result.data.confidence}%\n\n${text}`;
|
||||||
} else {
|
} else {
|
||||||
return result.data.text;
|
return text;
|
||||||
}
|
}
|
||||||
} catch (err) {
|
} catch (err) {
|
||||||
throw new OperationError(`Error performing OCR on image. (${err})`);
|
throw new OperationError(`Error performing OCR on image. (${err})`);
|
||||||
|
|||||||
@ -1665,7 +1665,12 @@ class InputWaiter {
|
|||||||
*/
|
*/
|
||||||
handlePostMessage(e) {
|
handlePostMessage(e) {
|
||||||
log.debug(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") {
|
if (e.data.id === "setInput") {
|
||||||
this.setInput(e.data.value);
|
this.setInput(e.data.value);
|
||||||
}
|
}
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user