minh
minh

Reputation: 133

CSS injection in UIwebview

I'm trying to use this example posted https://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)

Upvotes: 3

Views: 1547

Answers (1)

favo
favo

Reputation: 5458

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

Upvotes: 3

Related Questions