addJavaScriptCode function

Future<bool> addJavaScriptCode(
  1. String scriptCode
)

Adds a JavaScript code (scriptCode) into DOM.

Implementation

Future<bool> addJavaScriptCode(String scriptCode) async {
  var prevCall = _addedJavaScriptCodes[scriptCode];
  if (prevCall != null) return prevCall;

  Future<bool> future;

  try {
    var head = querySelector('head')!;

    var script = ScriptElement()
      ..type = 'text/javascript'
      ..text = scriptCode;

    head.children.add(script);

    future = Future.value(true);
  } catch (e, s) {
    print(e);
    print(s);
    future = Future.value(false);
  }

  _addedJavaScriptCodes[scriptCode] = future;

  return future;
}