mirror of
https://github.com/gchq/CyberChef.git
synced 2026-01-30 12:20:33 -08:00
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
2 changed files with 19 additions and 4 deletions
|
|
@ -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})`);
|
||||
|
|
|
|||
|
|
@ -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);
|
||||
}
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue