在nativescript中获取webview的内容

在nativescript中获取webview的内容,webview,nativescript,Webview,Nativescript,是否有一种方法可以在页面加载后读取webview的内容 原因是重定向(如window.location.replace或window.location.href)在我的情况下在IOS中不起作用,在Android中可以正常工作 我可以访问url,错误。但是如何访问内容呢 纳拉扬我只是在找IOS。我找到了答案并在这里分享。对于Android,我想指出一些线索 if (webView.ios) { var webHeader = webView.ios.stringByEvaluatingJ

是否有一种方法可以在页面加载后读取webview的内容

原因是重定向(如window.location.replace或window.location.href)在我的情况下在IOS中不起作用,在Android中可以正常工作

我可以访问url,错误。但是如何访问内容呢


纳拉扬

我只是在找IOS。我找到了答案并在这里分享。对于Android,我想指出一些线索

if (webView.ios) {
    var webHeader = webView.ios.stringByEvaluatingJavaScriptFromString("document.head.innerHTML").trim();
    console.log(webHeader);

    var webBody = webView.ios.stringByEvaluatingJavaScriptFromString("document.body.innerHTML").trim();
    console.log(webBody);

} else if (webView.android) {
    webTitle = webView.android.getTitle(); //getting the title title
    console.log(webTitle)
}

我只是在找IOS。我找到了答案并在这里分享。对于Android,我想指出一些线索

if (webView.ios) {
    var webHeader = webView.ios.stringByEvaluatingJavaScriptFromString("document.head.innerHTML").trim();
    console.log(webHeader);

    var webBody = webView.ios.stringByEvaluatingJavaScriptFromString("document.body.innerHTML").trim();
    console.log(webBody);

} else if (webView.android) {
    webTitle = webView.android.getTitle(); //getting the title title
    console.log(webTitle)
}

你可以看看这篇文章。安装一个库,该库允许通过可观察对象与webview进行通信。现在我自己也在使用它,它对iOS和Android都很好

1-安装: tns插件添加nativescript webview界面 2-在web项目中复制插件文件 cp节点_模块/nativescript webview接口/www/nativescript-webview-interface.js应用程序/www/lib/ 3-代码: xml:

更多信息:


    • 你可以看看这篇文章。安装一个库,该库允许通过可观察对象与webview进行通信。现在我自己也在使用它,它对iOS和Android都很好

      1-安装: tns插件添加nativescript webview界面 2-在web项目中复制插件文件 cp节点_模块/nativescript webview接口/www/nativescript-webview-interface.js应用程序/www/lib/ 3-代码: xml:

      更多信息:


      android getTitle有点粗糙,但它救了我的命!如何获取当前url?我试着使用console.log(webView.src),它总是在导航到其他链接后打印初始的src。android getTitle有点粗糙,但它救了我一命!如何获取当前url?我尝试使用console.log(webView.src),它总是打印初始src,即使在导航到其他链接之后也是如此。
      <html>
      <head></head>
      <body>
          <script src="path/to/nativescript-webview-interface.js"></script> 
          <script src="path/to/your-custom-script.js"></script>        
      </body>
      
          var oWebViewInterface = window.nsWebViewInterface;
      
      // register listener for any event from native app
      oWebViewInterface.on('anyEvent', function(eventData){
      
      });
      
      // emit event to native app
      oWebViewInterface.emit('anyEvent', eventData);
      
      // function which can be called by native app
      window.functionCalledByNative = function(arg1, arg2){
          // do any processing
          return dataOrPromise;
      }