get content of webview in nativescript

前端 未结 2 369
刺人心
刺人心 2021-01-28 11:00

Is there a way I can read the content of webview after the page is loaded ?

The reason is redirect (like window.location.replace or window.location.href ) is not workin

相关标签:
2条回答
  • 2021-01-28 11:36

    I was only looking for IOS. I found the answer and sharing it here. For Android I would like to point some leads.

    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)
    }
    

    Some stack overflow lead for Android

    0 讨论(0)
  • 2021-01-28 11:56

    You could look at this post. installs a library that allows communication with the webview through observables. Right now I'm using it myself and it's great for both iOS and Android

    1- install: tns plugin add nativescript-webview-interface 2- in web project copy plugin file cp node_modules/nativescript-webview-interface/www/nativescript-webview-interface.js app/www/lib/ 3- code: xml:

    <Page xmlns="http://schemas.nativescript.org/tns.xsd" 
    loaded="pageLoaded">
    <web-view id="webView"></web-view>
    </Page>
    var webViewInterfaceModule = require('nativescript-webview- 
    interface');
    var oWebViewInterface;
    
    function pageLoaded(args){
    page = args.object;
    setupWebViewInterface(page) 
    }
    
    function setupWebViewInterface(page){
    var webView = page.getViewById('webView');
    oWebViewInterface = new 
    webViewInterfaceModule.WebViewInterface(webView, '~/www/index.html');
    }
    
    function handleEventFromWebView(){
    oWebViewInterface.on('anyEvent', function(eventData){
        // perform action on event
    });
    }
    
    function emitEventToWebView(){
        oWebViewInterface.emit('anyEvent', eventData);
    }
    
    function callJSFunction(){
        oWebViewInterface.callJSFunction('functionName', args, function(result){
    
        });
    }
    

    web-view:

    <html>
    <head></head>
    <body>
        <script src="path/to/nativescript-webview-interface.js"></script> 
        <script src="path/to/your-custom-script.js"></script>        
    </body>
    

    web-view js:

        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;
    }
    

    More Info:

    • https://www.npmjs.com/package/nativescript-webview-interface

    • http://shripalsoni.com/blog/nativescript-webview-native-bi-directional-communication/

    0 讨论(0)
提交回复
热议问题