inappwebview_script_guard 0.1.1
inappwebview_script_guard: ^0.1.1 copied to clipboard
Detect silently-failed JavaScript injection in flutter_inappwebview: canary-verified script loading plus commit-time AST linting of embedded JS.
inappwebview_script_guard example #
import 'dart:convert';
import 'package:flutter_inappwebview/flutter_inappwebview.dart';
import 'package:inappwebview_script_guard/inappwebview_script_guard.dart';
/// A literal script: raw triple-quoted string, no interpolation.
final probeScript = JsScript(
name: 'probe',
source: r'''
JSON.stringify((function () {
return {title: document.title, blocks: document.querySelectorAll('p').length};
})());
''',
);
Future<void> runProbe(InAppWebViewController controller) async {
final (canary, bridgeRaw) =
await injectAndVerifyCanary(controller, probeScript);
switch (canary) {
case LoadedResult():
final payload = jsonDecode(bridgeRaw as String) as Map<String, dynamic>;
print('probe ok: $payload');
case MissingResult():
print('script-side failure (parse error / mid-run throw): $canary');
case InjectThrewResult(:final exception):
print('bridge-side failure: $exception');
case StaleResult():
print('cached older script running — reload the page: $canary');
case MalformedResult():
print('flag clobbered by other JS — investigate: $canary');
}
}
The commit-time tooling is wired separately — see the README's
"Git-hook wiring" section and the scripts in example/hooks/.