|
1 | | -"use strict"; |
2 | 1 | { |
3 | 2 | const createElement = document.createElement |
4 | 3 | document.createElement = function (tagName) { |
5 | 4 | const instance = createElement.call(document, tagName) |
6 | 5 | if (tagName === 'iframe') { |
7 | 6 | instance.setAttribute('nwdisable', 'true') |
| 7 | + } else if (tagName === 'webview') { |
| 8 | + Object.defineProperty(instance, 'instance', { |
| 9 | + set(value) { |
| 10 | + console.warn('webview instance set', value) |
| 11 | + this._instance = value |
| 12 | + if (value) { |
| 13 | + Object.defineProperty(value, '_type', { |
| 14 | + set(type) { |
| 15 | + console.warn('webview type set', type) |
| 16 | + this.__type = type |
| 17 | + if (type === 'skyline_appservice') { |
| 18 | + const client = require('skyline-addon/build/skyline.node') |
| 19 | + const setErrorMsg = (msg) => { |
| 20 | + console.error('skyline error', msg) |
| 21 | + const store = require('core.wxvpkg/b7691e109ad844af265d9385e5205802.js') |
| 22 | + if (msg?.includes('Not connected')) { |
| 23 | + msg = 'Skyline链接丢失,请检查服务状态' |
| 24 | + } else if (msg?.includes('Socket connection failed') || msg?.includes('Connection refused')) { |
| 25 | + msg = 'Skyline连接失败,请检查服务状态' |
| 26 | + } |
| 27 | + store.dispatch({ |
| 28 | + type: 'SIMULATOR_LAUNCH_ERROR', |
| 29 | + data: msg || 'Skyline出现异常,无法启动AppService,请检查Skyline是否正常运行', |
| 30 | + }) |
| 31 | + } |
| 32 | + try { |
| 33 | + client.Controller.connect() |
| 34 | + } catch (e) { |
| 35 | + console.error('connect skyline error', e) |
| 36 | + setErrorMsg(e?.message) |
| 37 | + throw e |
| 38 | + } |
| 39 | + const controller = new client.Controller(setErrorMsg) |
| 40 | + const webview = controller.webview |
| 41 | + Object.defineProperties(this, { |
| 42 | + src: { |
| 43 | + set(src) { |
| 44 | + console.warn('skyline_appservice webview set src', src) |
| 45 | + webview.src = src |
| 46 | + this.__webview__.src = 'about:blank' |
| 47 | + }, |
| 48 | + get() { |
| 49 | + console.warn('skyline_appservice webview get src', webview.src) |
| 50 | + return webview.src |
| 51 | + }, |
| 52 | + configurable: true, |
| 53 | + }, |
| 54 | + }) |
| 55 | + webview.showDevTools = async function (show, container) { |
| 56 | + try{ |
| 57 | + // 1. 获取列表 |
| 58 | + const resp = await fetch('http://127.0.0.1:9222/json/list') |
| 59 | + const list = await resp.json() |
| 60 | + // 2. 筛选 webview - appservice/mainframe |
| 61 | + const target = list.find(item => item.type === 'webview') |
| 62 | + // 3. 取到 webSocketDebuggerUrl |
| 63 | + const webSocketDebuggerUrl = target.webSocketDebuggerUrl.replace('ws://', '') |
| 64 | + // devtools://devtools/bundled/devtools_app.html?ws=127.0.0.1:9222/devtools/page/7601AF914FBACB429A289A8AC7AA82EB |
| 65 | + container.src = `devtools://devtools/bundled/devtools_app.html?ws=${webSocketDebuggerUrl}` |
| 66 | + // this.__webview__.showDevTools(show, container) |
| 67 | + } |
| 68 | + catch(e){ |
| 69 | + console.error('showDevTools error', e) |
| 70 | + require('core.wxvpkg/b7691e109ad844af265d9385e5205802.js').dispatch({ |
| 71 | + type: 'SIMULATOR_LAUNCH_ERROR', |
| 72 | + data: 'Skyline出现异常,无法启动AppService,请检查Skyline是否正常运行', |
| 73 | + }) |
| 74 | + } |
| 75 | + } |
| 76 | + this.getOriginElement = function () { |
| 77 | + return webview |
| 78 | + } |
| 79 | + this.setAttribute = function (name, value) { |
| 80 | + return webview.setAttribute(name, value) |
| 81 | + } |
| 82 | + { |
| 83 | + const mount = this.mount |
| 84 | + this.mount = function (e) { |
| 85 | + mount.apply(this, e) |
| 86 | + controller.mount() |
| 87 | + } |
| 88 | + } |
| 89 | + { |
| 90 | + const unmount = this.unmount |
| 91 | + this.unmount = function (e) { |
| 92 | + unmount.apply(this, e) |
| 93 | + controller.unmount() |
| 94 | + } |
| 95 | + } |
| 96 | + } |
| 97 | + }, |
| 98 | + get() { |
| 99 | + return this.__type |
| 100 | + }, |
| 101 | + configurable: true, |
| 102 | + }) |
| 103 | + } |
| 104 | + }, |
| 105 | + get() { |
| 106 | + return this._instance |
| 107 | + }, |
| 108 | + configurable: true, |
| 109 | + }) |
8 | 110 | } |
9 | 111 | return instance |
10 | 112 | } |
|
0 commit comments