Android WebView.loadDataWithBaseURL() JavaScript 注入问题

发布于 2024-11-06 08:55:07 字数 2415 浏览 1 评论 0原文

我到处寻找这个问题的解决方案,虽然我找到了一个问题,但我发现了零个答案。我将列出整个情况,因为也许有一个我没有看到的解决方案...

我有一个 HTML 文件加载到用户的 SD 卡上。我使用 WebView.loadURL(...) 将该 HTML 文件加载到 WebView 中。这很好用。 HTML 像这样导入外部 Javascript 文件:

<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 /'>

当然,这不起作用,因为在 SDK 1.0 之后,加载此外部代码的能力被认为是一个安全问题(或者至少我是这么理解的)。我更改了每个 HTML 文件(有数百个)以使用内联 Javascript,并且一切正常。

每个 HTML 文件都有使用“onClick”事件调用 Javascript 方法的链接。

<a href="/drug" onclick="return content('drug', {name:'Tamoxifen', pageId:'9475b30a-5a8b-04fa-f975-7fcaeaa0a2cf'});">Tamoxifen</a>

正如您所看到的,当用户单击此链接时,将调用 javascript 方法“content”。这也很好用。

但是,某些文件具有指向“支持资产”的链接,这些文件本身本质上是单独的 HTML 页面,但没有实际页面。这是一个例子:

<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>

*注意,我的游戏中可能存在一些语法问题,但我保证实际代码中的语法都是正确的。

因此,这里的问题是支持资源 HTML 作为参数包含在对 Javascript 方法“content”的调用中。为了在用户单击支持资产时显示此内容,我使用...

webView.loadDataWithBaseURL("file://" + baseLocation, html, "text/html", "utf-8", null);

...其中“html”是传递到 Javascript 方法“content”中的 HTML。这个新页面显示良好,但是...如果您在支持资产内容中注意到,还有另一个调用 Javascript 方法“内容”的链接。这就是问题所在...

我尝试将...附加

<script type='text/javascript' src='js/jquery-1.3.2.min.js' />

到原始HTML,然后再将其加载到网络视图中。没有什么。

我尝试添加内联 Javascript 方法。没有什么。

我尝试在 pageLoad 之后添加 Javascript 方法...

    @Override
    public void onPageFinished(WebView view, String url) {
        if (loadingPageDialog != null) {
            loadingPageDialog.dismiss();
        }

        view.loadUrl("javascript:(function content(path, params) { " +  
                "document.getElementsByTagName('body')[0].style.color = 'red'; " +  
        "})()");  
    }

...什么也没有。

实际问题

有谁知道我可以注入Javascript方法“内容”的方法,以便现有的对“内容”的调用起作用?请注意,我无法更改对“内容”的调用,并且显示支持资产内容的 Web 视图位于与原始 Web 视图不同的活动中。

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) {
            loadingPageDialog.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.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

可是我不能没有你 2024-11-13 08:55:07

我相信你有一个错字。改变,

"file://"

"file:///"

I believe you have a typo. Change,

"file://"

to

"file:///"
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文