src/CsrfMiddleware.php
private function injectJqueryToResponse(string $body, string $tokenValue): string
    {
        $regex = '/(.*?)(<\/body>)/is';
        $jQueryCode = sprintf(
            '<script>$.ajaxSetup({beforeSend: function (xhr) { xhr.setRequestHeader("X-CSRF-Token","%s"); }});</script>',
            $tokenValue
        );
        $body = preg_replace($regex, '$1' . $jQueryCode . '$2', $body, -1, $count) ?? '';

        if (!$count) {
            // Inject JS code anyway
            $body .= $jQueryCode;
        }

        return $body;
    }
private function injectJqueryToResponse(string $body, string $tokenValue): string
    {
        $regex = '/(.*?)(<\/body>)/is';
        $jQueryCode = sprintf(
            '<script>$.ajaxSetup({beforeSend: function (xhr) { xhr.setRequestHeader("X-CSRF-Token","%s"); }});</script>',
            $tokenValue
        );
        $body = preg_replace($regex, '$1' . $jQueryCode . '$2', $body, -1, $count) ?? '';

        if (!$count) {
            // Inject JS code anyway
            $body .= $jQueryCode;
        }

        return $body;
    }
private function injectJqueryToResponse ( string $ body , string $ tokenValue ) : string { $ regex = '/(.*?)(<\/body>)/is' ; $ jQueryCode = sprintf ( '<script>$.ajaxSetup({beforeSend: function (xhr) { xhr.setRequestHeader("X-CSRF-Token","%s"); }});</script>' , $ tokenValue ) ; $ body = preg_replace ( $ regex , '$1' . $ jQueryCode . '$2' , $ body , - 1 , $ count ) ?? '' ; if ( ! $ count ) { // Inject JS code anyway $ body .= $ jQueryCode ; } return $ body ; }
Inject jquery code.

@param string $body body data
@param string $tokenValue token value

@return string html
Inject jquery code .