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

How to inject script file with webviewcontroller.injectScriptFile() ? #154

Closed
blaueeiner opened this issue Sep 10, 2019 · 1 comment
Closed

Comments

@blaueeiner
Copy link

blaueeiner commented Sep 10, 2019

Hi I tried to inject a js file with the webviewcontroller.injectScriptFile(String urlFile) of the flutter_inappbrowser package in Flutter to an open InAppBrowser website. Actually my function in the js file that should be injected should automatically run when it is injected but it does not. There might be nothing wrong with my js file so it has to be the way on how I try to inject the file by the path or something.

Here are the docs for the flutter_inappbrowser package: https://pub.dev/packages/flutter_inappbrowser

I tried the following: 1. I added my whole assets folder to be recognized by flutter in pubspec.yaml 2. My locale path to the js file is assets/js/example.js so I tried to give exactly this path to the parameter as a string. 3. I also tried to change my js file that there is a normal function and export it. And call this function with webviewcontroller.injectScriptCode("functionFromJsFile()") but error is functionFromJsFile() is not defined

EXAMPLE.JS:

(function() {
    alert("Test");
})();

CODE TO INJECT FILE INSIDE CLASS THAT EXTENDS INAPPBROWSER (The own class works as expected. Only the given line in onLoadStop does not.):

class CustomInAppBrowser extends InAppBrowser {
  @override
  Future onLoadStop(String url) async {

    await this.webViewController.injectScriptFile("assets/js/example.js");
  }
}

CODE FOR OPENING THE INAPPBROWSER:

CustomInAppBrowser inAppBrowser = CustomInAppBrowser();
inAppBrowser.open(url: "https://www.example.com", options: {
      "useShouldOverrideUrlLoading": true,
      "useOnLoadResource": true,
      "domStorageEnabled": true,
      "clearCache": true,
    });

I actually expect that when I then call the function to open the InAppBrowser the InAppBrowser opens the given website and bring me the alert. Everything works except the alert that comes from my given js file but js file is not even injected or something is wrong with the parameter urlFile which in my case is "assets/js/example.js"

@pichillilorenzo
Copy link
Owner

With 2.0.0 version of the plugin, you can use:

  • evaluateJavascript({@required String source}): Evaluates JavaScript code into the WebView and returns the result of the evaluation.
  • injectJavascriptFileFromUrl({@required String urlFile}): Injects an external JavaScript file into the WebView from a defined url.
  • injectJavascriptFileFromAsset({@required String assetFilePath}): Injects a JavaScript file into the WebView from the flutter assets directory.

To be able to load a file from the assets folder, see https://github.com/pichillilorenzo/flutter_inappwebview#load-a-file-inside-assets-folder

Also, this plugin changed its name to flutter_inappwebview. The current latest version now is 2.1.0+1. So, you can change your dependency influtter_inappwebview: ^2.1.0+1.

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

2 participants