loadJsFromAssetsWeb function

Future<void> loadJsFromAssetsWeb()

Loads the JavaScript AES-GCM implementation from the plugin's assets into the DOM.

This function injects a <script> element containing the contents of assets/aes_encryption_decryption.js into the <head> of the web document. It checks if the script is already loaded using the ID 'aes-gcm-js' to avoid re-injection.

This is required before calling encryptAesGcmWeb or decryptAesGcmWeb.

Implementation

Future<void> loadJsFromAssetsWeb() async {
  // Check if script is already added
  if (document.getElementById('aes-gcm-js') != null) return;

  String pluginPath = "packages/aes_gcm_crypto/";

  String jsCode = await rootBundle.loadString("${pluginPath}assets/aes_encryption_decryption.js");

  ScriptElement script = ScriptElement()
    ..id = "aes-gcm-js"
    ..type = "text/javascript"
    ..text = jsCode;

  document.head!.append(script);
}