injectJavaScript function

Middleware injectJavaScript (String script)

Creates a Middleware that injects a script into every html response.

This needs some understanding of package: shelf.

Implementation

Middleware injectJavaScript(String script) =>
    createMiddleware(responseHandler: (res) async {
      if (res.headers[_contentType] == _textHtml) {
        final body = await res.readAsString();
        return res.change(body: body.replaceFirst('</body>', '$script</body>'));
      }
      return res;
    });