Android WebView.loadDataWithBaseURL() JavaScript Injection Question
I have searched high and low for a solution to this problem, and while I found one question, I have found zero responses. I'll lay out the entire situation because maybe there's a solution I'm not seeing...
I have an HTML file loaded on to the user's SD card. I'm loading that HTML file into a WebView using WebView.loadURL(...). This works fine. That HTML imports external Javascript files like so:
<script type='text/javascript' src='js/jquery-1.3.2.min.js' />
<script type='text/javascript' src='js/inPractice-utilities.js' />
<script type='text/javascript' src='js/inPractice.js' />
<script type='text/javascript' src='js/mobile/inpractice.ipad.js /'>
Of course, this does not work because after SDK 1.0, the ability load this external code was deemed a security issue (or at least that's how I understood it). I changed each HTML file (there are hundreds) to use inline Javascript, and all works fine.
Each HTML file has links that call Javascript methods using their "onClick" event.
<a href="/drug" onclick="return content('drug', {name:'Tamoxifen', pageId:'9475b30a-5a8b-04fa-f975-7fcaeaa0a2cf'});">Tamoxifen</a>
As you can see, the javascript method, "content", is called when the user clicks on this link. This also works fine.
However, some files have links to "supporting assets", which are essentially separate HTML pages themselves, except without the actual page. Here's an example:
<a href="/supportingAsset" onclick="return content('supportingAsset', {'html':'<div class='supportingAsset'><p><a href='/drug' onclick='return content('drug', {name:'Tamoxifen', pageId:'9475b30a-5a8b-04fa-f975-7fcaeaa0a2cf'});'>Tamoxifen</a>'});">Supporting Asset</a>
*Note, there may be some syntax issues in my recreation, but I promise the syntax is all correct in the actual code.
So, the issue here is that the supporting asset HTML is included as a parameter to the call to Javascript method "content". In order to display this content when the user clicks on the supporting asset, I use...
webView.loadDataWithBaseURL("file://" + baseLocation, html, "text/html", "utf-8", null);
...where "html" is the HTML passed into the Javascript method, "content". This new page is displaying fine, BUT...if you notice in the supporting asset content, there is ANOTHER link that calls out to the Javascript method, "content". This is where the problem lies...
I've tried appending...
<script type='text/javascript' src='js/jquery-1.3.2.min.js' />
...to the raw HTML before loading it into the webview. Nothing.
I've tried appending the inline Javascript method. Nothing.
I've tried adding the Javascript method after pageLoad...
@Override
public void onPageFinished(WebView view, String url) {
if (loadingPageDialog != null) {
lo开发者_如何学运维adingPageDialog.dismiss();
}
view.loadUrl("javascript:(function content(path, params) { " +
"document.getElementsByTagName('body')[0].style.color = 'red'; " +
"})()");
}
...Nothing.
Actual Question
Does anyone know of a way I can inject the Javascript method "content", such that the existing call to "content" works? Note, I can't change the call to "content", and the webview that displays the supporting asset content is in a separate activity from the original webview.
I believe you have a typo. Change,
"file://"
to
"file:///"
精彩评论