|
|
package com.br_technology.securitytrain_master.util;
|
|
|
|
|
|
import android.content.Context;
|
|
|
import android.text.TextUtils;
|
|
|
|
|
|
import com.br_technology.securitytrain_master.base.view.BaseApplication;
|
|
|
|
|
|
import org.jsoup.nodes.Document;
|
|
|
import org.jsoup.nodes.Element;
|
|
|
|
|
|
import java.util.List;
|
|
|
|
|
|
public class HtmlConvert {
|
|
|
|
|
|
public static String convert(Document doc, boolean isDark, String url, String img) {
|
|
|
List<Element> list = doc.select("img");
|
|
|
List<Element> listVideo = doc.select("video");
|
|
|
for (Element element : listVideo) {
|
|
|
String dataSource = element.attr("src");
|
|
|
element.attr("object-fit", "fill");
|
|
|
element.attr("preload", "meta");
|
|
|
element.removeAttr("height");
|
|
|
|
|
|
Element parent = element.parent();
|
|
|
parent.attr("width", "100%");
|
|
|
List<Element> elements = element.children();
|
|
|
if (elements != null && elements.size() > 0) {
|
|
|
// elements.get(0).attr("src", "http://vfile1.hnntv.cn/2020/1605/0853/2443/160508532443.ssm/160508532443.m3u8");
|
|
|
element.attr("controls", "controls");
|
|
|
element.attr("poster", TextUtils.isEmpty(img) ? "file:///android_res/mipmap/loading_web.png" : img);
|
|
|
} else {
|
|
|
if (TextUtils.isEmpty(dataSource) || !dataSource.startsWith("http")) {
|
|
|
if (!TextUtils.isEmpty(url)) {
|
|
|
Element sou = doc.createElement("source");
|
|
|
sou.attr("src", url);
|
|
|
// element.attr("src", url);
|
|
|
sou.appendTo(element);
|
|
|
element.attr("controls", "controls");
|
|
|
element.attr("poster", TextUtils.isEmpty(img) ? "file:///android_res/mipmap/loading_web.png" : img);
|
|
|
} else {
|
|
|
element.remove();
|
|
|
}
|
|
|
}
|
|
|
}
|
|
|
}
|
|
|
for (Element element : list) {
|
|
|
element.removeAttr("class");
|
|
|
String souce = element.attr("src");
|
|
|
if (loadImage(BaseApplication.Companion.getInstance().getApplicationContext())) {
|
|
|
// if (!souce.startsWith("http")) {
|
|
|
// element.attr("src", ConnectUrl.BASE_URL_IMG() + souce);
|
|
|
// }
|
|
|
if (element.hasParent()) {
|
|
|
Element parent = element.parent();
|
|
|
if (parent.tagName().equals("a")) {
|
|
|
element.attr("alt", "none_click");
|
|
|
}
|
|
|
}
|
|
|
} else {
|
|
|
element.attr("src", "file:///android_res/mipmap/loading_web.png");
|
|
|
if (element.hasParent()) {
|
|
|
Element parent = element.parent();
|
|
|
if (parent.tagName().equals("a")) {
|
|
|
element.attr("alt", "none_click");
|
|
|
}
|
|
|
}
|
|
|
}
|
|
|
// element.attr("onclick",onClick(souce,arr));
|
|
|
}
|
|
|
doc.select("br").remove();
|
|
|
// int fontSize = SettingKits.getFontSize(BaseApplication.Companion.getInstance().getApplicationContext());
|
|
|
int fontSize = 0;
|
|
|
List<Element> spanList = doc.select("span");
|
|
|
for (Element element : spanList) {
|
|
|
updateStyle(element, "font-size", "small");
|
|
|
}
|
|
|
Element head = doc.head();
|
|
|
head.append("<meta charset=\"utf-8\">")
|
|
|
.append("<meta name=\"viewport\" content=\"initial-scale=1.0, maximum-scale=1.0, user-scalable=no\" />");
|
|
|
if (isDark) {
|
|
|
head.append("<style>" +
|
|
|
"body{text-indent:0rem!important;" +
|
|
|
"text-align:justify!important;text-justify:inter-ideograph!important;" +
|
|
|
"word-break:break-all!important;font-color:#ffffff;overflow: hidden;}" +
|
|
|
"img{max-width: 100%!important;height:auto}" +
|
|
|
"video{width: 100%;display: block;}" +
|
|
|
"p{padding:0px;overflow: hidden;" +
|
|
|
"font-size:" + TextKits.fontSize(fontSize) + "px;" +
|
|
|
"color:#ffffff;" +
|
|
|
"}" +
|
|
|
"span{color:#ffffff;text-indent:2em;}" +
|
|
|
"</style>");
|
|
|
} else {
|
|
|
head.append("<style>" +
|
|
|
"body{text-indent:0rem!important;" +
|
|
|
"text-align:justify!important;text-justify:inter-ideograph!important;" +
|
|
|
"word-break:break-all!important;}" +
|
|
|
"img{max-width: 100%!important;height:auto}" +
|
|
|
"p{font-size:" + TextKits.fontSize(fontSize) + "px" +
|
|
|
";}" +
|
|
|
"video{width:100%;}" +
|
|
|
"span{text-indent:2em;}" +
|
|
|
"</style>");
|
|
|
}
|
|
|
// if (listVideo.size() > 0) {
|
|
|
// head.append("<link href=\"file:///android_asset/video-js.min.css\" rel=\"stylesheet\"></link>" +
|
|
|
// "<script src=\"file:///android_asset/video.min.js\"></script>");
|
|
|
// }
|
|
|
List<Element> elements = doc.select("p");
|
|
|
for (Element element : elements) {
|
|
|
List<Element> list1 = element.children();
|
|
|
if (list1 == null || list1.isEmpty()) {
|
|
|
if (!isTextCenter(element)) {
|
|
|
updateStyle(element, "text-indent", "2em");
|
|
|
}
|
|
|
} else {
|
|
|
int count = 0;
|
|
|
for (Element element1 : list1) {
|
|
|
if (element1.is("img")
|
|
|
|| element1.is("video")
|
|
|
|| element1.is("audio")
|
|
|
|| element1.is("a")
|
|
|
|| (element1.children() != null && !element1.children().isEmpty())
|
|
|
|| isTextCenter(element1)) {
|
|
|
count++;
|
|
|
}
|
|
|
}
|
|
|
if (count == 0) {
|
|
|
updateStyle(element, "text-indent", "2em");
|
|
|
}
|
|
|
}
|
|
|
}
|
|
|
return doc.toString();
|
|
|
}
|
|
|
|
|
|
|
|
|
public static String convertNone(Document doc, boolean isDark) {
|
|
|
List<Element> list = doc.select("img");
|
|
|
List<Element> listVideo = doc.select("video");
|
|
|
for (Element element : listVideo) {
|
|
|
element.remove();
|
|
|
}
|
|
|
for (Element element : list) {
|
|
|
element.removeAttr("class");
|
|
|
String souce = element.attr("src");
|
|
|
if (loadImage(BaseApplication.Companion.getInstance().getApplicationContext())) {
|
|
|
// if (!souce.startsWith("http")) {
|
|
|
// element.attr("src", ConnectUrl.BASE_URL_IMG() + souce);
|
|
|
// }
|
|
|
if (element.hasParent()) {
|
|
|
Element parent = element.parent();
|
|
|
if (parent.tagName().equals("a")) {
|
|
|
element.attr("alt", "none_click");
|
|
|
}
|
|
|
}
|
|
|
} else {
|
|
|
String url = "file:///android_res/mipmap/loading_web.png";
|
|
|
element.attr("src", url);
|
|
|
}
|
|
|
// element.attr("onclick",onClick(souce,arr));
|
|
|
}
|
|
|
doc.select("br").remove();
|
|
|
// int fontSize = SettingKits.getFontSize(BaseApplication.Companion.getInstance().getApplicationContext());
|
|
|
int fontSize = 0;
|
|
|
List<Element> spanList = doc.select("span");
|
|
|
for (Element element : spanList) {
|
|
|
updateStyle(element, "font-size", "small");
|
|
|
}
|
|
|
Element head = doc.head();
|
|
|
head.append("<meta charset=\"utf-8\">")
|
|
|
.append("<meta name=\"viewport\" content=\"initial-scale=1.0, maximum-scale=1.0, user-scalable=no\" />");
|
|
|
if (isDark) {
|
|
|
head.append("<style>" +
|
|
|
"body{text-indent:0rem!important;" +
|
|
|
"text-align:justify!important;text-justify:inter-ideograph!important;" +
|
|
|
"word-break:break-all!important;font-color:#ffffff;overflow: hidden;}" +
|
|
|
"img{max-width: 100%!important;height:auto}" +
|
|
|
"video{width: 100%;display: block;}" +
|
|
|
"p{padding:0px;overflow: hidden;" +
|
|
|
"font-size:" + TextKits.fontSize(fontSize) + "px;" +
|
|
|
"color:#ffffff;" +
|
|
|
"}" +
|
|
|
"span{color:#ffffff;text-indent:2em;}" +
|
|
|
"</style>");
|
|
|
} else {
|
|
|
head.append("<style>" +
|
|
|
"body{text-indent:0rem!important;" +
|
|
|
"text-align:justify!important;text-justify:inter-ideograph!important;" +
|
|
|
"word-break:break-all!important;}" +
|
|
|
"img{max-width: 100%!important;height:auto}" +
|
|
|
"p{font-size:" + TextKits.fontSize(fontSize) + "px" +
|
|
|
";}" +
|
|
|
"video{width: 100%;height:260;}" +
|
|
|
"span{text-indent:2em;}" +
|
|
|
"</style>");
|
|
|
}
|
|
|
List<Element> elements = doc.select("p");
|
|
|
for (Element element : elements) {
|
|
|
List<Element> list1 = element.children();
|
|
|
if (list1 == null || list1.isEmpty()) {
|
|
|
if (!isTextCenter(element)) {
|
|
|
updateStyle(element, "text-indent", "2em");
|
|
|
}
|
|
|
} else {
|
|
|
int count = 0;
|
|
|
for (Element element1 : list1) {
|
|
|
if (element1.is("img")
|
|
|
|| element1.is("video")
|
|
|
|| element1.is("audio")
|
|
|
|| element1.is("a")
|
|
|
|| (element1.children() != null && !element1.children().isEmpty())
|
|
|
|| isTextCenter(element1)) {
|
|
|
count++;
|
|
|
}
|
|
|
}
|
|
|
if (count == 0) {
|
|
|
updateStyle(element, "text-indent", "2em");
|
|
|
}
|
|
|
}
|
|
|
}
|
|
|
return doc.toString();
|
|
|
}
|
|
|
|
|
|
private static boolean loadImage(Context context) {
|
|
|
// return DeviceUtils.isConnectionWifi(context) || !SettingKits.getLoadWifiImg(context);
|
|
|
return true;
|
|
|
}
|
|
|
|
|
|
public static boolean isTextCenter(Element element) {
|
|
|
if (element.hasAttr("text-align")) {
|
|
|
String text = element.attr("text-align");
|
|
|
return text.contains("center");
|
|
|
}
|
|
|
if (element.hasAttr("style")) {
|
|
|
String style = element.attr("style");
|
|
|
return style.contains("center");
|
|
|
}
|
|
|
return false;
|
|
|
}
|
|
|
|
|
|
private static void updateStyle(Element element, String attr, String value) {
|
|
|
if (element.hasAttr("style")) {
|
|
|
String styleStr = element.attr("style");
|
|
|
if (styleStr.contains(attr)) {
|
|
|
String s1 = styleStr.substring(0, styleStr.indexOf(attr));
|
|
|
String s2 = styleStr.substring(styleStr.indexOf(attr));
|
|
|
String s3 = s2.contains(";") ? s2.substring(s2.indexOf(";")) : "";
|
|
|
element.removeAttr("style");
|
|
|
element.attr("style", s1 + attr + ":" + value + s3);
|
|
|
} else {
|
|
|
element.removeAttr("style");
|
|
|
element.attr("style", styleStr + attr + ":" + value);
|
|
|
}
|
|
|
} else {
|
|
|
element.attr("style", attr + ":" + value);
|
|
|
}
|
|
|
}
|
|
|
} |
...
|
...
|
|