2017-09-01 86 views
1

我試圖在我的反應本機應用程序中實現google reCAPTCHA。我正在使用一個包裝的webview。React Native與reCATPCHA

<WebView 
    javaScriptEnabled={true} 
    mixedContentMode={'always'} 
    style={{height: 200}} 
    source={{ 
     html: this.getWebviewContent() 
    }}/> 


getWebviewContent(){ 
    var originalForm = '<!DOCTYPE html><html><head><script src="https://www.google.com/recaptcha/api.js"></script></head><body><form action="[POST_URL]" method="post"><input type="hidden" value="[TITLE]"><input type="hidden" value="[DESCRIPTION]"><input type="hidden" value="[URL]"><div class="g-recaptcha" data-sitekey="<My key>"></div><input type="submit" value="Send"/></form></body></html>' 
    var tmp = originalForm 
     .replace("[POST_URL]", "http://localhost:3000/v1/video") 
     .replace("[TITLE]", this.state.form.title) 
     .replace("[DESCRIPTION]", this.state.form.description) 
     .replace("[URL]", this.state.form.url); 

    return tmp; 
} 

如果我呈現它,它給了我下面的錯誤: ERROR for site owner: Invalid domain for site key

我有一個理論,它不希望合作,因爲我跑它作爲一個「文件」 WebView,並且您無法將文件作爲Google信息中心的域的一部分進行添加。

是否有任何方式在Google的reCAPTCHA儀表板中添加file://權限或以任何方式僞造域,以便我可以在儀表板中添加該僞造的域。還是我完全失去了,這個問題是另一回事?

+0

是否行得通? ' mihai1990

+0

是的,這確實有效!非常感謝您的反饋。如果您編寫示例解決方案,我可以將其設置爲接受的答案。 –

回答

0

您可以通過在source道具設置baseUrl設置您的WebView域:

如果你設定的baseUrl域
<WebView 
    javaScriptEnabled={true} 
    mixedContentMode={'always'} 
    style={{height: 200}} 
    source={{ 
     html: this.getWebviewContent(), 
     baseUrl: 'http://your-domain.com' // <-- SET YOUR DOMAIN HERE 
    }}/> 


getWebviewContent(){ 
    var originalForm = '<!DOCTYPE html><html><head><script src="https://www.google.com/recaptcha/api.js"></script></head><body><form action="[POST_URL]" method="post"><input type="hidden" value="[TITLE]"><input type="hidden" value="[DESCRIPTION]"><input type="hidden" value="[URL]"><div class="g-recaptcha" data-sitekey="<My key>"></div><input type="submit" value="Send"/></form></body></html>' 
    var tmp = originalForm 
     .replace("[POST_URL]", "http://localhost:3000/v1/video") 
     .replace("[TITLE]", this.state.form.title) 
     .replace("[DESCRIPTION]", this.state.form.description) 
     .replace("[URL]", this.state.form.url); 

    return tmp; 
}