在 Android 中将 Playbuzz 嵌入脚本加载到 WebView
Load Playbuzz embeded script into a WebView in Android
我正在尝试在 WebView
中加载 Playbuzz
内容的脚本内容
我找到了一种方法来加载包含脚本的 url 网页:
String urlPlayBuzz = "http://www.jorgesys.com/playbuzzcontent.html";
webview.loadUrl(urlPlayBuzz);
但要求是仅加载脚本并显示 playbuzz
内容:
String script = "<center style=\"width:100%;\"><script> (function(d,s,id){var js,fjs=d.getElementsByTagName(s)[0];if(d.getElementById(id))return;js=d.createElement(s);js.id=id;js.src='//embed.playbuzz.com/sdk.js' ;fjs.parentNode.insertBefore(js,fjs);}(document, 'script' , 'playbuzz-sdk' ));</script><div class=\"playbuzz\" data-id=\"9ed89fec-22cb-441e-b2a3-69b3bd1e6953\" data-show-info=\"false\" data-show-share=\"false\" style=\"width:100%;height:56.25vw\"></div></center>";
我试过这两个选项:
webview.loadUrl(script);
和
webview.loadData(script,"text/html","UTF-8");
没有成功,结果我只有一个空 WebView
:
如何加载 playbuzz 内容的脚本并将其显示到 WebView
?
好吧,我发现我的脚本包含如下相对路径:
js.src='//embed.playbuzz.com/sdk.js'
与仅包含绝对路径的 twitter 或 instagram 的嵌入式脚本不同。
所以我使用了这个方法:
loadDataWithBaseURL() Loads the given data into this WebView, using baseUrl as the base URL for the content. The base URL is used both to resolve relative URLs and when applying JavaScript's same origin policy. The historyUrl is used for the history entry..
您可以为内容定义基础URL:
webview.loadDataWithBaseURL("https://embed.playbuzz.com.com", url, "text/html", "UTF-8", null);
现在我可以加载包含具有相对路径的脚本的内容了!!!
我正在尝试在 WebView
Playbuzz
内容的脚本内容
我找到了一种方法来加载包含脚本的 url 网页:
String urlPlayBuzz = "http://www.jorgesys.com/playbuzzcontent.html";
webview.loadUrl(urlPlayBuzz);
但要求是仅加载脚本并显示 playbuzz
内容:
String script = "<center style=\"width:100%;\"><script> (function(d,s,id){var js,fjs=d.getElementsByTagName(s)[0];if(d.getElementById(id))return;js=d.createElement(s);js.id=id;js.src='//embed.playbuzz.com/sdk.js' ;fjs.parentNode.insertBefore(js,fjs);}(document, 'script' , 'playbuzz-sdk' ));</script><div class=\"playbuzz\" data-id=\"9ed89fec-22cb-441e-b2a3-69b3bd1e6953\" data-show-info=\"false\" data-show-share=\"false\" style=\"width:100%;height:56.25vw\"></div></center>";
我试过这两个选项:
webview.loadUrl(script);
和
webview.loadData(script,"text/html","UTF-8");
没有成功,结果我只有一个空 WebView
:
如何加载 playbuzz 内容的脚本并将其显示到 WebView
?
好吧,我发现我的脚本包含如下相对路径:
js.src='//embed.playbuzz.com/sdk.js'
与仅包含绝对路径的 twitter 或 instagram 的嵌入式脚本不同。
所以我使用了这个方法:
loadDataWithBaseURL() Loads the given data into this WebView, using baseUrl as the base URL for the content. The base URL is used both to resolve relative URLs and when applying JavaScript's same origin policy. The historyUrl is used for the history entry..
您可以为内容定义基础URL:
webview.loadDataWithBaseURL("https://embed.playbuzz.com.com", url, "text/html", "UTF-8", null);
现在我可以加载包含具有相对路径的脚本的内容了!!!