|
1 | 1 | package com.heyanle.easybangumi4.plugin.js.component
|
2 | 2 |
|
3 | 3 | import androidx.annotation.WorkerThread
|
| 4 | +import com.heyanle.easybangumi4.plugin.js.runtime.JSScope |
4 | 5 | import com.heyanle.easybangumi4.source_api.Source
|
5 | 6 | import com.heyanle.easybangumi4.source_api.component.ComponentWrapper
|
6 | 7 | import com.heyanle.easybangumi4.source_api.component.page.PageComponent
|
7 | 8 | import com.heyanle.easybangumi4.source_api.component.page.SourcePage
|
| 9 | +import org.mozilla.javascript.Function |
8 | 10 | import org.mozilla.javascript.ScriptableObject
|
9 | 11 | import org.mozilla.javascript.Context as JSContext
|
10 | 12 |
|
11 | 13 | /**
|
12 | 14 | * Created by heyanle on 2024/7/27.
|
13 | 15 | * https://github.com/heyanLE
|
14 | 16 | */
|
15 |
| -class JSPageComponent: ComponentWrapper(), PageComponent { |
| 17 | +class JSPageComponent( |
| 18 | + private val jsScope: JSScope, |
| 19 | + private val getMainTabs: Function, |
| 20 | + private val getSubTabs: Function, |
| 21 | + private val initPageKey: Function, |
| 22 | + private val getContent: Function, |
| 23 | +): ComponentWrapper(), PageComponent { |
16 | 24 |
|
17 | 25 |
|
18 | 26 | companion object {
|
19 | 27 |
|
20 |
| - const val CheckJsCode = """ |
21 |
| - typeof PageComponent_getMainTabs == 'function' && |
22 |
| - typeof PageComponent_getSubTabs == 'function' && |
23 |
| - typeof PageComponent_initPageKey == 'function' && |
24 |
| - typeof PageComponent_getContent == 'function' |
25 |
| - """ |
26 | 28 |
|
27 |
| - @WorkerThread |
28 |
| - fun check( jsContext: JSContext, scope: ScriptableObject) { |
29 |
| - jsContext.evaluateString(scope, CheckJsCode, null, 1, null) |
| 29 | + const val FUNCTION_NAME_GET_MAIN_TABS = "PageComponent_getMainTabs" |
| 30 | + const val FUNCTION_NAME_GET_SUB_TABS = "PageComponent_getSubTabs" |
| 31 | + const val FUNCTION_NAME_INIT_PAGE_KEY = "PageComponent_initPageKey" |
| 32 | + const val FUNCTION_NAME_GET_CONTENT = "PageComponent_getContent" |
| 33 | + |
| 34 | + suspend fun of (jsScope: JSScope) : JSPageComponent ? { |
| 35 | + return jsScope.runWithScope { _, scriptable -> |
| 36 | + val getMainTabs = scriptable.get(FUNCTION_NAME_GET_MAIN_TABS, scriptable) as? Function |
| 37 | + val getSubTabs = scriptable.get(FUNCTION_NAME_GET_SUB_TABS, scriptable) as? Function |
| 38 | + val initPageKey = scriptable.get(FUNCTION_NAME_INIT_PAGE_KEY, scriptable) as? Function |
| 39 | + val getContent = scriptable.get(FUNCTION_NAME_GET_CONTENT, scriptable) as? Function |
| 40 | + if(getMainTabs == null || getSubTabs == null || initPageKey == null || getContent == null){ |
| 41 | + return@runWithScope null |
| 42 | + } |
| 43 | + return@runWithScope JSPageComponent(jsScope, getMainTabs, getSubTabs, initPageKey, getContent) |
| 44 | + } |
30 | 45 | }
|
31 | 46 | }
|
32 | 47 |
|
|
0 commit comments