Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Subsequent script injections do not stack #112

Closed
peter-meemo opened this issue Jun 7, 2019 · 3 comments
Closed

Subsequent script injections do not stack #112

peter-meemo opened this issue Jun 7, 2019 · 3 comments

Comments

@peter-meemo
Copy link

Documentation seems to imply that injected javascript code can see/use previously injected code, but this does not seem to be the case. From the example:

await this.webViewController.injectScriptFile("https://code.jquery.com/jquery-3.3.1.min.js");
    this.webViewController.injectScriptCode("""
      \$( "body" ).html( "Next Step..." )
    """);

If I use this code and await the second injection, it gives ReferenceError: $ is not defined.

A similar test setup:

class miab extends InAppBrowser {
  @override
  Future<void> onLoadStop(String url) async {
    await this.webViewController.injectScriptCode(_withErrorHandler("function foo() {return 2;}"));
    final result = await this.webViewController.injectScriptCode(_withErrorHandler("foo();"));
  }

  @override
  void onBrowserCreated() {
    webViewController.addJavaScriptHandler('msg', ((List<dynamic> args) {
      print('MIAB got msg: $args');
    }));
  }

  static String _withErrorHandler(String js) =>
      "try {$js} catch(e) {window.flutter_inappbrowser.callHandler('msg', 'err', e.toString());}";
}

shows

MIAB got msg: [err, ReferenceError: foo is not defined]

I also see this error, which seems related to https://github.com/pichillilorenzo/flutter_inappbrowser/issues/104

E/MethodChannel#com.pichillilorenzo/flutter_inappbrowser(23351): Failed to handle method call result
E/MethodChannel#com.pichillilorenzo/flutter_inappbrowser(23351): java.lang.NullPointerException: Attempt to read from field 'com.pichillilorenzo.flutter_inappbrowser.InAppWebView.InAppWebView com.pichillilorenzo.flutter_inappbrowser.FlutterWebView.webView' on a null object reference
E/MethodChannel#com.pichillilorenzo/flutter_inappbrowser(23351): 	at com.pichillilorenzo.flutter_inappbrowser.JavaScriptBridgeInterface$1$1.success(JavaScriptBridgeInterface.java:55)
E/MethodChannel#com.pichillilorenzo/flutter_inappbrowser(23351): 	at io.flutter.plugin.common.MethodChannel$IncomingResultHandler.reply(MethodChannel.java:179)
E/MethodChannel#com.pichillilorenzo/flutter_inappbrowser(23351): 	at io.flutter.embedding.engine.dart.DartMessenger.handlePlatformMessageResponse(DartMessenger.java:103)
E/MethodChannel#com.pichillilorenzo/flutter_inappbrowser(23351): 	at io.flutter.embedding.engine.FlutterJNI.handlePlatformMessageResponse(FlutterJNI.java:228)
E/MethodChannel#com.pichillilorenzo/flutter_inappbrowser(23351): 	at android.os.MessageQueue.nativePollOnce(Native Method)
E/MethodChannel#com.pichillilorenzo/flutter_inappbrowser(23351): 	at android.os.MessageQueue.next(MessageQueue.java:326)
E/MethodChannel#com.pichillilorenzo/flutter_inappbrowser(23351): 	at android.os.Looper.loop(Looper.java:160)
E/MethodChannel#com.pichillilorenzo/flutter_inappbrowser(23351): 	at android.app.ActivityThread.main(ActivityThread.java:6669)
E/MethodChannel#com.pichillilorenzo/flutter_inappbrowser(23351): 	at java.lang.reflect.Method.invoke(Native Method)
E/MethodChannel#com.pichillilorenzo/flutter_inappbrowser(23351): 	at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:493)
E/MethodChannel#com.pichillilorenzo/flutter_inappbrowser(23351): 	at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:858)
@peter-meemo
Copy link
Author

It seems like calls to injectScriptCode are wrapped such that it's not surprising that they don't interact in this way (thought it would be good to document this). injectScriptFile seems like it modifies the document, so I would expect it to work.

@peter-meemo
Copy link
Author

It appears that this does work in ios, so this is probably a bug on the android side?

@peter-meemo
Copy link
Author

Looks like this is due to a combination of problems in my test setup and various pages blocking scripts loaded via injectScriptFile.

This was referenced Jul 6, 2020
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant