javascript - React Native WebView JavaScript 构建问题

标签 javascript webview react-native

我有一个适用于 Android 的 React Native 应用程序,它支持实时聊天(使用 Intercom)。为了访问 Intercom,我使用 WebViewinjectedJavaScript 来显示 UI。它在我的开发版本上运行良好,但是当我进行发布版本时,它提示找不到 window.Intercom (如果删除 window.,我也会遇到相同的相关问题。 )

这是我尝试运行的代码

IntercomContainer.js

// =============================================================================
// Components >> IntercomContainer
// =============================================================================
// @flow

// Import
// =============================================================================

import * as React from 'react';
import { View, WebView } from 'react-native';
import Spinner from 'react-native-loading-spinner-overlay';

import styles from './styles';

// Content
// =============================================================================

type State = {
    isLoading: boolean,
};

type Props = {
    appId: string,
}

// Render
// =============================================================================

export default class IntercomContainer extends React.Component<Props, State> {

    props: Props = {
        appId: '',
    };

    state: State = {
        isLoading: true,
    }

    setState: Function;

    injectedJS = (appId: string) => {
        return `
            try {

                window.Intercom('boot', {
                    app_id: '${appId}',
                }); 

                window.Intercom('show');
                window.Intercom('onShow', function() { 
                    document.getElementById('message').innerHTML = '';
                    setTimeout(() => {
                        document.getElementById('message').innerHTML = 'Click on the chat button in the bottom-right to open chat...';
                    }, 1000)
                });

            } catch(e) {
                alert('Intercom failed to load: ' + e.message);
            }
        `;
    }

    onLoadEnd = () => {
        this.setState({
            isLoading: false,
        });
    }

    render(){
        const { appId } = this.props;
        const { isLoading } = this.state;

        return (
            <View style={styles.container}>
                <Spinner visible={isLoading} />
                <WebView
                    injectedJavaScript={this.injectedJS(appId)}
                    source={require('./IntercomWebView.html')}
                    onLoadEnd={this.onLoadEnd}
                    javaScriptEnabled={true}
                    style={styles.webView}
                />
            </View>
        );
    }
}

IntercomWebView.html

<!DOCTYPE html>
<head>
    <script>
        // intercom JS library
        var APP_ID = '';
        (function(){
            debugger;
            console.log("Executing function main...");
            var w=window;
            var ic=w.Intercom;
            if (typeof ic === "function") {
                ic('reattach_activator');
                ic('update',intercomSettings);
            } else {
                var d=document;
                var i= function() {
                    i.c(arguments)
                };
                i.q=[];
                i.c=function(args){
                    i.q.push(args)
                };
                w.Intercom=i;

                function l(){
                    debugger;
                    console.log("Executing function l...");
                    var s=d.createElement('script');
                    s.type='text/javascript';
                    s.async=true;
                    s.src='https://widget.intercom.io/widget/' + APP_ID;
                    var x=d.getElementsByTagName('script')[0];
                    x.parentNode.insertBefore(s,x);
                }

                if(w.attachEvent){
                    w.attachEvent('onload',l);
                }else{
                    w.addEventListener('load',l,false);
                }
            }
        })();
    </script>
    <style>
        main {
            align-items: center;
            background-color: #fefefe;
            color: #999;
            display: flex;
            font-family: sans-serif;
            height: 80vh;
            justify-content: center;
            text-align: center;
        }
    </style>
</head>
<body>
    <main id="message">
        loading...
    </main>
</body>
</html>

谢谢!

最佳答案

您的问题的根本原因很可能与postMessage相同。漏洞。使用 Intercom 对象的代码将在初始化该对象的 JavaScript 代码之前加载。作为解决方法,您可以使用一些神奇值在 setTimeout 中调用此代码,或实现 more "neat" solution您将推迟对 Intercom 对象的实际调用,直到它初始化

关于javascript - React Native WebView JavaScript 构建问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46648549/

相关文章:

android - DownloadListener.onDownloadStart() 从未调用过

android - 堆叠在另一个 TouchableOpacity 内的 TouchableOpacity 不可点击

react-native - React Native Sentry中配置的build_type在哪里

javascript - 两个 html Canvas 彼此重叠,一个游戏层和一个 GUI 层。好坏?

javascript - 多次调用编码的 COM 接口(interface)

javascript - HTML:td-iframe 位于表格内。它可以在空白处显示在外面吗?

javascript - ajax调用成功后重新初始化Slick js

java - 通过 WebView (Android Studio) 导航到应用程序时,Youtube 会在应用程序中启动

Android WebView 在应用加载时重新加载

android - 使用react-native检查手机上是否安装了Metamask应用程序