gpt4 book ai didi

javascript - UIwebview 中的 CSS 注入(inject)

转载 作者:太空狗 更新时间:2023-10-29 12:55:51 28 4
gpt4 key购买 nike

我正在尝试使用此示例发布 https://gist.github.com/811993 ;我正在尝试为我 parent 的网站 www.baomoi.com 使用自定义 css 文件,以使其更加简约和高对比度。此代码示例在加载时导致强制关闭。我在/Resources 文件夹中有 styles.css。如何确定导致错误的位置?

预先感谢大家的帮助。

https://gist.github.com/811993

在调试中,这是我得到的:E/KrollCallback( 382): (kroll$1: app://app.js) [234,4575] 错误,调用:[callMethod UI.WebView.UI.WebView:event:load null],消息:大小必须> = 0E/KrollCallback(382):java.lang.IllegalArgumentException:大小必须 >= 0E/AndroidRuntime(345):在 org.appcelerator.titanium.kroll.KrollHandlerThread.run(KrollHandlerThread.java:86)E/AndroidRuntime(345):引起:java.lang.IllegalArgumentException:大小必须> = 0E/AndroidRuntime(345):在 java.io.ByteArrayOutputStream。(ByteArrayOutputStream.java:67)E/AndroidRuntime(345):在 org.appcelerator.titanium.util.TiStreamHelper.toByteArray(TiStreamHelper.java:106)E/AndroidRuntime(345):在 org.appcelerator.titanium.TiBlob.getBytes(TiBlob.java:120)

最佳答案

您应该使用 evalJS,而不是使用 url 属性来注入(inject)您的 javascript。

您可以从文件中读取本地 CSS 数据并像这样插入:

// read the css content from styles.css    
var cssContent = Titanium.Filesystem.getFile('styles.css');

// make it available as a variable in the webview
webview.evalJS("var myCssContent = " + JSON.stringify(String(cssContent.read())) + ";");

// create the style element with the css content
webview.evalJS("var s = document.createElement('style'); s.setAttribute('type', 'text/css'); s.innerHTML = myCssContent; document.getElementsByTagName('head')[0].appendChild(s);");

关于javascript - UIwebview 中的 CSS 注入(inject),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/6920081/

28 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com