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

[Android] injectScript not work! #69

Closed
peng051410 opened this issue Mar 3, 2016 · 15 comments
Closed

[Android] injectScript not work! #69

peng051410 opened this issue Mar 3, 2016 · 15 comments

Comments

@peng051410
Copy link

I write bellow code:

function webViewBridgeReady(cb) {
if (window.WebViewBridge) {
cb(window.WebViewBridge);
return;
}

function handler() {
  document.removeEventListener('WebViewBridge', handler, false);
  cb(window.WebViewBridge);
}
document.addEventListener('WebViewBridge', handler, false);

}

function goDetail(id) {
WebViewBridge.send("jump:" + id);
}

webViewBridgeReady(function (webViewBridge) {
webViewBridge.send("message from webview");
});

the html is get from server, html have a button and click to invoke the goDetail function,but in android the
log cat say 'goDetail is not defined'.
can you help me?

@YangYouYong
Copy link

@peng051410 you should change your html code

@quentinzhang
Copy link

me either, the same code as iOS, but the injected code seems not work.

@YangYouYong
Copy link

@quentinzhang so can you load your page in Android?

@YangYouYong
Copy link

@peng051410 I solve this in iOS like #59

@peng051410
Copy link
Author

@YangYouYong 已加你qq!

@quentinzhang
Copy link

能加载出网页,只是注入的代码没有生效。已经在QQ上添加你了。

On Fri, Mar 18, 2016 at 4:54 PM, YangYouYong notifications@github.com
wrote:

那你能在安卓上加载出来网页吗?

在 2016年3月18日,下午4:52,quentinzhang notifications@github.com 写道:

me either, the same code as iOS, but the injected code seems not work.


You are receiving this because you commented.
Reply to this email directly or view it on GitHub <
#69 (comment)


You are receiving this because you commented.
Reply to this email directly or view it on GitHub
#69 (comment)

@stereodenis
Copy link
Contributor

got the same issue

@g6ling
Copy link
Contributor

g6ling commented Apr 4, 2016

I think what write handler() is old. change injectedScript
(function () {
if (WebViewBridge) {

                  WebViewBridge.onMessage = function (message) {
                    if (message === "hello from react-native") {
                      WebViewBridge.send("got the message inside webview");
                    }
                  };

                  WebViewBridge.send("hello from webview");
                }
              }());

If you read example and tried it, you will solve problem

@MacKentoch
Copy link

I experienced something interesting.

My script works on iOS but applying it to android did not work.

  • first as @g6ling suggested watch example
  • secondthat is what is interestingerase all javascript comments in your scripts!!!
    • it broke my script on android!!! It was why it did not work...

Illustration:

const scriptNotWorking = `
 // a comment <= erase all comments like this one!!!
 alert('it won\'t work');
`;
const scriptWorking = `
 alert('it will work');
`;

@eremzeit
Copy link

eremzeit commented May 24, 2016

I also am unable to inject JS into a WebViewBridge on android. If I change the type to plain old WebView with all the same props then it works so I suspect it must be something with this library.

Further, I'm seeing the same weird issue with comments as @MacKentoch but I'm seeing it with the plain old WebView. Any comments at all (even at the end of the line or at the end of all the JS) cause the javascript to not run.

@HADB
Copy link

HADB commented May 26, 2016

I got the same issue. The code works on iOS buy didn't work on Android even the simplest code as MacKentoch's.

@weremax
Copy link

weremax commented Sep 28, 2016

Any updates or eta on a resolution to this related to android injected javascript not working, please?

@alinz
Copy link
Owner

alinz commented Sep 28, 2016

@weremax, recently @hmedney made a contribution to the code that solves android issues. Have you tried pulling it from master branch?

@weremax
Copy link

weremax commented Sep 28, 2016

I managed to get it working, thanks @alinz

@jenskuhrjorgensen
Copy link

GEEEEEEEZUZ christ
Thank @MacKentoch for pointing me in the direction of retarded errors. I also had problems executing injected scripts on Android whereas it worked flawless on iOS. After reading your solution I removed all comments, but it still didn't work. However, I then added SEMICOLON's at each and every line ending and now it works. Once again: Thanks! :D

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