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
|
export const ENABLED_WECHAT_JSSDK_API_LIST = [ 'getLocation', 'onMenuShareAppMessage', 'onMenuShareQQ', 'chooseImage', 'uploadImage', 'previewImage', 'hideOptionMenu', 'showOptionMenu', 'hideMenuItems', 'showMenuItems', 'hideAllNonBaseMenuItem', 'showAllNonBaseMenuItem', 'scanQRCode', ]
const _wx = window.wx
const HACK_RETRY_CALL_TAG = Symbol() const HACK_RETRY_COUNT_TAG = Symbol() const MAX_HACK_RETRY = 5
let hackPending: Promise<void> | null = null let hackPendingResolve: (() => void) | null = null
function resolveHackPending() { if (!hackPending) return hackPending = null hackPendingResolve?.() hackPendingResolve = null }
function hackWechat() { return new Promise((resolve) => { const iframe = document.createElement('iframe') iframe.src = window.location.href.split('#')[0] + '#hack-wechat-harmonyos' iframe.style.display = 'none'
window.addEventListener('message', function handler(event) { if (event.data?.type !== 'hackWechatHarmonyosReady') return
iframe.remove() window.removeEventListener('message', handler) setTimeout(resolve, 10) })
document.body.append(iframe) }) }
window.wx = new Proxy<any>( {}, { get(_, prop: any, receiver) { if (!ENABLED_WECHAT_JSSDK_API_LIST.includes(prop)) { return (_wx as any)[prop] }
return async (options: any) => { options ??= {} let skipComplete = false if (!options[HACK_RETRY_CALL_TAG]) { await hackPending }
const hackOptions = { ...options, cancel(...args: any[]) { if (options[HACK_RETRY_CALL_TAG]) { resolveHackPending() } options.cancel?.(...args) }, success(...args: any[]) { if (options[HACK_RETRY_CALL_TAG]) { resolveHackPending() } options.success?.(...args) }, async fail(...args: any[]) { const errMsg = args[0]?.errMsg if (errMsg?.includes('fail cancel')) { if (options[HACK_RETRY_CALL_TAG]) { resolveHackPending() } options.cancel?.(...args) return } if ( !errMsg.endsWith(':fail invalid signature') && !errMsg.endsWith(':permission denied') ) { if (options[HACK_RETRY_CALL_TAG]) { resolveHackPending() } options.fail?.(...args) return }
const retryCount = (options[HACK_RETRY_COUNT_TAG] as number | undefined) ?? 0 if (retryCount >= MAX_HACK_RETRY) { if (options[HACK_RETRY_CALL_TAG]) { resolveHackPending() } options.fail?.(...args) return }
if (hackPending && !options[HACK_RETRY_CALL_TAG]) { await hackPending receiver[prop](options) return }
skipComplete = true hackPending = new Promise((resolve) => { hackPendingResolve = resolve })
await hackWechat() receiver[prop]({ ...options, [HACK_RETRY_CALL_TAG]: true, [HACK_RETRY_COUNT_TAG]: retryCount + 1, }) }, complete(...args: any[]) { if (skipComplete) return options.complete?.(...args) }, }
;(_wx as any)[prop](hackOptions) } }, }, )
|