@onekeyfe/onekey-cross-webview 0.0.7-alpha.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/LICENSE +202 -0
- package/README.md +1 -0
- package/dist/DesktopWebView.d.ts +4 -0
- package/dist/DesktopWebView.js +136 -0
- package/dist/JsBridgeDesktopHost.d.ts +12 -0
- package/dist/JsBridgeDesktopHost.js +23 -0
- package/dist/JsBridgeNativeHost.d.ts +13 -0
- package/dist/JsBridgeNativeHost.js +17 -0
- package/dist/NativeWebView.d.ts +4 -0
- package/dist/NativeWebView.js +63 -0
- package/dist/cjs/DesktopWebView.js +158 -0
- package/dist/cjs/JsBridgeDesktopHost.js +26 -0
- package/dist/cjs/JsBridgeNativeHost.js +20 -0
- package/dist/cjs/NativeWebView.js +85 -0
- package/dist/cjs/index.js +18 -0
- package/dist/cjs/useIsIpcReady.js +33 -0
- package/dist/cjs/useWebViewBridge.js +25 -0
- package/dist/index.d.ts +6 -0
- package/dist/index.js +6 -0
- package/dist/useIsIpcReady.d.ts +1 -0
- package/dist/useIsIpcReady.js +29 -0
- package/dist/useWebViewBridge.d.ts +15 -0
- package/dist/useWebViewBridge.js +21 -0
- package/package.json +42 -0
package/LICENSE
ADDED
|
@@ -0,0 +1,202 @@
|
|
|
1
|
+
|
|
2
|
+
Apache License
|
|
3
|
+
Version 2.0, January 2004
|
|
4
|
+
http://www.apache.org/licenses/
|
|
5
|
+
|
|
6
|
+
TERMS AND CONDITIONS FOR USE, REPRODUCTION, AND DISTRIBUTION
|
|
7
|
+
|
|
8
|
+
1. Definitions.
|
|
9
|
+
|
|
10
|
+
"License" shall mean the terms and conditions for use, reproduction,
|
|
11
|
+
and distribution as defined by Sections 1 through 9 of this document.
|
|
12
|
+
|
|
13
|
+
"Licensor" shall mean the copyright owner or entity authorized by
|
|
14
|
+
the copyright owner that is granting the License.
|
|
15
|
+
|
|
16
|
+
"Legal Entity" shall mean the union of the acting entity and all
|
|
17
|
+
other entities that control, are controlled by, or are under common
|
|
18
|
+
control with that entity. For the purposes of this definition,
|
|
19
|
+
"control" means (i) the power, direct or indirect, to cause the
|
|
20
|
+
direction or management of such entity, whether by contract or
|
|
21
|
+
otherwise, or (ii) ownership of fifty percent (50%) or more of the
|
|
22
|
+
outstanding shares, or (iii) beneficial ownership of such entity.
|
|
23
|
+
|
|
24
|
+
"You" (or "Your") shall mean an individual or Legal Entity
|
|
25
|
+
exercising permissions granted by this License.
|
|
26
|
+
|
|
27
|
+
"Source" form shall mean the preferred form for making modifications,
|
|
28
|
+
including but not limited to software source code, documentation
|
|
29
|
+
source, and configuration files.
|
|
30
|
+
|
|
31
|
+
"Object" form shall mean any form resulting from mechanical
|
|
32
|
+
transformation or translation of a Source form, including but
|
|
33
|
+
not limited to compiled object code, generated documentation,
|
|
34
|
+
and conversions to other media types.
|
|
35
|
+
|
|
36
|
+
"Work" shall mean the work of authorship, whether in Source or
|
|
37
|
+
Object form, made available under the License, as indicated by a
|
|
38
|
+
copyright notice that is included in or attached to the work
|
|
39
|
+
(an example is provided in the Appendix below).
|
|
40
|
+
|
|
41
|
+
"Derivative Works" shall mean any work, whether in Source or Object
|
|
42
|
+
form, that is based on (or derived from) the Work and for which the
|
|
43
|
+
editorial revisions, annotations, elaborations, or other modifications
|
|
44
|
+
represent, as a whole, an original work of authorship. For the purposes
|
|
45
|
+
of this License, Derivative Works shall not include works that remain
|
|
46
|
+
separable from, or merely link (or bind by name) to the interfaces of,
|
|
47
|
+
the Work and Derivative Works thereof.
|
|
48
|
+
|
|
49
|
+
"Contribution" shall mean any work of authorship, including
|
|
50
|
+
the original version of the Work and any modifications or additions
|
|
51
|
+
to that Work or Derivative Works thereof, that is intentionally
|
|
52
|
+
submitted to Licensor for inclusion in the Work by the copyright owner
|
|
53
|
+
or by an individual or Legal Entity authorized to submit on behalf of
|
|
54
|
+
the copyright owner. For the purposes of this definition, "submitted"
|
|
55
|
+
means any form of electronic, verbal, or written communication sent
|
|
56
|
+
to the Licensor or its representatives, including but not limited to
|
|
57
|
+
communication on electronic mailing lists, source code control systems,
|
|
58
|
+
and issue tracking systems that are managed by, or on behalf of, the
|
|
59
|
+
Licensor for the purpose of discussing and improving the Work, but
|
|
60
|
+
excluding communication that is conspicuously marked or otherwise
|
|
61
|
+
designated in writing by the copyright owner as "Not a Contribution."
|
|
62
|
+
|
|
63
|
+
"Contributor" shall mean Licensor and any individual or Legal Entity
|
|
64
|
+
on behalf of whom a Contribution has been received by Licensor and
|
|
65
|
+
subsequently incorporated within the Work.
|
|
66
|
+
|
|
67
|
+
2. Grant of Copyright License. Subject to the terms and conditions of
|
|
68
|
+
this License, each Contributor hereby grants to You a perpetual,
|
|
69
|
+
worldwide, non-exclusive, no-charge, royalty-free, irrevocable
|
|
70
|
+
copyright license to reproduce, prepare Derivative Works of,
|
|
71
|
+
publicly display, publicly perform, sublicense, and distribute the
|
|
72
|
+
Work and such Derivative Works in Source or Object form.
|
|
73
|
+
|
|
74
|
+
3. Grant of Patent License. Subject to the terms and conditions of
|
|
75
|
+
this License, each Contributor hereby grants to You a perpetual,
|
|
76
|
+
worldwide, non-exclusive, no-charge, royalty-free, irrevocable
|
|
77
|
+
(except as stated in this section) patent license to make, have made,
|
|
78
|
+
use, offer to sell, sell, import, and otherwise transfer the Work,
|
|
79
|
+
where such license applies only to those patent claims licensable
|
|
80
|
+
by such Contributor that are necessarily infringed by their
|
|
81
|
+
Contribution(s) alone or by combination of their Contribution(s)
|
|
82
|
+
with the Work to which such Contribution(s) was submitted. If You
|
|
83
|
+
institute patent litigation against any entity (including a
|
|
84
|
+
cross-claim or counterclaim in a lawsuit) alleging that the Work
|
|
85
|
+
or a Contribution incorporated within the Work constitutes direct
|
|
86
|
+
or contributory patent infringement, then any patent licenses
|
|
87
|
+
granted to You under this License for that Work shall terminate
|
|
88
|
+
as of the date such litigation is filed.
|
|
89
|
+
|
|
90
|
+
4. Redistribution. You may reproduce and distribute copies of the
|
|
91
|
+
Work or Derivative Works thereof in any medium, with or without
|
|
92
|
+
modifications, and in Source or Object form, provided that You
|
|
93
|
+
meet the following conditions:
|
|
94
|
+
|
|
95
|
+
(a) You must give any other recipients of the Work or
|
|
96
|
+
Derivative Works a copy of this License; and
|
|
97
|
+
|
|
98
|
+
(b) You must cause any modified files to carry prominent notices
|
|
99
|
+
stating that You changed the files; and
|
|
100
|
+
|
|
101
|
+
(c) You must retain, in the Source form of any Derivative Works
|
|
102
|
+
that You distribute, all copyright, patent, trademark, and
|
|
103
|
+
attribution notices from the Source form of the Work,
|
|
104
|
+
excluding those notices that do not pertain to any part of
|
|
105
|
+
the Derivative Works; and
|
|
106
|
+
|
|
107
|
+
(d) If the Work includes a "NOTICE" text file as part of its
|
|
108
|
+
distribution, then any Derivative Works that You distribute must
|
|
109
|
+
include a readable copy of the attribution notices contained
|
|
110
|
+
within such NOTICE file, excluding those notices that do not
|
|
111
|
+
pertain to any part of the Derivative Works, in at least one
|
|
112
|
+
of the following places: within a NOTICE text file distributed
|
|
113
|
+
as part of the Derivative Works; within the Source form or
|
|
114
|
+
documentation, if provided along with the Derivative Works; or,
|
|
115
|
+
within a display generated by the Derivative Works, if and
|
|
116
|
+
wherever such third-party notices normally appear. The contents
|
|
117
|
+
of the NOTICE file are for informational purposes only and
|
|
118
|
+
do not modify the License. You may add Your own attribution
|
|
119
|
+
notices within Derivative Works that You distribute, alongside
|
|
120
|
+
or as an addendum to the NOTICE text from the Work, provided
|
|
121
|
+
that such additional attribution notices cannot be construed
|
|
122
|
+
as modifying the License.
|
|
123
|
+
|
|
124
|
+
You may add Your own copyright statement to Your modifications and
|
|
125
|
+
may provide additional or different license terms and conditions
|
|
126
|
+
for use, reproduction, or distribution of Your modifications, or
|
|
127
|
+
for any such Derivative Works as a whole, provided Your use,
|
|
128
|
+
reproduction, and distribution of the Work otherwise complies with
|
|
129
|
+
the conditions stated in this License.
|
|
130
|
+
|
|
131
|
+
5. Submission of Contributions. Unless You explicitly state otherwise,
|
|
132
|
+
any Contribution intentionally submitted for inclusion in the Work
|
|
133
|
+
by You to the Licensor shall be under the terms and conditions of
|
|
134
|
+
this License, without any additional terms or conditions.
|
|
135
|
+
Notwithstanding the above, nothing herein shall supersede or modify
|
|
136
|
+
the terms of any separate license agreement you may have executed
|
|
137
|
+
with Licensor regarding such Contributions.
|
|
138
|
+
|
|
139
|
+
6. Trademarks. This License does not grant permission to use the trade
|
|
140
|
+
names, trademarks, service marks, or product names of the Licensor,
|
|
141
|
+
except as required for reasonable and customary use in describing the
|
|
142
|
+
origin of the Work and reproducing the content of the NOTICE file.
|
|
143
|
+
|
|
144
|
+
7. Disclaimer of Warranty. Unless required by applicable law or
|
|
145
|
+
agreed to in writing, Licensor provides the Work (and each
|
|
146
|
+
Contributor provides its Contributions) on an "AS IS" BASIS,
|
|
147
|
+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or
|
|
148
|
+
implied, including, without limitation, any warranties or conditions
|
|
149
|
+
of TITLE, NON-INFRINGEMENT, MERCHANTABILITY, or FITNESS FOR A
|
|
150
|
+
PARTICULAR PURPOSE. You are solely responsible for determining the
|
|
151
|
+
appropriateness of using or redistributing the Work and assume any
|
|
152
|
+
risks associated with Your exercise of permissions under this License.
|
|
153
|
+
|
|
154
|
+
8. Limitation of Liability. In no event and under no legal theory,
|
|
155
|
+
whether in tort (including negligence), contract, or otherwise,
|
|
156
|
+
unless required by applicable law (such as deliberate and grossly
|
|
157
|
+
negligent acts) or agreed to in writing, shall any Contributor be
|
|
158
|
+
liable to You for damages, including any direct, indirect, special,
|
|
159
|
+
incidental, or consequential damages of any character arising as a
|
|
160
|
+
result of this License or out of the use or inability to use the
|
|
161
|
+
Work (including but not limited to damages for loss of goodwill,
|
|
162
|
+
work stoppage, computer failure or malfunction, or any and all
|
|
163
|
+
other commercial damages or losses), even if such Contributor
|
|
164
|
+
has been advised of the possibility of such damages.
|
|
165
|
+
|
|
166
|
+
9. Accepting Warranty or Additional Liability. While redistributing
|
|
167
|
+
the Work or Derivative Works thereof, You may choose to offer,
|
|
168
|
+
and charge a fee for, acceptance of support, warranty, indemnity,
|
|
169
|
+
or other liability obligations and/or rights consistent with this
|
|
170
|
+
License. However, in accepting such obligations, You may act only
|
|
171
|
+
on Your own behalf and on Your sole responsibility, not on behalf
|
|
172
|
+
of any other Contributor, and only if You agree to indemnify,
|
|
173
|
+
defend, and hold each Contributor harmless for any liability
|
|
174
|
+
incurred by, or claims asserted against, such Contributor by reason
|
|
175
|
+
of your accepting any such warranty or additional liability.
|
|
176
|
+
|
|
177
|
+
END OF TERMS AND CONDITIONS
|
|
178
|
+
|
|
179
|
+
APPENDIX: How to apply the Apache License to your work.
|
|
180
|
+
|
|
181
|
+
To apply the Apache License to your work, attach the following
|
|
182
|
+
boilerplate notice, with the fields enclosed by brackets "[]"
|
|
183
|
+
replaced with your own identifying information. (Don't include
|
|
184
|
+
the brackets!) The text should be enclosed in the appropriate
|
|
185
|
+
comment syntax for the file format. We also recommend that a
|
|
186
|
+
file or class name and description of purpose be included on the
|
|
187
|
+
same "printed page" as the copyright notice for easier
|
|
188
|
+
identification within third-party archives.
|
|
189
|
+
|
|
190
|
+
Copyright [yyyy] [name of copyright owner]
|
|
191
|
+
|
|
192
|
+
Licensed under the Apache License, Version 2.0 (the "License");
|
|
193
|
+
you may not use this file except in compliance with the License.
|
|
194
|
+
You may obtain a copy of the License at
|
|
195
|
+
|
|
196
|
+
http://www.apache.org/licenses/LICENSE-2.0
|
|
197
|
+
|
|
198
|
+
Unless required by applicable law or agreed to in writing, software
|
|
199
|
+
distributed under the License is distributed on an "AS IS" BASIS,
|
|
200
|
+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
201
|
+
See the License for the specific language governing permissions and
|
|
202
|
+
limitations under the License.
|
package/README.md
ADDED
|
@@ -0,0 +1 @@
|
|
|
1
|
+
# @onekeyfe/onekey-native-webview
|
|
@@ -0,0 +1,4 @@
|
|
|
1
|
+
import React from 'react';
|
|
2
|
+
import { InpageProviderWebViewProps } from '@onekeyfe/cross-inpage-provider-types';
|
|
3
|
+
declare const DesktopWebView: React.ForwardRefExoticComponent<Pick<React.ClassAttributes<HTMLWebViewElement> & React.WebViewHTMLAttributes<HTMLWebViewElement> & InpageProviderWebViewProps, "slot" | "style" | "title" | "src" | "receiveHandler" | "onSrcChange" | "key" | "allowFullScreen" | "allowpopups" | "autoFocus" | "autosize" | "blinkfeatures" | "disableblinkfeatures" | "disableguestresize" | "disablewebsecurity" | "guestinstance" | "httpreferrer" | "nodeintegration" | "partition" | "plugins" | "preload" | "useragent" | "webpreferences" | "defaultChecked" | "defaultValue" | "suppressContentEditableWarning" | "suppressHydrationWarning" | "accessKey" | "className" | "contentEditable" | "contextMenu" | "dir" | "draggable" | "hidden" | "id" | "lang" | "placeholder" | "spellCheck" | "tabIndex" | "translate" | "radioGroup" | "role" | "about" | "datatype" | "inlist" | "prefix" | "property" | "resource" | "typeof" | "vocab" | "autoCapitalize" | "autoCorrect" | "autoSave" | "color" | "itemProp" | "itemScope" | "itemType" | "itemID" | "itemRef" | "results" | "security" | "unselectable" | "inputMode" | "is" | "aria-activedescendant" | "aria-atomic" | "aria-autocomplete" | "aria-busy" | "aria-checked" | "aria-colcount" | "aria-colindex" | "aria-colspan" | "aria-controls" | "aria-current" | "aria-describedby" | "aria-details" | "aria-disabled" | "aria-dropeffect" | "aria-errormessage" | "aria-expanded" | "aria-flowto" | "aria-grabbed" | "aria-haspopup" | "aria-hidden" | "aria-invalid" | "aria-keyshortcuts" | "aria-label" | "aria-labelledby" | "aria-level" | "aria-live" | "aria-modal" | "aria-multiline" | "aria-multiselectable" | "aria-orientation" | "aria-owns" | "aria-placeholder" | "aria-posinset" | "aria-pressed" | "aria-readonly" | "aria-relevant" | "aria-required" | "aria-roledescription" | "aria-rowcount" | "aria-rowindex" | "aria-rowspan" | "aria-selected" | "aria-setsize" | "aria-sort" | "aria-valuemax" | "aria-valuemin" | "aria-valuenow" | "aria-valuetext" | "children" | "dangerouslySetInnerHTML" | "onCopy" | "onCopyCapture" | "onCut" | "onCutCapture" | "onPaste" | "onPasteCapture" | "onCompositionEnd" | "onCompositionEndCapture" | "onCompositionStart" | "onCompositionStartCapture" | "onCompositionUpdate" | "onCompositionUpdateCapture" | "onFocus" | "onFocusCapture" | "onBlur" | "onBlurCapture" | "onChange" | "onChangeCapture" | "onBeforeInput" | "onBeforeInputCapture" | "onInput" | "onInputCapture" | "onReset" | "onResetCapture" | "onSubmit" | "onSubmitCapture" | "onInvalid" | "onInvalidCapture" | "onLoad" | "onLoadCapture" | "onError" | "onErrorCapture" | "onKeyDown" | "onKeyDownCapture" | "onKeyPress" | "onKeyPressCapture" | "onKeyUp" | "onKeyUpCapture" | "onAbort" | "onAbortCapture" | "onCanPlay" | "onCanPlayCapture" | "onCanPlayThrough" | "onCanPlayThroughCapture" | "onDurationChange" | "onDurationChangeCapture" | "onEmptied" | "onEmptiedCapture" | "onEncrypted" | "onEncryptedCapture" | "onEnded" | "onEndedCapture" | "onLoadedData" | "onLoadedDataCapture" | "onLoadedMetadata" | "onLoadedMetadataCapture" | "onLoadStart" | "onLoadStartCapture" | "onPause" | "onPauseCapture" | "onPlay" | "onPlayCapture" | "onPlaying" | "onPlayingCapture" | "onProgress" | "onProgressCapture" | "onRateChange" | "onRateChangeCapture" | "onSeeked" | "onSeekedCapture" | "onSeeking" | "onSeekingCapture" | "onStalled" | "onStalledCapture" | "onSuspend" | "onSuspendCapture" | "onTimeUpdate" | "onTimeUpdateCapture" | "onVolumeChange" | "onVolumeChangeCapture" | "onWaiting" | "onWaitingCapture" | "onAuxClick" | "onAuxClickCapture" | "onClick" | "onClickCapture" | "onContextMenu" | "onContextMenuCapture" | "onDoubleClick" | "onDoubleClickCapture" | "onDrag" | "onDragCapture" | "onDragEnd" | "onDragEndCapture" | "onDragEnter" | "onDragEnterCapture" | "onDragExit" | "onDragExitCapture" | "onDragLeave" | "onDragLeaveCapture" | "onDragOver" | "onDragOverCapture" | "onDragStart" | "onDragStartCapture" | "onDrop" | "onDropCapture" | "onMouseDown" | "onMouseDownCapture" | "onMouseEnter" | "onMouseLeave" | "onMouseMove" | "onMouseMoveCapture" | "onMouseOut" | "onMouseOutCapture" | "onMouseOver" | "onMouseOverCapture" | "onMouseUp" | "onMouseUpCapture" | "onSelect" | "onSelectCapture" | "onTouchCancel" | "onTouchCancelCapture" | "onTouchEnd" | "onTouchEndCapture" | "onTouchMove" | "onTouchMoveCapture" | "onTouchStart" | "onTouchStartCapture" | "onPointerDown" | "onPointerDownCapture" | "onPointerMove" | "onPointerMoveCapture" | "onPointerUp" | "onPointerUpCapture" | "onPointerCancel" | "onPointerCancelCapture" | "onPointerEnter" | "onPointerEnterCapture" | "onPointerLeave" | "onPointerLeaveCapture" | "onPointerOver" | "onPointerOverCapture" | "onPointerOut" | "onPointerOutCapture" | "onGotPointerCapture" | "onGotPointerCaptureCapture" | "onLostPointerCapture" | "onLostPointerCaptureCapture" | "onScroll" | "onScrollCapture" | "onWheel" | "onWheelCapture" | "onAnimationStart" | "onAnimationStartCapture" | "onAnimationEnd" | "onAnimationEndCapture" | "onAnimationIteration" | "onAnimationIterationCapture" | "onTransitionEnd" | "onTransitionEndCapture"> & React.RefAttributes<unknown>>;
|
|
4
|
+
export { DesktopWebView };
|
|
@@ -0,0 +1,136 @@
|
|
|
1
|
+
var __rest = (this && this.__rest) || function (s, e) {
|
|
2
|
+
var t = {};
|
|
3
|
+
for (var p in s) if (Object.prototype.hasOwnProperty.call(s, p) && e.indexOf(p) < 0)
|
|
4
|
+
t[p] = s[p];
|
|
5
|
+
if (s != null && typeof Object.getOwnPropertySymbols === "function")
|
|
6
|
+
for (var i = 0, p = Object.getOwnPropertySymbols(s); i < p.length; i++) {
|
|
7
|
+
if (e.indexOf(p[i]) < 0 && Object.prototype.propertyIsEnumerable.call(s, p[i]))
|
|
8
|
+
t[p[i]] = s[p[i]];
|
|
9
|
+
}
|
|
10
|
+
return t;
|
|
11
|
+
};
|
|
12
|
+
/* eslint-disable @typescript-eslint/ban-ts-comment */
|
|
13
|
+
import React, { forwardRef, useCallback, useEffect, useImperativeHandle, useMemo, useRef, useState, } from 'react';
|
|
14
|
+
import { consts } from '@onekeyfe/cross-inpage-provider-core';
|
|
15
|
+
import { JsBridgeDesktopHost } from './JsBridgeDesktopHost';
|
|
16
|
+
import { useIsIpcReady } from './useIsIpcReady';
|
|
17
|
+
const isDev = process.env.NODE_ENV !== 'production';
|
|
18
|
+
const isBrowser = true;
|
|
19
|
+
function usePreloadJsUrl() {
|
|
20
|
+
var _a;
|
|
21
|
+
const { preloadJsUrl } = (_a = window.ONEKEY_DESKTOP_GLOBALS) !== null && _a !== void 0 ? _a : {};
|
|
22
|
+
useEffect(() => {
|
|
23
|
+
if (preloadJsUrl) {
|
|
24
|
+
return;
|
|
25
|
+
}
|
|
26
|
+
const timer = setTimeout(() => {
|
|
27
|
+
if (!preloadJsUrl) {
|
|
28
|
+
console.error(`Webview render failed:
|
|
29
|
+
Please send messages of channel SET_ONEKEY_DESKTOP_GLOBALS at app start
|
|
30
|
+
`);
|
|
31
|
+
}
|
|
32
|
+
}, 3000);
|
|
33
|
+
return () => clearTimeout(timer);
|
|
34
|
+
}, [preloadJsUrl]);
|
|
35
|
+
return preloadJsUrl;
|
|
36
|
+
}
|
|
37
|
+
// Used for webview type referencing
|
|
38
|
+
const WEBVIEW_TAG = 'webview';
|
|
39
|
+
const DesktopWebView = forwardRef((_a, ref) => {
|
|
40
|
+
var { src, receiveHandler, onSrcChange } = _a, props = __rest(_a, ["src", "receiveHandler", "onSrcChange"]);
|
|
41
|
+
const [isWebviewReady, setIsWebviewReady] = useState(false);
|
|
42
|
+
const webviewRef = useRef(null);
|
|
43
|
+
const isIpcReady = useIsIpcReady();
|
|
44
|
+
const [devToolsAtLeft, setDevToolsAtLeft] = useState(false);
|
|
45
|
+
useEffect(() => () => {
|
|
46
|
+
var _a;
|
|
47
|
+
// not working, ref is null after unmount
|
|
48
|
+
(_a = webviewRef.current) === null || _a === void 0 ? void 0 : _a.closeDevTools();
|
|
49
|
+
}, []);
|
|
50
|
+
// TODO extract to hooks
|
|
51
|
+
const jsBridge = useMemo(() => new JsBridgeDesktopHost({
|
|
52
|
+
webviewRef,
|
|
53
|
+
receiveHandler,
|
|
54
|
+
}), [receiveHandler]);
|
|
55
|
+
useImperativeHandle(ref, () => {
|
|
56
|
+
const wrapper = {
|
|
57
|
+
innerRef: webviewRef.current,
|
|
58
|
+
jsBridge,
|
|
59
|
+
reload: () => { var _a; return (_a = webviewRef.current) === null || _a === void 0 ? void 0 : _a.reload(); },
|
|
60
|
+
// eslint-disable-next-line @typescript-eslint/no-unused-vars
|
|
61
|
+
loadURL: (url, options) => {
|
|
62
|
+
if (onSrcChange) {
|
|
63
|
+
onSrcChange(url);
|
|
64
|
+
}
|
|
65
|
+
else {
|
|
66
|
+
console.warn('DesktopWebView: Please pass onSrcChange props to enable loadURL() working.');
|
|
67
|
+
}
|
|
68
|
+
// use onSrcChange props change src
|
|
69
|
+
// do not need call ElectronWebView.loadURL() manually.
|
|
70
|
+
// webviewRef.current?.loadURL(url);
|
|
71
|
+
},
|
|
72
|
+
};
|
|
73
|
+
jsBridge.webviewWrapper = wrapper;
|
|
74
|
+
return wrapper;
|
|
75
|
+
});
|
|
76
|
+
const initWebviewByRef = useCallback(($ref) => {
|
|
77
|
+
webviewRef.current = $ref;
|
|
78
|
+
// desktop "ipc-message" listener must be added after webviewReady
|
|
79
|
+
// so use ref to check it
|
|
80
|
+
setIsWebviewReady(true);
|
|
81
|
+
}, []);
|
|
82
|
+
useEffect(() => {
|
|
83
|
+
const webview = webviewRef.current;
|
|
84
|
+
if (!webview || !isIpcReady || !isWebviewReady) {
|
|
85
|
+
return;
|
|
86
|
+
}
|
|
87
|
+
const handleMessage = (event) => {
|
|
88
|
+
var _a;
|
|
89
|
+
if (event.channel === consts.JS_BRIDGE_MESSAGE_IPC_CHANNEL) {
|
|
90
|
+
const data = (_a = event === null || event === void 0 ? void 0 : event.args) === null || _a === void 0 ? void 0 : _a[0];
|
|
91
|
+
let origin = '';
|
|
92
|
+
const url = event.target.getURL();
|
|
93
|
+
// url initial value is empty after webview mounted
|
|
94
|
+
if (url) {
|
|
95
|
+
const uri = new URL(url);
|
|
96
|
+
origin = (uri === null || uri === void 0 ? void 0 : uri.origin) || '';
|
|
97
|
+
// - receive
|
|
98
|
+
jsBridge.receive(data, { origin });
|
|
99
|
+
}
|
|
100
|
+
}
|
|
101
|
+
// response back
|
|
102
|
+
// webview.send();
|
|
103
|
+
};
|
|
104
|
+
webview.addEventListener('ipc-message', handleMessage);
|
|
105
|
+
return () => {
|
|
106
|
+
webview.removeEventListener('ipc-message', handleMessage);
|
|
107
|
+
};
|
|
108
|
+
}, [jsBridge, isIpcReady, isWebviewReady]);
|
|
109
|
+
const preloadJsUrl = usePreloadJsUrl();
|
|
110
|
+
if (!preloadJsUrl) {
|
|
111
|
+
return null;
|
|
112
|
+
}
|
|
113
|
+
if (!isIpcReady) {
|
|
114
|
+
return null;
|
|
115
|
+
}
|
|
116
|
+
return (React.createElement(React.Fragment, null,
|
|
117
|
+
isDev && (React.createElement("button", { type: "button", style: {
|
|
118
|
+
fontSize: 12,
|
|
119
|
+
padding: 0,
|
|
120
|
+
opacity: 0.6,
|
|
121
|
+
position: 'absolute',
|
|
122
|
+
right: devToolsAtLeft ? undefined : 0,
|
|
123
|
+
left: devToolsAtLeft ? 0 : undefined,
|
|
124
|
+
}, onClick: () => {
|
|
125
|
+
var _a;
|
|
126
|
+
setDevToolsAtLeft(!devToolsAtLeft);
|
|
127
|
+
(_a = webviewRef.current) === null || _a === void 0 ? void 0 : _a.openDevTools();
|
|
128
|
+
} }, "DevTools")),
|
|
129
|
+
isBrowser && (React.createElement("webview", Object.assign({}, props, { ref: initWebviewByRef, preload: preloadJsUrl, src: src, style: { 'width': '100%', 'height': '100%' },
|
|
130
|
+
// @ts-ignore
|
|
131
|
+
allowpopups: "true",
|
|
132
|
+
// @ts-ignore
|
|
133
|
+
nodeintegration: "true", nodeintegrationinsubframes: "true", webpreferences: "contextIsolation=0, contextisolation=0, nativeWindowOpen=1" })))));
|
|
134
|
+
});
|
|
135
|
+
DesktopWebView.displayName = 'DesktopWebView';
|
|
136
|
+
export { DesktopWebView };
|
|
@@ -0,0 +1,12 @@
|
|
|
1
|
+
import React from 'react';
|
|
2
|
+
import { IElectronWebView, IJsBridgeConfig, IJsBridgeMessagePayload } from '@onekeyfe/cross-inpage-provider-types';
|
|
3
|
+
import { IWebViewWrapperRef } from './useWebViewBridge';
|
|
4
|
+
import { JsBridgeBase } from '@onekeyfe/cross-inpage-provider-core';
|
|
5
|
+
declare class JsBridgeDesktopHost extends JsBridgeBase {
|
|
6
|
+
constructor(config: IJsBridgeConfig);
|
|
7
|
+
sendAsString: boolean;
|
|
8
|
+
webviewRef?: React.RefObject<IElectronWebView>;
|
|
9
|
+
webviewWrapper?: IWebViewWrapperRef;
|
|
10
|
+
sendPayload(payload: IJsBridgeMessagePayload | string): void;
|
|
11
|
+
}
|
|
12
|
+
export { JsBridgeDesktopHost };
|
|
@@ -0,0 +1,23 @@
|
|
|
1
|
+
import { JsBridgeBase, injectedFactory, appDebugLogger, } from '@onekeyfe/cross-inpage-provider-core';
|
|
2
|
+
class JsBridgeDesktopHost extends JsBridgeBase {
|
|
3
|
+
constructor(config) {
|
|
4
|
+
super(config);
|
|
5
|
+
this.sendAsString = true;
|
|
6
|
+
this.webviewRef = config.webviewRef;
|
|
7
|
+
}
|
|
8
|
+
sendPayload(payload) {
|
|
9
|
+
var _a, _b;
|
|
10
|
+
if (this.webviewRef && this.webviewRef.current) {
|
|
11
|
+
const payloadStr = payload;
|
|
12
|
+
const inpageReceiveCode = injectedFactory.createCodeJsBridgeReceive(payloadStr);
|
|
13
|
+
if (this.webviewRef.current && this.webviewRef.current.executeJavaScript) {
|
|
14
|
+
appDebugLogger.webview('executeJavaScript', inpageReceiveCode, payload);
|
|
15
|
+
(_b = (_a = this.webviewRef.current) === null || _a === void 0 ? void 0 : _a.executeJavaScript) === null || _b === void 0 ? void 0 : _b.call(_a, inpageReceiveCode);
|
|
16
|
+
}
|
|
17
|
+
else {
|
|
18
|
+
throw new Error('JsBridgeDesktopHost executeJavaScript failed: webview ref not ready yet');
|
|
19
|
+
}
|
|
20
|
+
}
|
|
21
|
+
}
|
|
22
|
+
}
|
|
23
|
+
export { JsBridgeDesktopHost };
|
|
@@ -0,0 +1,13 @@
|
|
|
1
|
+
import React from 'react';
|
|
2
|
+
import { WebView } from 'react-native-webview';
|
|
3
|
+
import { JsBridgeBase } from '@onekeyfe/cross-inpage-provider-core';
|
|
4
|
+
import { IJsBridgeConfig, IJsBridgeMessagePayload } from '@onekeyfe/cross-inpage-provider-types';
|
|
5
|
+
import { IWebViewWrapperRef } from './useWebViewBridge';
|
|
6
|
+
declare class JsBridgeNativeHost extends JsBridgeBase {
|
|
7
|
+
constructor(config: IJsBridgeConfig);
|
|
8
|
+
sendAsString: boolean;
|
|
9
|
+
webviewRef?: React.RefObject<WebView>;
|
|
10
|
+
webviewWrapper?: IWebViewWrapperRef;
|
|
11
|
+
sendPayload(payload: IJsBridgeMessagePayload | string): void;
|
|
12
|
+
}
|
|
13
|
+
export { JsBridgeNativeHost };
|
|
@@ -0,0 +1,17 @@
|
|
|
1
|
+
import { injectedFactory, JsBridgeBase } from '@onekeyfe/cross-inpage-provider-core';
|
|
2
|
+
class JsBridgeNativeHost extends JsBridgeBase {
|
|
3
|
+
constructor(config) {
|
|
4
|
+
super(config);
|
|
5
|
+
this.sendAsString = true;
|
|
6
|
+
this.webviewRef = config.webviewRef;
|
|
7
|
+
}
|
|
8
|
+
sendPayload(payload) {
|
|
9
|
+
var _a, _b;
|
|
10
|
+
if (this.webviewRef && this.webviewRef.current) {
|
|
11
|
+
const payloadStr = payload;
|
|
12
|
+
const inpageReceiveCode = injectedFactory.createCodeJsBridgeReceive(payloadStr);
|
|
13
|
+
(_b = (_a = this.webviewRef.current) === null || _a === void 0 ? void 0 : _a.injectJavaScript) === null || _b === void 0 ? void 0 : _b.call(_a, inpageReceiveCode);
|
|
14
|
+
}
|
|
15
|
+
}
|
|
16
|
+
}
|
|
17
|
+
export { JsBridgeNativeHost };
|
|
@@ -0,0 +1,4 @@
|
|
|
1
|
+
import React from 'react';
|
|
2
|
+
import { InpageProviderWebViewProps } from '@onekeyfe/cross-inpage-provider-types';
|
|
3
|
+
declare const NativeWebView: React.ForwardRefExoticComponent<Pick<import("react-native-webview/lib/WebViewTypes").IOSWebViewProps & import("react-native-webview/lib/WebViewTypes").AndroidWebViewProps & InpageProviderWebViewProps, "source" | "src" | "receiveHandler" | "onSrcChange" | "onLoad" | "onError" | "onLoadStart" | "onScroll" | "onLoadProgress" | "injectedJavaScriptBeforeContentLoaded" | "incognito" | "bounces" | "decelerationRate" | "scrollEnabled" | "pagingEnabled" | "automaticallyAdjustContentInsets" | "automaticallyAdjustsScrollIndicatorInsets" | "contentInsetAdjustmentBehavior" | "contentInset" | "contentMode" | "dataDetectorTypes" | "allowsInlineMediaPlayback" | "hideKeyboardAccessoryView" | "allowsBackForwardNavigationGestures" | "useSharedProcessPool" | "userAgent" | "allowsLinkPreview" | "sharedCookiesEnabled" | "ignoreSilentHardwareSwitch" | "autoManageStatusBarEnabled" | "directionalLockEnabled" | "keyboardDisplayRequiresUserAction" | "allowingReadAccessToURL" | "allowFileAccessFromFileURLs" | "allowUniversalAccessFromFileURLs" | "onContentProcessDidTerminate" | "injectedJavaScriptForMainFrameOnly" | "injectedJavaScriptBeforeContentLoadedForMainFrameOnly" | "pullToRefreshEnabled" | "onFileDownload" | "limitsNavigationsToAppBoundDomains" | "textInteractionEnabled" | "mediaCapturePermissionGrantType" | "enableApplePay" | "menuItems" | "onCustomMenuSelection" | "javaScriptEnabled" | "javaScriptCanOpenWindowsAutomatically" | "containerStyle" | "renderError" | "renderLoading" | "onLoadEnd" | "onHttpError" | "onNavigationStateChange" | "onMessage" | "startInLoadingState" | "injectedJavaScript" | "showsHorizontalScrollIndicator" | "showsVerticalScrollIndicator" | "mediaPlaybackRequiresUserAction" | "originWhitelist" | "onShouldStartLoadWithRequest" | "nativeConfig" | "cacheEnabled" | "applicationNameForUserAgent" | "basicAuthCredential" | "onContentSizeChange" | "onRenderProcessGone" | "cacheMode" | "overScrollMode" | "scalesPageToFit" | "geolocationEnabled" | "allowFileAccess" | "saveFormDataDisabled" | "setSupportMultipleWindows" | "urlPrefixesForDefaultIntent" | "androidHardwareAccelerationDisabled" | "androidLayerType" | "thirdPartyCookiesEnabled" | "domStorageEnabled" | "textZoom" | "mixedContentMode" | "allowsFullscreenVideo" | "forceDarkOn" | "setBuiltInZoomControls" | "setDisplayZoomControls" | "nestedScrollEnabled" | "minimumFontSize"> & React.RefAttributes<unknown>>;
|
|
4
|
+
export { NativeWebView };
|
|
@@ -0,0 +1,63 @@
|
|
|
1
|
+
var __rest = (this && this.__rest) || function (s, e) {
|
|
2
|
+
var t = {};
|
|
3
|
+
for (var p in s) if (Object.prototype.hasOwnProperty.call(s, p) && e.indexOf(p) < 0)
|
|
4
|
+
t[p] = s[p];
|
|
5
|
+
if (s != null && typeof Object.getOwnPropertySymbols === "function")
|
|
6
|
+
for (var i = 0, p = Object.getOwnPropertySymbols(s); i < p.length; i++) {
|
|
7
|
+
if (e.indexOf(p[i]) < 0 && Object.prototype.propertyIsEnumerable.call(s, p[i]))
|
|
8
|
+
t[p[i]] = s[p[i]];
|
|
9
|
+
}
|
|
10
|
+
return t;
|
|
11
|
+
};
|
|
12
|
+
/* eslint-disable @typescript-eslint/ban-ts-comment */
|
|
13
|
+
import React, { forwardRef, useCallback, useEffect, useImperativeHandle, useMemo, useRef, } from 'react';
|
|
14
|
+
import { WebView } from 'react-native-webview';
|
|
15
|
+
import { appDebugLogger } from '@onekeyfe/cross-inpage-provider-core';
|
|
16
|
+
import { JsBridgeNativeHost } from './JsBridgeNativeHost';
|
|
17
|
+
const NativeWebView = forwardRef((_a, ref) => {
|
|
18
|
+
var { src, receiveHandler, onSrcChange, onLoadProgress, injectedJavaScriptBeforeContentLoaded } = _a, props = __rest(_a, ["src", "receiveHandler", "onSrcChange", "onLoadProgress", "injectedJavaScriptBeforeContentLoaded"]);
|
|
19
|
+
const webviewRef = useRef(null);
|
|
20
|
+
const jsBridge = useMemo(() => new JsBridgeNativeHost({
|
|
21
|
+
webviewRef,
|
|
22
|
+
receiveHandler,
|
|
23
|
+
}), [receiveHandler]);
|
|
24
|
+
const webviewOnMessage = useCallback((event) => {
|
|
25
|
+
// as WebViewMessageEvent
|
|
26
|
+
const event0 = event;
|
|
27
|
+
const { data } = event0.nativeEvent;
|
|
28
|
+
const uri = new URL(event0.nativeEvent.url);
|
|
29
|
+
const origin = (uri === null || uri === void 0 ? void 0 : uri.origin) || '';
|
|
30
|
+
appDebugLogger.webview('onMessage', origin, data);
|
|
31
|
+
// - receive
|
|
32
|
+
jsBridge.receive(data, { origin });
|
|
33
|
+
}, [jsBridge]);
|
|
34
|
+
useImperativeHandle(ref, () => {
|
|
35
|
+
const wrapper = {
|
|
36
|
+
innerRef: webviewRef.current,
|
|
37
|
+
jsBridge,
|
|
38
|
+
reload: () => { var _a; return (_a = webviewRef.current) === null || _a === void 0 ? void 0 : _a.reload(); },
|
|
39
|
+
loadURL: (url) => {
|
|
40
|
+
// ReactNativeWebview do not has method to loadURL
|
|
41
|
+
// so we need src props change it
|
|
42
|
+
if (onSrcChange) {
|
|
43
|
+
onSrcChange(url);
|
|
44
|
+
}
|
|
45
|
+
else {
|
|
46
|
+
console.warn('NativeWebView: Please pass onSrcChange props to enable loadURL() working.');
|
|
47
|
+
}
|
|
48
|
+
},
|
|
49
|
+
};
|
|
50
|
+
jsBridge.webviewWrapper = wrapper;
|
|
51
|
+
return wrapper;
|
|
52
|
+
});
|
|
53
|
+
useEffect(() => {
|
|
54
|
+
// console.log('NativeWebView injectedJavaScript \r\n', injectedNative);
|
|
55
|
+
}, []);
|
|
56
|
+
return (React.createElement(WebView, Object.assign({}, props, {
|
|
57
|
+
// @ts-ignore
|
|
58
|
+
style: { backgroundColor: 'transparent' }, onLoadProgress: onLoadProgress, ref: webviewRef,
|
|
59
|
+
// injectedJavaScript={injectedNative}
|
|
60
|
+
injectedJavaScriptBeforeContentLoaded: injectedJavaScriptBeforeContentLoaded || '', source: { uri: src }, onMessage: webviewOnMessage })));
|
|
61
|
+
});
|
|
62
|
+
NativeWebView.displayName = 'NativeWebView';
|
|
63
|
+
export { NativeWebView };
|
|
@@ -0,0 +1,158 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
|
|
3
|
+
if (k2 === undefined) k2 = k;
|
|
4
|
+
Object.defineProperty(o, k2, { enumerable: true, get: function() { return m[k]; } });
|
|
5
|
+
}) : (function(o, m, k, k2) {
|
|
6
|
+
if (k2 === undefined) k2 = k;
|
|
7
|
+
o[k2] = m[k];
|
|
8
|
+
}));
|
|
9
|
+
var __setModuleDefault = (this && this.__setModuleDefault) || (Object.create ? (function(o, v) {
|
|
10
|
+
Object.defineProperty(o, "default", { enumerable: true, value: v });
|
|
11
|
+
}) : function(o, v) {
|
|
12
|
+
o["default"] = v;
|
|
13
|
+
});
|
|
14
|
+
var __importStar = (this && this.__importStar) || function (mod) {
|
|
15
|
+
if (mod && mod.__esModule) return mod;
|
|
16
|
+
var result = {};
|
|
17
|
+
if (mod != null) for (var k in mod) if (k !== "default" && Object.prototype.hasOwnProperty.call(mod, k)) __createBinding(result, mod, k);
|
|
18
|
+
__setModuleDefault(result, mod);
|
|
19
|
+
return result;
|
|
20
|
+
};
|
|
21
|
+
var __rest = (this && this.__rest) || function (s, e) {
|
|
22
|
+
var t = {};
|
|
23
|
+
for (var p in s) if (Object.prototype.hasOwnProperty.call(s, p) && e.indexOf(p) < 0)
|
|
24
|
+
t[p] = s[p];
|
|
25
|
+
if (s != null && typeof Object.getOwnPropertySymbols === "function")
|
|
26
|
+
for (var i = 0, p = Object.getOwnPropertySymbols(s); i < p.length; i++) {
|
|
27
|
+
if (e.indexOf(p[i]) < 0 && Object.prototype.propertyIsEnumerable.call(s, p[i]))
|
|
28
|
+
t[p[i]] = s[p[i]];
|
|
29
|
+
}
|
|
30
|
+
return t;
|
|
31
|
+
};
|
|
32
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
33
|
+
exports.DesktopWebView = void 0;
|
|
34
|
+
/* eslint-disable @typescript-eslint/ban-ts-comment */
|
|
35
|
+
const react_1 = __importStar(require("react"));
|
|
36
|
+
const cross_inpage_provider_core_1 = require("@onekeyfe/cross-inpage-provider-core");
|
|
37
|
+
const JsBridgeDesktopHost_1 = require("./JsBridgeDesktopHost");
|
|
38
|
+
const useIsIpcReady_1 = require("./useIsIpcReady");
|
|
39
|
+
const isDev = process.env.NODE_ENV !== 'production';
|
|
40
|
+
const isBrowser = true;
|
|
41
|
+
function usePreloadJsUrl() {
|
|
42
|
+
var _a;
|
|
43
|
+
const { preloadJsUrl } = (_a = window.ONEKEY_DESKTOP_GLOBALS) !== null && _a !== void 0 ? _a : {};
|
|
44
|
+
(0, react_1.useEffect)(() => {
|
|
45
|
+
if (preloadJsUrl) {
|
|
46
|
+
return;
|
|
47
|
+
}
|
|
48
|
+
const timer = setTimeout(() => {
|
|
49
|
+
if (!preloadJsUrl) {
|
|
50
|
+
console.error(`Webview render failed:
|
|
51
|
+
Please send messages of channel SET_ONEKEY_DESKTOP_GLOBALS at app start
|
|
52
|
+
`);
|
|
53
|
+
}
|
|
54
|
+
}, 3000);
|
|
55
|
+
return () => clearTimeout(timer);
|
|
56
|
+
}, [preloadJsUrl]);
|
|
57
|
+
return preloadJsUrl;
|
|
58
|
+
}
|
|
59
|
+
// Used for webview type referencing
|
|
60
|
+
const WEBVIEW_TAG = 'webview';
|
|
61
|
+
const DesktopWebView = (0, react_1.forwardRef)((_a, ref) => {
|
|
62
|
+
var { src, receiveHandler, onSrcChange } = _a, props = __rest(_a, ["src", "receiveHandler", "onSrcChange"]);
|
|
63
|
+
const [isWebviewReady, setIsWebviewReady] = (0, react_1.useState)(false);
|
|
64
|
+
const webviewRef = (0, react_1.useRef)(null);
|
|
65
|
+
const isIpcReady = (0, useIsIpcReady_1.useIsIpcReady)();
|
|
66
|
+
const [devToolsAtLeft, setDevToolsAtLeft] = (0, react_1.useState)(false);
|
|
67
|
+
(0, react_1.useEffect)(() => () => {
|
|
68
|
+
var _a;
|
|
69
|
+
// not working, ref is null after unmount
|
|
70
|
+
(_a = webviewRef.current) === null || _a === void 0 ? void 0 : _a.closeDevTools();
|
|
71
|
+
}, []);
|
|
72
|
+
// TODO extract to hooks
|
|
73
|
+
const jsBridge = (0, react_1.useMemo)(() => new JsBridgeDesktopHost_1.JsBridgeDesktopHost({
|
|
74
|
+
webviewRef,
|
|
75
|
+
receiveHandler,
|
|
76
|
+
}), [receiveHandler]);
|
|
77
|
+
(0, react_1.useImperativeHandle)(ref, () => {
|
|
78
|
+
const wrapper = {
|
|
79
|
+
innerRef: webviewRef.current,
|
|
80
|
+
jsBridge,
|
|
81
|
+
reload: () => { var _a; return (_a = webviewRef.current) === null || _a === void 0 ? void 0 : _a.reload(); },
|
|
82
|
+
// eslint-disable-next-line @typescript-eslint/no-unused-vars
|
|
83
|
+
loadURL: (url, options) => {
|
|
84
|
+
if (onSrcChange) {
|
|
85
|
+
onSrcChange(url);
|
|
86
|
+
}
|
|
87
|
+
else {
|
|
88
|
+
console.warn('DesktopWebView: Please pass onSrcChange props to enable loadURL() working.');
|
|
89
|
+
}
|
|
90
|
+
// use onSrcChange props change src
|
|
91
|
+
// do not need call ElectronWebView.loadURL() manually.
|
|
92
|
+
// webviewRef.current?.loadURL(url);
|
|
93
|
+
},
|
|
94
|
+
};
|
|
95
|
+
jsBridge.webviewWrapper = wrapper;
|
|
96
|
+
return wrapper;
|
|
97
|
+
});
|
|
98
|
+
const initWebviewByRef = (0, react_1.useCallback)(($ref) => {
|
|
99
|
+
webviewRef.current = $ref;
|
|
100
|
+
// desktop "ipc-message" listener must be added after webviewReady
|
|
101
|
+
// so use ref to check it
|
|
102
|
+
setIsWebviewReady(true);
|
|
103
|
+
}, []);
|
|
104
|
+
(0, react_1.useEffect)(() => {
|
|
105
|
+
const webview = webviewRef.current;
|
|
106
|
+
if (!webview || !isIpcReady || !isWebviewReady) {
|
|
107
|
+
return;
|
|
108
|
+
}
|
|
109
|
+
const handleMessage = (event) => {
|
|
110
|
+
var _a;
|
|
111
|
+
if (event.channel === cross_inpage_provider_core_1.consts.JS_BRIDGE_MESSAGE_IPC_CHANNEL) {
|
|
112
|
+
const data = (_a = event === null || event === void 0 ? void 0 : event.args) === null || _a === void 0 ? void 0 : _a[0];
|
|
113
|
+
let origin = '';
|
|
114
|
+
const url = event.target.getURL();
|
|
115
|
+
// url initial value is empty after webview mounted
|
|
116
|
+
if (url) {
|
|
117
|
+
const uri = new URL(url);
|
|
118
|
+
origin = (uri === null || uri === void 0 ? void 0 : uri.origin) || '';
|
|
119
|
+
// - receive
|
|
120
|
+
jsBridge.receive(data, { origin });
|
|
121
|
+
}
|
|
122
|
+
}
|
|
123
|
+
// response back
|
|
124
|
+
// webview.send();
|
|
125
|
+
};
|
|
126
|
+
webview.addEventListener('ipc-message', handleMessage);
|
|
127
|
+
return () => {
|
|
128
|
+
webview.removeEventListener('ipc-message', handleMessage);
|
|
129
|
+
};
|
|
130
|
+
}, [jsBridge, isIpcReady, isWebviewReady]);
|
|
131
|
+
const preloadJsUrl = usePreloadJsUrl();
|
|
132
|
+
if (!preloadJsUrl) {
|
|
133
|
+
return null;
|
|
134
|
+
}
|
|
135
|
+
if (!isIpcReady) {
|
|
136
|
+
return null;
|
|
137
|
+
}
|
|
138
|
+
return (react_1.default.createElement(react_1.default.Fragment, null,
|
|
139
|
+
isDev && (react_1.default.createElement("button", { type: "button", style: {
|
|
140
|
+
fontSize: 12,
|
|
141
|
+
padding: 0,
|
|
142
|
+
opacity: 0.6,
|
|
143
|
+
position: 'absolute',
|
|
144
|
+
right: devToolsAtLeft ? undefined : 0,
|
|
145
|
+
left: devToolsAtLeft ? 0 : undefined,
|
|
146
|
+
}, onClick: () => {
|
|
147
|
+
var _a;
|
|
148
|
+
setDevToolsAtLeft(!devToolsAtLeft);
|
|
149
|
+
(_a = webviewRef.current) === null || _a === void 0 ? void 0 : _a.openDevTools();
|
|
150
|
+
} }, "DevTools")),
|
|
151
|
+
isBrowser && (react_1.default.createElement("webview", Object.assign({}, props, { ref: initWebviewByRef, preload: preloadJsUrl, src: src, style: { 'width': '100%', 'height': '100%' },
|
|
152
|
+
// @ts-ignore
|
|
153
|
+
allowpopups: "true",
|
|
154
|
+
// @ts-ignore
|
|
155
|
+
nodeintegration: "true", nodeintegrationinsubframes: "true", webpreferences: "contextIsolation=0, contextisolation=0, nativeWindowOpen=1" })))));
|
|
156
|
+
});
|
|
157
|
+
exports.DesktopWebView = DesktopWebView;
|
|
158
|
+
DesktopWebView.displayName = 'DesktopWebView';
|
|
@@ -0,0 +1,26 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.JsBridgeDesktopHost = void 0;
|
|
4
|
+
const cross_inpage_provider_core_1 = require("@onekeyfe/cross-inpage-provider-core");
|
|
5
|
+
class JsBridgeDesktopHost extends cross_inpage_provider_core_1.JsBridgeBase {
|
|
6
|
+
constructor(config) {
|
|
7
|
+
super(config);
|
|
8
|
+
this.sendAsString = true;
|
|
9
|
+
this.webviewRef = config.webviewRef;
|
|
10
|
+
}
|
|
11
|
+
sendPayload(payload) {
|
|
12
|
+
var _a, _b;
|
|
13
|
+
if (this.webviewRef && this.webviewRef.current) {
|
|
14
|
+
const payloadStr = payload;
|
|
15
|
+
const inpageReceiveCode = cross_inpage_provider_core_1.injectedFactory.createCodeJsBridgeReceive(payloadStr);
|
|
16
|
+
if (this.webviewRef.current && this.webviewRef.current.executeJavaScript) {
|
|
17
|
+
cross_inpage_provider_core_1.appDebugLogger.webview('executeJavaScript', inpageReceiveCode, payload);
|
|
18
|
+
(_b = (_a = this.webviewRef.current) === null || _a === void 0 ? void 0 : _a.executeJavaScript) === null || _b === void 0 ? void 0 : _b.call(_a, inpageReceiveCode);
|
|
19
|
+
}
|
|
20
|
+
else {
|
|
21
|
+
throw new Error('JsBridgeDesktopHost executeJavaScript failed: webview ref not ready yet');
|
|
22
|
+
}
|
|
23
|
+
}
|
|
24
|
+
}
|
|
25
|
+
}
|
|
26
|
+
exports.JsBridgeDesktopHost = JsBridgeDesktopHost;
|
|
@@ -0,0 +1,20 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.JsBridgeNativeHost = void 0;
|
|
4
|
+
const cross_inpage_provider_core_1 = require("@onekeyfe/cross-inpage-provider-core");
|
|
5
|
+
class JsBridgeNativeHost extends cross_inpage_provider_core_1.JsBridgeBase {
|
|
6
|
+
constructor(config) {
|
|
7
|
+
super(config);
|
|
8
|
+
this.sendAsString = true;
|
|
9
|
+
this.webviewRef = config.webviewRef;
|
|
10
|
+
}
|
|
11
|
+
sendPayload(payload) {
|
|
12
|
+
var _a, _b;
|
|
13
|
+
if (this.webviewRef && this.webviewRef.current) {
|
|
14
|
+
const payloadStr = payload;
|
|
15
|
+
const inpageReceiveCode = cross_inpage_provider_core_1.injectedFactory.createCodeJsBridgeReceive(payloadStr);
|
|
16
|
+
(_b = (_a = this.webviewRef.current) === null || _a === void 0 ? void 0 : _a.injectJavaScript) === null || _b === void 0 ? void 0 : _b.call(_a, inpageReceiveCode);
|
|
17
|
+
}
|
|
18
|
+
}
|
|
19
|
+
}
|
|
20
|
+
exports.JsBridgeNativeHost = JsBridgeNativeHost;
|
|
@@ -0,0 +1,85 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
|
|
3
|
+
if (k2 === undefined) k2 = k;
|
|
4
|
+
Object.defineProperty(o, k2, { enumerable: true, get: function() { return m[k]; } });
|
|
5
|
+
}) : (function(o, m, k, k2) {
|
|
6
|
+
if (k2 === undefined) k2 = k;
|
|
7
|
+
o[k2] = m[k];
|
|
8
|
+
}));
|
|
9
|
+
var __setModuleDefault = (this && this.__setModuleDefault) || (Object.create ? (function(o, v) {
|
|
10
|
+
Object.defineProperty(o, "default", { enumerable: true, value: v });
|
|
11
|
+
}) : function(o, v) {
|
|
12
|
+
o["default"] = v;
|
|
13
|
+
});
|
|
14
|
+
var __importStar = (this && this.__importStar) || function (mod) {
|
|
15
|
+
if (mod && mod.__esModule) return mod;
|
|
16
|
+
var result = {};
|
|
17
|
+
if (mod != null) for (var k in mod) if (k !== "default" && Object.prototype.hasOwnProperty.call(mod, k)) __createBinding(result, mod, k);
|
|
18
|
+
__setModuleDefault(result, mod);
|
|
19
|
+
return result;
|
|
20
|
+
};
|
|
21
|
+
var __rest = (this && this.__rest) || function (s, e) {
|
|
22
|
+
var t = {};
|
|
23
|
+
for (var p in s) if (Object.prototype.hasOwnProperty.call(s, p) && e.indexOf(p) < 0)
|
|
24
|
+
t[p] = s[p];
|
|
25
|
+
if (s != null && typeof Object.getOwnPropertySymbols === "function")
|
|
26
|
+
for (var i = 0, p = Object.getOwnPropertySymbols(s); i < p.length; i++) {
|
|
27
|
+
if (e.indexOf(p[i]) < 0 && Object.prototype.propertyIsEnumerable.call(s, p[i]))
|
|
28
|
+
t[p[i]] = s[p[i]];
|
|
29
|
+
}
|
|
30
|
+
return t;
|
|
31
|
+
};
|
|
32
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
33
|
+
exports.NativeWebView = void 0;
|
|
34
|
+
/* eslint-disable @typescript-eslint/ban-ts-comment */
|
|
35
|
+
const react_1 = __importStar(require("react"));
|
|
36
|
+
const react_native_webview_1 = require("react-native-webview");
|
|
37
|
+
const cross_inpage_provider_core_1 = require("@onekeyfe/cross-inpage-provider-core");
|
|
38
|
+
const JsBridgeNativeHost_1 = require("./JsBridgeNativeHost");
|
|
39
|
+
const NativeWebView = (0, react_1.forwardRef)((_a, ref) => {
|
|
40
|
+
var { src, receiveHandler, onSrcChange, onLoadProgress, injectedJavaScriptBeforeContentLoaded } = _a, props = __rest(_a, ["src", "receiveHandler", "onSrcChange", "onLoadProgress", "injectedJavaScriptBeforeContentLoaded"]);
|
|
41
|
+
const webviewRef = (0, react_1.useRef)(null);
|
|
42
|
+
const jsBridge = (0, react_1.useMemo)(() => new JsBridgeNativeHost_1.JsBridgeNativeHost({
|
|
43
|
+
webviewRef,
|
|
44
|
+
receiveHandler,
|
|
45
|
+
}), [receiveHandler]);
|
|
46
|
+
const webviewOnMessage = (0, react_1.useCallback)((event) => {
|
|
47
|
+
// as WebViewMessageEvent
|
|
48
|
+
const event0 = event;
|
|
49
|
+
const { data } = event0.nativeEvent;
|
|
50
|
+
const uri = new URL(event0.nativeEvent.url);
|
|
51
|
+
const origin = (uri === null || uri === void 0 ? void 0 : uri.origin) || '';
|
|
52
|
+
cross_inpage_provider_core_1.appDebugLogger.webview('onMessage', origin, data);
|
|
53
|
+
// - receive
|
|
54
|
+
jsBridge.receive(data, { origin });
|
|
55
|
+
}, [jsBridge]);
|
|
56
|
+
(0, react_1.useImperativeHandle)(ref, () => {
|
|
57
|
+
const wrapper = {
|
|
58
|
+
innerRef: webviewRef.current,
|
|
59
|
+
jsBridge,
|
|
60
|
+
reload: () => { var _a; return (_a = webviewRef.current) === null || _a === void 0 ? void 0 : _a.reload(); },
|
|
61
|
+
loadURL: (url) => {
|
|
62
|
+
// ReactNativeWebview do not has method to loadURL
|
|
63
|
+
// so we need src props change it
|
|
64
|
+
if (onSrcChange) {
|
|
65
|
+
onSrcChange(url);
|
|
66
|
+
}
|
|
67
|
+
else {
|
|
68
|
+
console.warn('NativeWebView: Please pass onSrcChange props to enable loadURL() working.');
|
|
69
|
+
}
|
|
70
|
+
},
|
|
71
|
+
};
|
|
72
|
+
jsBridge.webviewWrapper = wrapper;
|
|
73
|
+
return wrapper;
|
|
74
|
+
});
|
|
75
|
+
(0, react_1.useEffect)(() => {
|
|
76
|
+
// console.log('NativeWebView injectedJavaScript \r\n', injectedNative);
|
|
77
|
+
}, []);
|
|
78
|
+
return (react_1.default.createElement(react_native_webview_1.WebView, Object.assign({}, props, {
|
|
79
|
+
// @ts-ignore
|
|
80
|
+
style: { backgroundColor: 'transparent' }, onLoadProgress: onLoadProgress, ref: webviewRef,
|
|
81
|
+
// injectedJavaScript={injectedNative}
|
|
82
|
+
injectedJavaScriptBeforeContentLoaded: injectedJavaScriptBeforeContentLoaded || '', source: { uri: src }, onMessage: webviewOnMessage })));
|
|
83
|
+
});
|
|
84
|
+
exports.NativeWebView = NativeWebView;
|
|
85
|
+
NativeWebView.displayName = 'NativeWebView';
|
|
@@ -0,0 +1,18 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
|
|
3
|
+
if (k2 === undefined) k2 = k;
|
|
4
|
+
Object.defineProperty(o, k2, { enumerable: true, get: function() { return m[k]; } });
|
|
5
|
+
}) : (function(o, m, k, k2) {
|
|
6
|
+
if (k2 === undefined) k2 = k;
|
|
7
|
+
o[k2] = m[k];
|
|
8
|
+
}));
|
|
9
|
+
var __exportStar = (this && this.__exportStar) || function(m, exports) {
|
|
10
|
+
for (var p in m) if (p !== "default" && !Object.prototype.hasOwnProperty.call(exports, p)) __createBinding(exports, m, p);
|
|
11
|
+
};
|
|
12
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
13
|
+
__exportStar(require("./DesktopWebView"), exports);
|
|
14
|
+
__exportStar(require("./JsBridgeDesktopHost"), exports);
|
|
15
|
+
__exportStar(require("./JsBridgeNativeHost"), exports);
|
|
16
|
+
__exportStar(require("./NativeWebView"), exports);
|
|
17
|
+
__exportStar(require("./useIsIpcReady"), exports);
|
|
18
|
+
__exportStar(require("./useWebViewBridge"), exports);
|
|
@@ -0,0 +1,33 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.useIsIpcReady = void 0;
|
|
4
|
+
const react_1 = require("react");
|
|
5
|
+
function useIsIpcReady() {
|
|
6
|
+
const [isIpcReady, setIsIpcReady] = (0, react_1.useState)(false);
|
|
7
|
+
const checkReady = (0, react_1.useCallback)(() => {
|
|
8
|
+
var _a;
|
|
9
|
+
const isBridgeInjected = Boolean((_a = window === null || window === void 0 ? void 0 : window.ONEKEY_DESKTOP_GLOBALS) === null || _a === void 0 ? void 0 : _a.preloadJsUrl);
|
|
10
|
+
if (isBridgeInjected) {
|
|
11
|
+
setIsIpcReady(true);
|
|
12
|
+
}
|
|
13
|
+
else {
|
|
14
|
+
setTimeout(checkReady, 100);
|
|
15
|
+
}
|
|
16
|
+
}, []);
|
|
17
|
+
(0, react_1.useEffect)(() => {
|
|
18
|
+
checkReady();
|
|
19
|
+
}, [checkReady]);
|
|
20
|
+
(0, react_1.useEffect)(() => {
|
|
21
|
+
if (isIpcReady) {
|
|
22
|
+
return;
|
|
23
|
+
}
|
|
24
|
+
const timer = setTimeout(() => {
|
|
25
|
+
if (!isIpcReady) {
|
|
26
|
+
console.error('electron ipc not ready yet, can not render <webview>, do you forget set parameter [preload] at new BrowserWindow() in app.ts ?');
|
|
27
|
+
}
|
|
28
|
+
}, 3000);
|
|
29
|
+
return () => clearTimeout(timer);
|
|
30
|
+
}, [isIpcReady]);
|
|
31
|
+
return isIpcReady;
|
|
32
|
+
}
|
|
33
|
+
exports.useIsIpcReady = useIsIpcReady;
|
|
@@ -0,0 +1,25 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.useWebViewBridge = void 0;
|
|
4
|
+
const react_1 = require("react");
|
|
5
|
+
function useWebViewBridge() {
|
|
6
|
+
const webviewRef = (0, react_1.useRef)(null);
|
|
7
|
+
const [jsBridge, setJsBridge] = (0, react_1.useState)(null);
|
|
8
|
+
// web3 provider
|
|
9
|
+
// const [provider, setProvider] = useState(null);
|
|
10
|
+
const setWebViewRef = (ref) => {
|
|
11
|
+
var _a;
|
|
12
|
+
webviewRef.current = ref;
|
|
13
|
+
const newJsBridge = (_a = webviewRef.current) === null || _a === void 0 ? void 0 : _a.jsBridge;
|
|
14
|
+
if (newJsBridge) {
|
|
15
|
+
setJsBridge(newJsBridge);
|
|
16
|
+
}
|
|
17
|
+
};
|
|
18
|
+
return {
|
|
19
|
+
jsBridge,
|
|
20
|
+
// provider,
|
|
21
|
+
webviewRef,
|
|
22
|
+
setWebViewRef,
|
|
23
|
+
};
|
|
24
|
+
}
|
|
25
|
+
exports.useWebViewBridge = useWebViewBridge;
|
package/dist/index.d.ts
ADDED
package/dist/index.js
ADDED
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export declare function useIsIpcReady(): boolean;
|
|
@@ -0,0 +1,29 @@
|
|
|
1
|
+
import { useCallback, useEffect, useState } from 'react';
|
|
2
|
+
export function useIsIpcReady() {
|
|
3
|
+
const [isIpcReady, setIsIpcReady] = useState(false);
|
|
4
|
+
const checkReady = useCallback(() => {
|
|
5
|
+
var _a;
|
|
6
|
+
const isBridgeInjected = Boolean((_a = window === null || window === void 0 ? void 0 : window.ONEKEY_DESKTOP_GLOBALS) === null || _a === void 0 ? void 0 : _a.preloadJsUrl);
|
|
7
|
+
if (isBridgeInjected) {
|
|
8
|
+
setIsIpcReady(true);
|
|
9
|
+
}
|
|
10
|
+
else {
|
|
11
|
+
setTimeout(checkReady, 100);
|
|
12
|
+
}
|
|
13
|
+
}, []);
|
|
14
|
+
useEffect(() => {
|
|
15
|
+
checkReady();
|
|
16
|
+
}, [checkReady]);
|
|
17
|
+
useEffect(() => {
|
|
18
|
+
if (isIpcReady) {
|
|
19
|
+
return;
|
|
20
|
+
}
|
|
21
|
+
const timer = setTimeout(() => {
|
|
22
|
+
if (!isIpcReady) {
|
|
23
|
+
console.error('electron ipc not ready yet, can not render <webview>, do you forget set parameter [preload] at new BrowserWindow() in app.ts ?');
|
|
24
|
+
}
|
|
25
|
+
}, 3000);
|
|
26
|
+
return () => clearTimeout(timer);
|
|
27
|
+
}, [isIpcReady]);
|
|
28
|
+
return isIpcReady;
|
|
29
|
+
}
|
|
@@ -0,0 +1,15 @@
|
|
|
1
|
+
/// <reference types="react" />
|
|
2
|
+
import { WebView as ReactNativeWebView } from 'react-native-webview';
|
|
3
|
+
import { JsBridgeBase } from '@onekeyfe/cross-inpage-provider-core';
|
|
4
|
+
import { IElectronWebView } from '@onekeyfe/cross-inpage-provider-types';
|
|
5
|
+
export declare type IWebViewWrapperRef = {
|
|
6
|
+
innerRef?: ReactNativeWebView | IElectronWebView | null;
|
|
7
|
+
jsBridge?: JsBridgeBase | null;
|
|
8
|
+
reload: () => void;
|
|
9
|
+
loadURL: (...args: any) => void;
|
|
10
|
+
};
|
|
11
|
+
export declare function useWebViewBridge(): {
|
|
12
|
+
jsBridge: JsBridgeBase | null;
|
|
13
|
+
webviewRef: import("react").MutableRefObject<IWebViewWrapperRef | null>;
|
|
14
|
+
setWebViewRef: (ref: IWebViewWrapperRef) => void;
|
|
15
|
+
};
|
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
import { useRef, useState } from 'react';
|
|
2
|
+
export function useWebViewBridge() {
|
|
3
|
+
const webviewRef = useRef(null);
|
|
4
|
+
const [jsBridge, setJsBridge] = useState(null);
|
|
5
|
+
// web3 provider
|
|
6
|
+
// const [provider, setProvider] = useState(null);
|
|
7
|
+
const setWebViewRef = (ref) => {
|
|
8
|
+
var _a;
|
|
9
|
+
webviewRef.current = ref;
|
|
10
|
+
const newJsBridge = (_a = webviewRef.current) === null || _a === void 0 ? void 0 : _a.jsBridge;
|
|
11
|
+
if (newJsBridge) {
|
|
12
|
+
setJsBridge(newJsBridge);
|
|
13
|
+
}
|
|
14
|
+
};
|
|
15
|
+
return {
|
|
16
|
+
jsBridge,
|
|
17
|
+
// provider,
|
|
18
|
+
webviewRef,
|
|
19
|
+
setWebViewRef,
|
|
20
|
+
};
|
|
21
|
+
}
|
package/package.json
ADDED
|
@@ -0,0 +1,42 @@
|
|
|
1
|
+
{
|
|
2
|
+
"name": "@onekeyfe/onekey-cross-webview",
|
|
3
|
+
"version": "0.0.7-alpha.0",
|
|
4
|
+
"keywords": [
|
|
5
|
+
"cross-inpage-provider"
|
|
6
|
+
],
|
|
7
|
+
"author": "dev-fe@onekey.so",
|
|
8
|
+
"repository": "https://github.com/OneKeyHQ/cross-inpage-provider",
|
|
9
|
+
"license": "Apache-2.0",
|
|
10
|
+
"publishConfig": {
|
|
11
|
+
"access": "public"
|
|
12
|
+
},
|
|
13
|
+
"type": "module",
|
|
14
|
+
"files": [
|
|
15
|
+
"dist/*"
|
|
16
|
+
],
|
|
17
|
+
"exports": {
|
|
18
|
+
"types": "./dist/index.d.ts",
|
|
19
|
+
"import": "./dist/index.js",
|
|
20
|
+
"require": "./dist/cjs/index.js"
|
|
21
|
+
},
|
|
22
|
+
"types": "./dist/index.d.ts",
|
|
23
|
+
"module": "./dist/index.js",
|
|
24
|
+
"main": "./dist/cjs/index.js",
|
|
25
|
+
"scripts": {
|
|
26
|
+
"prebuild": "rm -rf dist",
|
|
27
|
+
"build": "tsc && tsc --project tsconfig.cjs.json",
|
|
28
|
+
"start": "tsc --watch"
|
|
29
|
+
},
|
|
30
|
+
"peerDependencies": {
|
|
31
|
+
"electron": "*"
|
|
32
|
+
},
|
|
33
|
+
"devDependencies": {
|
|
34
|
+
"electron": "^17.1.0"
|
|
35
|
+
},
|
|
36
|
+
"dependencies": {
|
|
37
|
+
"@onekeyfe/cross-inpage-provider-core": "^0.0.7-alpha.0",
|
|
38
|
+
"@onekeyfe/cross-inpage-provider-types": "^0.0.7-alpha.0",
|
|
39
|
+
"react-native-webview": "^11.17.2"
|
|
40
|
+
},
|
|
41
|
+
"gitHead": "e163439202ef5f85319d7dc9bc7c18ee7d6a0b7b"
|
|
42
|
+
}
|