onMessage android Webview React Native无法在物理设备上运行(ios和虚拟android运行!)

时间:2019-01-25 02:44:13

标签: javascript android mobile webview react-native-android

我的webview包含(嵌入,代码等),所以因为我使用了onMessage和InjectionJavascript,所以我尝试在ios中的onMessage中控制台事件和虚拟android返回结果,只有物理android不返回结果,请让我使用关键字为什么?非常感谢你!

{
   "ResourceARNList": [ "arn:aws:ec2:${awsRegion}:${vpc.getOwnerId}:vpc-peering-connection/${vpcPeeringConnectionId}" ],
   "Tags": { 
      "Name" : "MyVpcPeeringConnectionName" 
   }
}

<WebView
    {...otherProps}
    javaScriptEnable={true}
    domStorageEnabled={true}
    mixedContentMode={'compatibility'}
    source={{ html: templates[this.props.template]({html, fontSizeContent, fontSizeContentPre, highlightThemes, enableNightMode}) }}
    onLoad={this.onLoad.bind(this)}
    injectedJavaScript={this.injectedJavaScript()}
    onMessage={this.onMessage.bind(this)}
    scrollEnabled={false}
    style={{
            height: Math.max(realContentHeight, minHeight), 
            backgroundColor: enableNightMode ? COLORS.COLOR_NIGHT_MODE : COLORS.COLOR_UN_NIGHT_MODE
          }}
    />

hackBefore() {
    return Platform.OS === 'ios' ?
    `
        (function(){
            var originalPostMessage = window.postMessage;
            var patchedPostMessage = function(message, targetOrigin, transfer) {
                originalPostMessage(message, targetOrigin, transfer);
            };
            patchedPostMessage.toString = function() {
                return String(Object.hasOwnProperty).replace('hasOwnProperty', 'postMessage');
            };
            window.postMessage = patchedPostMessage; `
    :
    `
        if (window.postMessage.length !== 1) {
            window.postMessage = function(msg) {
                setTimeout(function () {
                window.postMessage(msg);
                }, 5000);
            }
        }
    `
}

hackAfter() {
    return Platform.OS === 'ios' ? '})();' : ''
}

injectedJavaScript() {
    return ` 
        ${this.hackBefore()}
        NodeList.prototype.forEach = Array.prototype.forEach;

        if (!Element.prototype.matches)
        Element.prototype.matches = Element.prototype.msMatchesSelector || Element.prototype.webkitMatchesSelector;

        if (!Element.prototype.closest)
        Element.prototype.closest = function(s) {
            var el = this;
            if (!document.documentElement.contains(el)) return null;
            do {
                if (el.matches(s)) return el;
                el = el.parentElement || el.parentNode;
            } while (el !== null && el.nodeType === 1); 
            return null;
        };

        function dispatchAction(action, params) {
            window.postMessage(JSON.stringify({
                action,
                params,
            }));
        };


        ${this.hackAfter()}
    `
};

0 个答案:

没有答案