mirror of
https://github.com/woodchen-ink/clash-and-dashboard.git
synced 2025-07-18 14:01:56 +08:00
Refactor: refactor jsbridge lib makes it easily use
This commit is contained in:
parent
acaee8c63a
commit
d64d1786e8
@ -15,7 +15,7 @@
|
||||
/**
|
||||
* declare javascript bridge API
|
||||
*/
|
||||
export interface JsBridge {
|
||||
export interface JsBridgeAPI {
|
||||
|
||||
/**
|
||||
* Register a javascript bridge event handle
|
||||
@ -41,7 +41,7 @@ declare global {
|
||||
/**
|
||||
* Global jsbridge instance
|
||||
*/
|
||||
WebViewJavascriptBridge?: JsBridge | null
|
||||
WebViewJavascriptBridge?: JsBridgeAPI | null
|
||||
|
||||
/**
|
||||
* Global jsbridge init callback
|
||||
@ -52,12 +52,44 @@ declare global {
|
||||
|
||||
}
|
||||
|
||||
/**
|
||||
* Check if perched in ClashX Runtime
|
||||
*/
|
||||
export function isClashX () {
|
||||
return navigator.userAgent === 'ClashX Runtime'
|
||||
}
|
||||
|
||||
/**
|
||||
* Closure save jsbridge instance
|
||||
*/
|
||||
export let jsBridge: JsBridge = null
|
||||
|
||||
/**
|
||||
* JsBridge class
|
||||
*/
|
||||
export class JsBridge {
|
||||
|
||||
instance: JsBridgeAPI = null
|
||||
|
||||
constructor (callback = jsbridge => {}) {
|
||||
if (window.WebViewJavascriptBridge) {
|
||||
this.instance = window.WebViewJavascriptBridge
|
||||
callback(this.instance)
|
||||
}
|
||||
|
||||
// init jsbridge
|
||||
this.initBridge(jsBridge => {
|
||||
this.instance = jsBridge
|
||||
callback(jsBridge)
|
||||
})
|
||||
}
|
||||
|
||||
/**
|
||||
* setup a jsbridge before app render
|
||||
* @param {Function} cb callback when jsbridge initialized
|
||||
* @see https://github.com/marcuswestin/WebViewJavascriptBridge
|
||||
*/
|
||||
export function setupJsBridge (callback = jsBridge => {}) {
|
||||
private initBridge (callback) {
|
||||
/**
|
||||
* You need check if inClashX first
|
||||
*/
|
||||
@ -82,10 +114,12 @@ export function setupJsBridge (callback = jsBridge => {}) {
|
||||
document.documentElement.appendChild(WVJBIframe)
|
||||
setTimeout(() => document.documentElement.removeChild(WVJBIframe), 0)
|
||||
}
|
||||
|
||||
/**
|
||||
* Check if perched in ClashX Runtime
|
||||
*/
|
||||
export function isClashX () {
|
||||
return navigator.userAgent === 'ClashX Runtime'
|
||||
}
|
||||
|
||||
export function setupJsBridge (callback) {
|
||||
if (jsBridge) {
|
||||
return callback(jsBridge)
|
||||
}
|
||||
|
||||
jsBridge = new JsBridge(callback)
|
||||
}
|
||||
|
Loading…
x
Reference in New Issue
Block a user