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

Listen to requests #206

Closed
dudizimber opened this issue Dec 3, 2019 · 3 comments
Closed

Listen to requests #206

dudizimber opened this issue Dec 3, 2019 · 3 comments

Comments

@dudizimber
Copy link

Description

Hi,
I'm developing an app where the user is required to do few things in different websites, and I have to detect whether he completed that task or not.
In a few websites I can simply inject some javascript and send a message back to the app, but there are some websites that I cannot to that since they block inline javascript, or that's what I understood (Ex: twitter.com).
So looking for other option, I stumbled across the "chrome.webRequest" API which is used to listen to requests made from a page. However it looks like this API is mainly for extensions.

I wonder if it is possible to do something like that with InAppWebView package.

Thank you.

@pichillilorenzo
Copy link
Owner

pichillilorenzo commented Dec 3, 2019

There are also other ways to execute some javascript that is not restricted to injecting a javascript file! You can also use:

  • controller.evaluateJavascript(source: "");
  • controller.loadUrl(url: "javascript:(function(){ ... here some javascript ... })();");

For example, I loaded "https://twitter.com/home" and it works:

...

  @override
  Widget build(BuildContext context) {
    return Scaffold(
        appBar: AppBar(
            title: Text("InAppWebView")
        ),
        body: Container(
            child: Column(children: <Widget>[
              Expanded(
                child: Container(
                  child: InAppWebView(
                    initialUrl: "https://twitter.com/home",
                    initialHeaders: {},
                    initialOptions: InAppWebViewWidgetOptions(
                      inAppWebViewOptions: InAppWebViewOptions(
                        debuggingEnabled: true,
                      )
                    ),
                    onWebViewCreated: (InAppWebViewController controller) {
                      webView = controller;
                    },
                    onLoadStart: (InAppWebViewController controller, String url) {

                    },
                    onLoadStop: (InAppWebViewController controller, String url) {
                      controller.evaluateJavascript(source: "alert('HI');");
                      controller.loadUrl(url: "javascript:(function(){ alert('HI'); })();");
                    },
                  ),
                ),
              ),
            ]))
    );
  }

...

However, chrome APIs, such as chrome.webRequest, are only for Chrome Extension! These APIs are not Android WebView APIs (that are here).

@dudizimber
Copy link
Author

There are also other ways to execute some javascript that is not restricted to injecting a javascript file! You can also use:

* `controller.evaluateJavascript(source: "");`

* `controller.loadUrl(url: "javascript:(function(){ ... here some javascript ... })();");`

For example, I loaded "https://twitter.com/home" and it works:

...

  @override
  Widget build(BuildContext context) {
    return Scaffold(
        appBar: AppBar(
            title: Text("InAppWebView")
        ),
        body: Container(
            child: Column(children: <Widget>[
              Expanded(
                child: Container(
                  child: InAppWebView(
                    initialUrl: "https://twitter.com/home",
                    initialHeaders: {},
                    initialOptions: InAppWebViewWidgetOptions(
                      inAppWebViewOptions: InAppWebViewOptions(
                        debuggingEnabled: true,
                      )
                    ),
                    onWebViewCreated: (InAppWebViewController controller) {
                      webView = controller;
                    },
                    onLoadStart: (InAppWebViewController controller, String url) {

                    },
                    onLoadStop: (InAppWebViewController controller, String url) {
                      controller.evaluateJavascript(source: "alert('HI');");
                      controller.loadUrl(url: "javascript:(function(){ alert('HI'); })();");
                    },
                  ),
                ),
              ),
            ]))
    );
  }

...

However, chrome APIs, such as chrome.webRequest, are only for Chrome Extension! These APIs are not Android WebView APIs (that are here).

I'll definitely try that!
Thank you very much!

This was referenced Jul 6, 2020
Copy link

This thread has been automatically locked since there has not been any recent activity after it was closed. If you are still experiencing a similar issue, please open a new bug and a minimal reproduction of the issue.

@github-actions github-actions bot locked as resolved and limited conversation to collaborators Nov 12, 2024
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants