-
Anonymous
Локальное хранилище нарушает код при внедрении JavaScript в Android WebView
Сообщение
Anonymous »
У меня есть этот HTML-код, который я добавляю в webview.loadData():
Код: Выделить всё
@ReactProp(name = "message")
public void setWebviewHtml(LinearLayout view, String html) {
if(! this.cacheData) {
Toast.makeText(reactContext, "sfbsfbsfb: " + this.cacheData + " : " + messageId, Toast.LENGTH_SHORT).show();
String uniqueId = UUID.randomUUID().toString().replace("-", "");
uniqueId = "APPJS" + uniqueId;
// Toast.makeText(reactContext, "uniqueId: " + uniqueId, Toast.LENGTH_SHORT).show();
html = "HTML5 Boilerplate" + html + "";
// oast.makeText(reactContext, "html: " + html, Toast.LENGTH_SHORT).show();
html += "\n";
html += "localStorage.setItem('" + messageId + "', '" + html + "');\n"; // This line breaks the code and does not render anything
html += "$(document).ready(function() {\n";
html += " javascript:" + uniqueId + ".ResizeWebView($(document).height());\n";
html += "});\n";
html += "\n";
layout.setWebviewHtml(html, uniqueId);
}
}
И мой setWebviewHtml():
Код: Выделить всё
public void setWebviewHtml(String html, String uniqueId) {
// Toast.makeText(context, "html2: " + html, Toast.LENGTH_SHORT).show();
webView.loadData(html, "text/html", "UTF-8");
webView.getSettings().setCacheMode(WebSettings.LOAD_CACHE_ELSE_NETWORK);
webView.setLayerType(View.LAYER_TYPE_SOFTWARE, null);
webView.getSettings().setJavaScriptEnabled(true);
webView.addJavascriptInterface(new JSInterface(webView, context, this), uniqueId);
webView.getSettings().setDomStorageEnabled(true);
webView.getSettings().setAllowFileAccess(true);
// webView.getSettings().setAppCacheEnabled(true);
webView.getSettings().setDatabaseEnabled(true);
}
Когда я удалю эту строку:
Код: Выделить всё
html += "localStorage.setItem('" + messageId + "', '" + html + "');\n";
WebView обрабатывается, но с помощью кода LocalStorage приложение прекращает обработку.
Подробнее здесь:
https://stackoverflow.com/questions/790 ... id-webview
1726816903
Anonymous
У меня есть этот HTML-код, который я добавляю в webview.loadData():
[code]@ReactProp(name = "message")
public void setWebviewHtml(LinearLayout view, String html) {
if(! this.cacheData) {
Toast.makeText(reactContext, "sfbsfbsfb: " + this.cacheData + " : " + messageId, Toast.LENGTH_SHORT).show();
String uniqueId = UUID.randomUUID().toString().replace("-", "");
uniqueId = "APPJS" + uniqueId;
// Toast.makeText(reactContext, "uniqueId: " + uniqueId, Toast.LENGTH_SHORT).show();
html = "HTML5 Boilerplate" + html + "";
// oast.makeText(reactContext, "html: " + html, Toast.LENGTH_SHORT).show();
html += "\n";
html += "localStorage.setItem('" + messageId + "', '" + html + "');\n"; // This line breaks the code and does not render anything
html += "$(document).ready(function() {\n";
html += " javascript:" + uniqueId + ".ResizeWebView($(document).height());\n";
html += "});\n";
html += "\n";
layout.setWebviewHtml(html, uniqueId);
}
}
[/code]
И мой setWebviewHtml():
[code]public void setWebviewHtml(String html, String uniqueId) {
// Toast.makeText(context, "html2: " + html, Toast.LENGTH_SHORT).show();
webView.loadData(html, "text/html", "UTF-8");
webView.getSettings().setCacheMode(WebSettings.LOAD_CACHE_ELSE_NETWORK);
webView.setLayerType(View.LAYER_TYPE_SOFTWARE, null);
webView.getSettings().setJavaScriptEnabled(true);
webView.addJavascriptInterface(new JSInterface(webView, context, this), uniqueId);
webView.getSettings().setDomStorageEnabled(true);
webView.getSettings().setAllowFileAccess(true);
// webView.getSettings().setAppCacheEnabled(true);
webView.getSettings().setDatabaseEnabled(true);
}
[/code]
Когда я удалю эту строку:
[code]html += "localStorage.setItem('" + messageId + "', '" + html + "');\n";
[/code]
WebView обрабатывается, но с помощью кода LocalStorage приложение прекращает обработку.
Подробнее здесь: [url]https://stackoverflow.com/questions/79004934/local-storage-breaks-code-when-injecting-the-javascript-to-an-android-webview[/url]