开发者

CSS injection in UIwebview

I'm trying to use this example posted 开发者_如何学Pythonhttps://gist.github.com/811993 ; I am trying to use a custom css file for the website www.baomoi.com for my parents to make it more minimilistic and high contrast. This code example causes a force close as it loads. I have the styles.css in the /Resources folder. How can I determine where it is causing the error?

Thank you all in advance for your help.

https://gist.github.com/811993

In debug this is what I am getting: E/KrollCallback( 382): (kroll$1: app://app.js) [234,4575] Error, invocation: [callMethod UI.WebView.UI.WebView:event:load null], message: size must be >= 0 E/KrollCallback( 382): java.lang.IllegalArgumentException: size must be >= 0E/AndroidRuntime( 345): at org.appcelerator.titanium.kroll.KrollHandlerThread.run(KrollHandlerThread.java:86) E/AndroidRuntime( 345): Caused by: java.lang.IllegalArgumentException: size must be >= 0 E/AndroidRuntime( 345): at java.io.ByteArrayOutputStream.(ByteArrayOutputStream.java:67) E/AndroidRuntime( 345): at org.appcelerator.titanium.util.TiStreamHelper.toByteArray(TiStreamHelper.java:106) E/AndroidRuntime( 345): at org.appcelerator.titanium.TiBlob.getBytes(TiBlob.java:120)


Instead of using the url property to inject your javascript, you should use evalJS.

You can read your local CSS Data from a file and insert into like this:

// 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);");
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜