gpt4 book ai didi

android - 从 Android Studio 的 url 注入(inject) CSS

转载 作者:行者123 更新时间:2023-11-28 02:12:55 25 4
gpt4 key购买 nike

我正在使用 InjectCSS 脚本在 webview 上使用额外的 css 文件。但是脚本从 assets 文件夹中获取 CSS 文件,我想要外部托管的 css 文件。

    private void injectCSS() {
try {


InputStream inputStream = getAssets().open("style.css");

byte[] buffer = new byte[inputStream.available()];
inputStream.read(buffer);
inputStream.close();

String encoded = Base64.encodeToString(buffer, Base64.NO_WRAP);
wv.loadUrl("javascript:(function() {" +
"var parent = document.getElementsByTagName('head').item(0);" +
"var style = document.createElement('style');" +
"style.type = 'text/css';" +
// Tell the browser to BASE64-decode the string into your script !!!
"style.innerHTML = window.atob('" + encoded + "');" +
"parent.appendChild(style)" +
"})();");

} catch (Exception e) {
e.printStackTrace();
}
}

我尝试将输入流更改为 url,但没有成功。

InputStream inputSteam = new URL("http://www.website.com/folder/style.css").openStream();

最佳答案

如果需要使用BASE64编码,需要this

InputStream inputSteam = new URL("http://www.website.com/folder/style.css").openStream();
String encoded = new String(readBytes(inputStream), Base64.NO_WRAP);

// ...


public byte[] readBytes(InputStream inputStream) throws IOException {
// this dynamically extends to take the bytes you read
ByteArrayOutputStream byteBuffer = new ByteArrayOutputStream();

// this is storage overwritten on each iteration with bytes
int bufferSize = 1024;
byte[] buffer = new byte[bufferSize];

// we need to know how may bytes were read to write them to the byteBuffer
int len = 0;
while ((len = inputStream.read(buffer)) != -1) {
byteBuffer.write(buffer, 0, len);
}

// and then we can return your byte array.
return byteBuffer.toByteArray();
}

关于android - 从 Android Studio 的 url 注入(inject) CSS,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48642591/

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