0
votes

I have a small CSS file I use with JavaScript to override the CSS on a website loaded in a WebView. I would like to load it locally instead of need to access it online. I have gotten the JavaScript to work perfectly when the file is uploaded to a temporary filehost, so it works fine, but something regarding permissions won't let me use it as an asset.

I've tried setting permissions, changing the WebView's settings to allow MixedContentMode always, tried using LoadData, etc, but it always throws the "Not allowed" issue. The asset has the AndroidAsset build action.

string jsToInject = "javascript: var link=document.createElement('link');link.href='file:///android_asset/newsOverride.css';link.type ='text/css'; link.rel ='stylesheet';document.getElementsByTagName('head')[0].appendChild(link);";

view.LoadUrl(jsToInject);

When the link.href field is set to the file hosted on the web somewhere, it loads the CSS as expected. But when it's selected as an asset it simply throws I/chromium(11626): [INFO:CONSOLE(1)] "Not allowed to load local resource: file:///android_asset/newsOverride.css", source: (1)

All I've been able to find on the error is with Xamarin Forms, not Xamarin Android, any help is appreciated.

1
Did you try to implementing the WebViewClient with a shouldInterceptRequest override like this thread stackoverflow.com/questions/19997146/… - Leon Lu - MSFT
That did it. It had been falling into the catch block but that was because you don't need file:///android_asset/ before the asset filename in Xamarin. What a weird workaround. - W. Hoffman
Yes, that is weird, did you solve this issue? If so, can I post my comment to answer and accept it? Helps other searching for questions to help on so they don't waste time on questions that are answered. Thanks! - Leon Lu - MSFT
Yes, it's working as it should. Go ahead and post it as an answer. - W. Hoffman

1 Answers

0
votes

This issue should implementing the WebViewClient with a shouldInterceptRequest override like this thread. Kitkat kills: Not allowed to load local resource: file:///android_asset/webkit/android-weberror.png