get content of webview in nativescript

前端 未结 2 398
刺人心
刺人心 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: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:

    
    
    
    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:

    
    
    
         
                
    
    

    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/

提交回复
热议问题