{"record":{"id":"f3dd3c5f35686c55","repo":"SubtitleEdit/subtitleedit","slug":"could-not-find-matching-af-initdatacallback","errorCode":null,"errorMessage":"Could not find matching AF_initDataCallback","messagePattern":"Could not find matching AF_initDataCallback","errorType":"exception","errorClass":"Exception","httpStatus":null,"severity":"warning","filePath":"src/ui/Logic/Ocr/GoogleLens/Core.cs","lineNumber":324,"sourceCode":"            {\n                if (DateTimeOffset.TryParse(subParts[1].Trim(), out var expires))\n                {\n                    cookie.Expires = expires.ToUnixTimeMilliseconds();\n                }\n            }\n        }\n\n        return cookie;\n    }\n\n    public static JObject? GetAFData(string text)\n    {\n        var matches = Regex.Matches(text, @\"AF_initDataCallback\\((\\{.*?\\})\\)\", RegexOptions.Singleline);\n        var lensCallback = matches.FirstOrDefault(m => m.Value.Contains(\"DetectedObject\"));\n        if (lensCallback == null)\n        {\n            Console.WriteLine(matches);\n            throw new Exception(\"Could not find matching AF_initDataCallback\");\n        }\n\n        var json = lensCallback.Groups[1].Value;\n        return JObject.Parse(json);\n    }\n\n    public static List<string> ParseResult(JObject afData, int[] imageDimensions)\n    {\n        var data = afData[\"data\"];\n        var fullTextPart = data?[3];\n        var textSegments = new List<string>();\n        var textRegions = new List<double[]>();\n\n        try\n        {\n            // Method 1: get text segments and regions directly\n            if (fullTextPart?[4]?[0]?[0] != null)\n            {","sourceCodeStart":306,"sourceCodeEnd":342,"githubUrl":"https://github.com/SubtitleEdit/subtitleedit/blob/17a9f0748781032255db3526b7215d2fb891e3af/src/ui/Logic/Ocr/GoogleLens/Core.cs#L306-L342","documentation":"Thrown by Core.GetAFData when the regex AF_initDataCallback({.*?}) finds no match whose value contains 'DetectedObject'. Lens embeds OCR results inside an AF_initDataCallback JS call tagged with DetectedObject; if none is present, either Lens found no text or the response structure changed. The matches are dumped to the console before throwing to aid debugging.","triggerScenarios":"A 200 Lens response with no detected objects (blank/no-text image), a response that is a captcha/interstitial, or a schema change where the DetectedObject marker was renamed. Also when the response is HTML error content.","commonSituations":"OCR'ing an image with no recognizable text; Google changed the callback key; the response is a consent/captcha page returned with status 200; regional response variants.","solutions":["Check the console output of matches to see what callbacks Lens did return.","Treat 'no DetectedObject' as 'no text found' rather than a hard error for blank images.","Update the regex / marker string if Google renamed DetectedObject.","Verify the image actually contains text; retry with a higher-contrast crop."],"exampleFix":"// before\nvar af = Core.GetAFData(text);\n\n// after\nvar af = Core.GetAFData(text); // wrap:\ntry { var af = Core.GetAFData(text); }\ncatch (Exception ex) when (ex.Message.Contains(\"AF_initDataCallback\")) { return new List<string>(); }","handlingStrategy":"try-catch","validationCode":null,"typeGuard":"static bool HasDetectedObjectCallback(string text) => Regex.IsMatch(text, @\"AF_initDataCallback\\(\\{.*?DetectedObject.*?\\}\\)\", RegexOptions.Singleline);","tryCatchPattern":"try { var af = Core.GetAFData(text); }\ncatch (Exception ex) when (ex.Message.Contains(\"AF_initDataCallback\")) { return new List<string>(); // treat as no text found }","preventionTips":["Treat 'no DetectedObject' as an empty result for blank images rather than a fatal error.","Log the console matches dump to detect schema changes.","Update the regex/marker when Google renames the callback.","Confirm the image actually contains legible text."],"tags":["google-lens","ocr","parsing","regex","schema","no-text"],"backgroundTag":null,"analyzedSha":"17a9f0748781032255db3526b7215d2fb891e3af","analyzedAt":"2026-08-13T18:11:43.374Z","schemaVersion":2},"datasetVersion":"2026-08-13T19:17:28.613Z"}