-
Notifications
You must be signed in to change notification settings - Fork 4
Expand file tree
/
Copy pathApp.js
More file actions
378 lines (339 loc) · 17.6 KB
/
Copy pathApp.js
File metadata and controls
378 lines (339 loc) · 17.6 KB
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
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
import React, { Component, createRef } from "react"
import axios from "axios"
import { Image, View, StatusBar, Dimensions, Text, BackHandler } from "react-native"
import WebView from "react-native-webview"
import RNFS from "react-native-fs"
import storage from "./utils/storage"
export default class App extends Component {
width = Dimensions.get("window").width
height = Dimensions.get("window").height
a = this.height / 400
timer = null
webview = createRef()
webviewState = 0
showMessage = data => {
if (this.timer) clearTimeout(this.timer)
this.setState({
message: data
})
this.timer = setTimeout(() => {
this.setState({
message: null
})
}, 1500)
}
listen = async e => {
const { type, data, withCookie, time } = JSON.parse(e.nativeEvent.data)
let resData
switch (type) {
case "console":
console.log(...data)
return
case "cookie":
console.log("接收到 cookie")
console.log(data)
await storage.save({
key: "cookie",
data
})
this.cookie = data
// 这里必须先切换为0,再切换为1,直接切换不能重新 render 停留在登录页面,不知道为啥
this.setState({
show: 0
})
await this.initApp()
return
case "message":
this.showMessage(data)
return
case "changeState":
this.webviewState = data
return
case "isTV":
await storage.save({
key: "isTV",
data
})
this.isTV = data
return
case "login":
console.log("收到登录请求")
this.setState({
show: 2
})
return
case "liveHistory":
console.log("保存直播历史记录")
let arr
try {
arr = await storage.load({ key: "liveHistory" })
} catch (error) {
arr = []
}
arr = [data, ...arr]
storage.save({
key: "liveHistory",
data: arr
})
return
case "axios":
console.log("接收到 axios 请求")
if (withCookie) {
data.headers = {}
data.headers.cookie = this.cookie
}
try {
resData = (await axios(data)).data
this.webview.current.postMessage(JSON.stringify({
type,
time,
data: resData
}))
} catch (error) {
this.webview.current.postMessage(JSON.stringify({
type,
time,
data: "Network Error"
}))
}
return
default:
return
}
}
state = {
show: 0,
html: "",
message: null
}
backAction = () => {
if (this.webviewState === 0) return false
else {
this.webview.current.postMessage(JSON.stringify({
type: "back"
}))
return true
}
}
async componentDidMount() {
this.backHandler = BackHandler.addEventListener(
"hardwareBackPress",
this.backAction
)
await this.initApp()
}
initApp = async () => {
if (!this.react) this.react = await RNFS.readFileAssets("react.production.min.js")
if (!this.reactdom) this.reactdom = await RNFS.readFileAssets("react-dom.production.min.js")
if (!this.player) this.player = await RNFS.readFileAssets("xgplayer.js")
if (!this.hlsjsplayer) this.hlsjsplayer = await RNFS.readFileAssets("HlsJsPlayer.js")
if (!this.flvjsplayer) this.flvjsplayer = await RNFS.readFileAssets("FlvJsPlayer.js")
if (!this.babel) this.babel = await RNFS.readFileAssets("babel.min.js")
if (!this.main) this.main = await RNFS.readFileAssets("main.js")
if (!this.cookie) {
try {
this.cookie = await storage.load({ key: "cookie" })
} catch (error) {
this.cookie = ""
}
}
if (!this.isTV) {
try {
this.isTV = await storage.load({ key: "isTV" })
} catch (error) {
this.isTV = -1
}
}
if (!this.liveHistory) {
try {
this.liveHistory = await storage.load({ key: "liveHistory" })
} catch (error) {
this.liveHistory = []
}
}
this.setState({
show: 1,
html: `
<!DOCTYPE html>
<html lang="zh">
<head>
<meta charset="UTF-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="initial-scale=0, maximum-scale=0, user-scalable=0">
<title>AcFun TV</title>
<script>
${this.react}
</script>
<script>
${this.reactdom}
</script>
<script>
${this.player}
</script>
<script>
${this.hlsjsplayer}
</script>
<script>
${this.flvjsplayer}
</script>
<script>
${this.babel}
</script>
<style>
html, body, #root, #app {
width: 100%;
height: 100%;
}
* {
margin: 0;
padding: 0;
}
</style>
</head>
<body>
<div id="root"></div>
</body>
<script type="text/babel">
const isTV = ${this.isTV}
const cookie = "${this.cookie}"
const liveHistory = ${JSON.stringify(this.liveHistory)}
const postMessage = message => window.ReactNativeWebView.postMessage(JSON.stringify(message))
console.log = (...rest) => postMessage({
type: "console",
data: rest
})
console.warn = console.log
console.error = console.log
console.debug = console.log
console.info = console.log
try {
${this.main}
} catch (error) {
console.log(error.toString())
}
console.log(!!Player)
</script>
</html>
`
})
}
render() {
const { listen, webview, width, height, a } = this
const { show, html, message } = this.state
return (
<>
<StatusBar hidden={true} />
{
(() => {
switch (show) {
case 0:
return (
<Image source={require("./images/loading.png")} resizeMode="cover" style={{ width, height }} />
)
case 1:
return (
<WebView style={{ flex: 1 }} source={{ html }} onMessage={listen} ref={webview} mediaPlaybackRequiresUserAction={false} />
)
case 2:
return (
<WebView
style={{ flex: 1 }}
source={{ uri: "https://www.acfun.cn/login?returnUrl=https%3A%2F%2Fwww.acfun.cn%2Finfo%2F%23page%3Dabout" }}
onMessage={listen}
userAgent="Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/97.0.4692.20 Safari/537.36"
injectedJavaScript={`
const postMessage = message => window.ReactNativeWebView.postMessage(JSON.stringify(message))
const meta = document.createElement('meta')
meta.setAttribute('content', 'initial-scale=0, maximum-scale=0, user-scalable=0')
meta.setAttribute('name', 'viewport')
document.head.appendChild(meta)
function hideOthers(e) {
if (e === document.body) return
for (let i of e.parentElement.children) {
if (i.style && i !== e) i.style.display = "none"
}
e.parentElement.style = "margin: 0; padding: 0; background-color: transparent; background-image: none; border: none; "
hideOthers(e.parentElement)
}
if (location.href === "https://www.acfun.cn/login?returnUrl=https%3A%2F%2Fwww.acfun.cn%2Finfo%2F%23page%3Dabout") {
const style = document.createElement("style")
style.innerHTML = ".status { display: none !important; }"
document.head.appendChild(style)
const code = document.querySelector("#code img")
hideOthers(code)
const bg = document.createElement("div")
bg.style = "position: fixed; width: 100%; height: 100%; background-image: url(https://s3.bmp.ovh/imgs/2022/01/07cee7b5cfdf4ad7.jpg); background-size: cover; background-repeat: no-repeat; background-position: center; z-index: 9990;"
document.body.appendChild(bg)
const cover = document.createElement("div")
cover.style = "position: fixed; width: 100%; height: 100%; background-color: rgba(0, 0, 0, 0.6); z-index: 9991;"
document.body.appendChild(cover)
const width = parseFloat(getComputedStyle(bg).width)
const height = parseFloat(getComputedStyle(bg).height)
const o = height / 720
document.querySelector("#main").style.zIndex = "9992"
code.style = \`position: fixed; left: \${160 * o}px; top: \${180 * o}px; width: \${300 * o}px; height: \${300 * o}px\`
const tip = document.createElement("div")
tip.innerHTML = "请使用 AcFun 手机版扫码登录"
tip.style = \`position: fixed; left: \${160 * o}px; top: \${480 * o}px; width: \${300 * o}px; line-height: \${60 * o}px; color: #FD4C5D; font-weight: bold; text-align: center; z-index: 9993; font-size: \${20 * o}px\`
document.body.appendChild(tip)
const account = document.createElement("input")
account.placeholder = "请输入手机号或者邮箱"
account.autofocus = true
account.id = "account"
account.type = "text"
account.style = \`position: fixed; right: \${160 * o}px; top: \${180 * o}px; width: \${500 * o}px; height: \${80 * o}px; font-size: \${40 * o}px; padding-left: \${40 * o}px; background-color: white; z-index: 99999; box-sizing: border-box;\`
document.body.appendChild(account)
const password = document.createElement("input")
password.placeholder = "请输入密码"
password.id = "password"
password.type = "password"
password.style = \`position: fixed; right: \${160 * o}px; top: \${290 * o}px; width: \${500 * o}px; height: \${80 * o}px; font-size: \${40 * o}px; padding-left: \${40 * o}px; background-color: white; z-index: 99999; box-sizing: border-box;\`
document.body.appendChild(password)
const login = document.createElement("button")
login.id = "login"
login.innerHTML = "登录"
login.onclick = () => {
document.querySelector("#ipt-account-login").value = account.value
document.querySelector("#ipt-pwd-login").value = password.value
document.querySelector(".btn-login").click()
}
login.onfocus = () => login.style.backgroundColor = "red"
login.onblur = () => login.style.backgroundColor = "#FD4C5D"
login.style = \`position: fixed; right: \${160 * o}px; top: \${400 * o}px; width: \${500 * o}px; height: \${80 * o}px; line-height: \${80 * o}px; font-size: \${40 * o}px; background-color: #FD4C5D; z-index: 99999; box-sizing: border-box; text-align: center; color: white;\`
document.body.appendChild(login)
const loginTip = document.createElement("div")
loginTip.innerHTML = "或者使用键盘上下方向键来进行登录"
loginTip.style = \`position: fixed; right: \${160 * o}px; top: \${480 * o}px; width: \${500 * o}px; line-height: \${60 * o}px; color: #FD4C5D; font-weight: bold; text-align: center; z-index: 9993; font-size: \${20 * o}px;\`
document.body.appendChild(loginTip)
const targetNode = document.getElementsByClassName("status")[0]
const config = { attributes: true, childList: true, subtree: true }
const callback = function (mutationsList, observer) {
if (getComputedStyle(targetNode).display !== "none") {
console.log("点击了按钮")
document.querySelector(".refresh-btn").click()
}
}
const observer = new MutationObserver(callback)
observer.observe(targetNode, config)
}
if (location.href === "https://www.acfun.cn/info/#page=about") {
window.ReactNativeWebView.postMessage(JSON.stringify({
type: "cookie",
data: document.cookie
}))
}
`}
/>
)
default:
return
}
})()
}
<View style={{ position: "absolute", width, height, left: 0, top: 0, justifyContent: "center", alignItems: "center", display: message ? "flex" : "none" }} >
<Text style={{ lineHeight: 50 * a, fontSize: 30 * a, backgroundColor: "rgba(0, 0, 0, 0.5)", paddingLeft: 20 * a, paddingRight: 20 * a, borderRadius: 6 * a, color: "white" }}>{message}</Text>
</View>
</>
)
}
}