shouldInject($response)) { $originalView = $response->original ?? null; $injectedContent = $this->injectScript($response->getContent()); $response->setContent($injectedContent); if ($originalView instanceof View && property_exists($response, 'original')) { $response->original = $originalView; } } return $response; } protected function shouldInject(Response $response): bool { $responseTypes = [ StreamedResponse::class, BinaryFileResponse::class, JsonResponse::class, RedirectResponse::class, ]; foreach ($responseTypes as $type) { if ($response instanceof $type) { return false; } } if (! str_contains((string) $response->headers->get('content-type', ''), 'html')) { return false; } $content = $response->getContent(); // Check if it's HTML if (! str_contains($content, ' if (str_contains($content, '')) { return str_replace('', $script."\n", $content); } // Fallback: inject before closing if (str_contains($content, '')) { return str_replace('', $script."\n", $content); } return $content.$script; } }