admin
2021-12-22 0a1336cd2b95126d66d6f3126cb48a446cdbfad1
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
package com.hanju.video.app.ui.video.parser;
 
import android.os.Bundle;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.webkit.ConsoleMessage;
import android.webkit.JavascriptInterface;
import android.webkit.ValueCallback;
import android.webkit.WebChromeClient;
import android.webkit.WebSettings;
import android.webkit.WebView;
 
import com.hanju.video.app.util.downutils.StringUtils;
 
import java.util.List;
 
import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.fragment.app.Fragment;
 
/**
 * 视频播放路径解析
 */
public class VideoPlayUrlParseFragment extends Fragment {
    private String TAG = "VideoPlayUrlParseFragment";
    private WebView webView;
    private String url;
 
    private WriteHandlingWebViewClient mWriteHandlingWebViewClient;
 
    private void initView() {
        WebSettings settings = webView.getSettings();
        settings.setJavaScriptEnabled(true);
        mWriteHandlingWebViewClient = new WriteHandlingWebViewClient(webView);
        webView.setWebViewClient(mWriteHandlingWebViewClient);
        webView.setWebChromeClient(new WebChromeClient() {
            @Override
            public boolean onConsoleMessage(ConsoleMessage consoleMessage) {
                Log.i(TAG, consoleMessage.lineNumber() + ":" + consoleMessage.message());
                return super.onConsoleMessage(consoleMessage);
            }
        });
    }
 
    @Override
    public void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
    }
 
    @Nullable
    @Override
    public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        webView = new WebView(getContext());
        initView();
 
        return webView;
    }
 
 
    public void startParse(String url, List<ParseParams> parseParamsList, final IVideoParseListener videoParseListener) {
        this.url = url;
        webView.loadUrl(url);
 
        mWriteHandlingWebViewClient.init(parseParamsList, new WriteHandlingWebViewClient.OnResponseListener() {
 
            @Override
            public void onResult(WriteHandlingWebResourceRequest request, ParseParams parseParam, String result) {
                webView.post(new Runnable() {
                    @Override
                    public void run() {
                        webView.loadUrl("javascript:" + parseParam.getParseJS());
 
//                        webView.loadUrl("javascript:help.log(document.getElementsByTagName('html')[0].innerHTML)");
 
//                        webView.loadUrl("javascript:"+String.format("parseResult(\"%s\")", result));
 
                        String requestUrl = request.getUrl().toString();
                        webView.evaluateJavascript(String.format("parseResult(\"%s\",\"%s\")", requestUrl, parseParam.needResult ? result.replace("\"", "\\" + "\"") : ""), new ValueCallback<String>() {
 
                            @Override
                            public void onReceiveValue(String value) {
                                if (StringUtils.isNullOrEmpty(value))
                                    return;
                                if (value.contains("url=")) {
                                    String ps = value.split("\\?")[1];
                                    String[] sts = ps.split("&");
                                    for (String st : sts) {
                                        if (st.contains("url=")) {
                                            value = st.replace("url=", "");
                                            break;
                                        }
                                    }
                                }
 
                                Log.i(TAG, "解析结果:" + value);
                                if (videoParseListener != null) {
                                    videoParseListener.onSuccess(value.replace("\"", ""));
                                }
                            }
                        });
 
                    }
                });
            }
        });
    }
 
    @Override
    public void onResume() {
        super.onResume();
    }
 
    @Override
    public void onDestroy() {
        super.onDestroy();
        webView.destroy();
    }
 
    public interface IVideoParseListener {
        public void onSuccess(String result);
 
        public void onFail(String msg);
    }
 
    public static class ParseParams {
 
        private String interceptRegex;
        private String parseJS;
        private boolean needResult;
 
        public boolean isNeedResult() {
            return needResult;
        }
 
        public void setNeedResult(boolean needResult) {
            this.needResult = needResult;
        }
 
        public ParseParams(String interceptRegex, String parseJS, boolean needResult) {
            this.interceptRegex = interceptRegex;
            this.parseJS = parseJS;
            this.needResult = needResult;
        }
 
        public String getInterceptRegex() {
            return interceptRegex;
        }
 
        public void setInterceptRegex(String interceptRegex) {
            this.interceptRegex = interceptRegex;
        }
 
        public String getParseJS() {
            return parseJS;
        }
 
        public void setParseJS(String parseJS) {
            this.parseJS = parseJS;
        }
    }
 
 
}