Failing to override javascript function inside Webview

Viewed 176

I'm working on an React-Native app that needs to get a response from a webview.

the problem is that I have this function on the game and I need to override it so my app get the link and open it in a different webview, instead of the one running at the moment.

function getPrepayArg(serverid, actid, payIndex) {
console.log(payIndex);
if(payIndex == 88 || payIndex == 30){
    window.open("https://somelink/user/exchange");
} else(window.open("https://somelink/user/exchange"));
}

When the player clicks a button inside the game, the function is called.

I've tried to override it using the "injectedJavaScript" method from the WebView.

My code is looking like this:

<WebView ref={WEBVIEW_REF}
    injectedJavaScriptBeforeContentLoaded={false}
    injectedJavaScript={jsCode}
    onLoadEnd={OverrideFunction}
    onMessage={(event) => {
      const { data } = event.nativeEvent;
      treatEvent(data)
    }}
    onNavigationStateChange={(navState) => { console.log(jsCode); setCanGoBack(navState.canGoBack) }}
    source={{uri: 'https://swdanceh5.com/'}}
    style={{flex: 1}}
 />

and the jsCode variable:

const jsCode = `


  document.querySelectorAll('.icons-ft-naptien')[0].addEventListener('click', stopDefAction, false);
  document.querySelectorAll('.icons-ft-naptien')[0].addEventListener('click', sendLinkDiamante, false);
  function sendLinkDiamante(){
    ReactNativeWebView.postMessage(String(document.querySelectorAll('.icons-ft-naptien')[0].getAttribute('href')));
  }

  document.querySelectorAll('.icons-ft-fanpage')[0].addEventListener('click', stopDefAction, false);
  document.querySelectorAll('.icons-ft-fanpage')[0].addEventListener('click', sendLinkFacebook, false);
  function sendLinkFacebook(){
    ReactNativeWebView.postMessage(String(document.querySelectorAll('.icons-ft-fanpage')[0].getAttribute('href')));
  }


  //Stopping Default Button click action

  function stopDefAction(evt) {
    evt.preventDefault();
  }

  function getPrepayArg(serverid, actid, payIndex){
    console.log(payIndex);
    if(payIndex == 88 || payIndex == 30){
      ReactNativeWebView.postMessage('https://swdanceh5.com/user/exchange');
    } else(ReactNativeWebView.postMessage('https://swdanceh5.com/user/exchange'))
  }

  
`;
0 Answers
Related