injectJavaScript function
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: (response) async {
if (response.headers[_contentType] == _textHtml) {
return response.change(
body: (await response.readAsString())
.replaceFirst('</body>', '<script>$script</script></body>'));
}
return response;
});