@onekeyfe/extension-bridge-injected 0.0.1 → 0.0.2
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.
|
@@ -1,10 +1,10 @@
|
|
|
1
1
|
import { IJsBridgeConfig, IJsBridgeMessagePayload } from '@onekeyfe/cross-inpage-provider-types';
|
|
2
2
|
import { JsBridgeBase } from '@onekeyfe/cross-inpage-provider-core';
|
|
3
|
+
declare function getOrCreateExtInjectedJsBridge(options?: IJsBridgeConfig): JsBridgeBase;
|
|
3
4
|
declare class JsBridgeExtInjected extends JsBridgeBase {
|
|
4
5
|
constructor(config: IJsBridgeConfig);
|
|
5
6
|
sendAsString: boolean;
|
|
6
7
|
isInjected: boolean;
|
|
7
8
|
sendPayload(payloadObj: IJsBridgeMessagePayload | string): void;
|
|
8
|
-
setupPostMessageListener(): void;
|
|
9
9
|
}
|
|
10
|
-
export { JsBridgeExtInjected };
|
|
10
|
+
export { JsBridgeExtInjected, getOrCreateExtInjectedJsBridge };
|
|
@@ -1,14 +1,48 @@
|
|
|
1
1
|
/* eslint-disable @typescript-eslint/no-unsafe-call */
|
|
2
2
|
/* eslint-disable @typescript-eslint/no-unsafe-member-access */
|
|
3
|
-
import {
|
|
4
|
-
import { JsBridgeBase } from '@onekeyfe/cross-inpage-provider-core';
|
|
3
|
+
import { fakeDebugLogger, consts } from '@onekeyfe/cross-inpage-provider-core';
|
|
4
|
+
import { JsBridgeBase, injectedProviderReceiveHandler, injectJsBridge, } from '@onekeyfe/cross-inpage-provider-core';
|
|
5
5
|
const { JS_BRIDGE_MESSAGE_DIRECTION, JS_BRIDGE_MESSAGE_EXT_CHANNEL } = consts;
|
|
6
|
+
function getOrCreateExtInjectedJsBridge(options = {}) {
|
|
7
|
+
// create ext bridge by default
|
|
8
|
+
const bridgeCreator = () => new JsBridgeExtInjected(Object.assign(Object.assign({}, options), { receiveHandler: injectedProviderReceiveHandler }));
|
|
9
|
+
const bridge = injectJsBridge(bridgeCreator);
|
|
10
|
+
return bridge;
|
|
11
|
+
}
|
|
12
|
+
let postMessageListenerAdded = false;
|
|
13
|
+
function setupPostMessageListener(options = {}) {
|
|
14
|
+
const debugLogger = options.debugLogger || fakeDebugLogger;
|
|
15
|
+
if (postMessageListenerAdded) {
|
|
16
|
+
return;
|
|
17
|
+
}
|
|
18
|
+
postMessageListenerAdded = true;
|
|
19
|
+
// - receive
|
|
20
|
+
// #### content-script -> injected
|
|
21
|
+
window.addEventListener('message', (event) => {
|
|
22
|
+
var _a;
|
|
23
|
+
// We only accept messages from ourselves
|
|
24
|
+
if (event.source !== window) {
|
|
25
|
+
return;
|
|
26
|
+
}
|
|
27
|
+
const eventData = event.data;
|
|
28
|
+
// only accept extension messages
|
|
29
|
+
if (eventData.channel === JS_BRIDGE_MESSAGE_EXT_CHANNEL &&
|
|
30
|
+
eventData.direction === JS_BRIDGE_MESSAGE_DIRECTION.HOST_TO_INPAGE) {
|
|
31
|
+
debugLogger.extInjected('onWindowPostMessage', eventData);
|
|
32
|
+
const payload = eventData.payload;
|
|
33
|
+
const jsBridge = (_a = window === null || window === void 0 ? void 0 : window.$onekey) === null || _a === void 0 ? void 0 : _a.jsBridge;
|
|
34
|
+
if (jsBridge) {
|
|
35
|
+
jsBridge.receive(payload);
|
|
36
|
+
}
|
|
37
|
+
}
|
|
38
|
+
}, false);
|
|
39
|
+
}
|
|
6
40
|
class JsBridgeExtInjected extends JsBridgeBase {
|
|
7
41
|
constructor(config) {
|
|
8
42
|
super(config);
|
|
9
43
|
this.sendAsString = false;
|
|
10
44
|
this.isInjected = true;
|
|
11
|
-
|
|
45
|
+
setupPostMessageListener(config);
|
|
12
46
|
}
|
|
13
47
|
sendPayload(payloadObj) {
|
|
14
48
|
window.postMessage({
|
|
@@ -17,28 +51,5 @@ class JsBridgeExtInjected extends JsBridgeBase {
|
|
|
17
51
|
payload: payloadObj,
|
|
18
52
|
});
|
|
19
53
|
}
|
|
20
|
-
setupPostMessageListener() {
|
|
21
|
-
// TODO off event
|
|
22
|
-
// - receive
|
|
23
|
-
// #### content-script -> injected
|
|
24
|
-
window.addEventListener('message', (event) => {
|
|
25
|
-
var _a;
|
|
26
|
-
// We only accept messages from ourselves
|
|
27
|
-
if (event.source !== window) {
|
|
28
|
-
return;
|
|
29
|
-
}
|
|
30
|
-
const eventData = event.data;
|
|
31
|
-
// only accept extension messages
|
|
32
|
-
if (eventData.channel === JS_BRIDGE_MESSAGE_EXT_CHANNEL &&
|
|
33
|
-
eventData.direction === JS_BRIDGE_MESSAGE_DIRECTION.HOST_TO_INPAGE) {
|
|
34
|
-
debugLogger.extInjected('window on message', eventData);
|
|
35
|
-
const payload = eventData.payload;
|
|
36
|
-
const jsBridge = (_a = window === null || window === void 0 ? void 0 : window.$onekey) === null || _a === void 0 ? void 0 : _a.jsBridge;
|
|
37
|
-
if (jsBridge) {
|
|
38
|
-
jsBridge.receive(payload);
|
|
39
|
-
}
|
|
40
|
-
}
|
|
41
|
-
}, false);
|
|
42
|
-
}
|
|
43
54
|
}
|
|
44
|
-
export { JsBridgeExtInjected };
|
|
55
|
+
export { JsBridgeExtInjected, getOrCreateExtInjectedJsBridge };
|
|
@@ -1,17 +1,52 @@
|
|
|
1
1
|
"use strict";
|
|
2
2
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
-
exports.JsBridgeExtInjected = void 0;
|
|
3
|
+
exports.getOrCreateExtInjectedJsBridge = exports.JsBridgeExtInjected = void 0;
|
|
4
4
|
/* eslint-disable @typescript-eslint/no-unsafe-call */
|
|
5
5
|
/* eslint-disable @typescript-eslint/no-unsafe-member-access */
|
|
6
6
|
const cross_inpage_provider_core_1 = require("@onekeyfe/cross-inpage-provider-core");
|
|
7
7
|
const cross_inpage_provider_core_2 = require("@onekeyfe/cross-inpage-provider-core");
|
|
8
8
|
const { JS_BRIDGE_MESSAGE_DIRECTION, JS_BRIDGE_MESSAGE_EXT_CHANNEL } = cross_inpage_provider_core_1.consts;
|
|
9
|
+
function getOrCreateExtInjectedJsBridge(options = {}) {
|
|
10
|
+
// create ext bridge by default
|
|
11
|
+
const bridgeCreator = () => new JsBridgeExtInjected(Object.assign(Object.assign({}, options), { receiveHandler: cross_inpage_provider_core_2.injectedProviderReceiveHandler }));
|
|
12
|
+
const bridge = (0, cross_inpage_provider_core_2.injectJsBridge)(bridgeCreator);
|
|
13
|
+
return bridge;
|
|
14
|
+
}
|
|
15
|
+
exports.getOrCreateExtInjectedJsBridge = getOrCreateExtInjectedJsBridge;
|
|
16
|
+
let postMessageListenerAdded = false;
|
|
17
|
+
function setupPostMessageListener(options = {}) {
|
|
18
|
+
const debugLogger = options.debugLogger || cross_inpage_provider_core_1.fakeDebugLogger;
|
|
19
|
+
if (postMessageListenerAdded) {
|
|
20
|
+
return;
|
|
21
|
+
}
|
|
22
|
+
postMessageListenerAdded = true;
|
|
23
|
+
// - receive
|
|
24
|
+
// #### content-script -> injected
|
|
25
|
+
window.addEventListener('message', (event) => {
|
|
26
|
+
var _a;
|
|
27
|
+
// We only accept messages from ourselves
|
|
28
|
+
if (event.source !== window) {
|
|
29
|
+
return;
|
|
30
|
+
}
|
|
31
|
+
const eventData = event.data;
|
|
32
|
+
// only accept extension messages
|
|
33
|
+
if (eventData.channel === JS_BRIDGE_MESSAGE_EXT_CHANNEL &&
|
|
34
|
+
eventData.direction === JS_BRIDGE_MESSAGE_DIRECTION.HOST_TO_INPAGE) {
|
|
35
|
+
debugLogger.extInjected('onWindowPostMessage', eventData);
|
|
36
|
+
const payload = eventData.payload;
|
|
37
|
+
const jsBridge = (_a = window === null || window === void 0 ? void 0 : window.$onekey) === null || _a === void 0 ? void 0 : _a.jsBridge;
|
|
38
|
+
if (jsBridge) {
|
|
39
|
+
jsBridge.receive(payload);
|
|
40
|
+
}
|
|
41
|
+
}
|
|
42
|
+
}, false);
|
|
43
|
+
}
|
|
9
44
|
class JsBridgeExtInjected extends cross_inpage_provider_core_2.JsBridgeBase {
|
|
10
45
|
constructor(config) {
|
|
11
46
|
super(config);
|
|
12
47
|
this.sendAsString = false;
|
|
13
48
|
this.isInjected = true;
|
|
14
|
-
|
|
49
|
+
setupPostMessageListener(config);
|
|
15
50
|
}
|
|
16
51
|
sendPayload(payloadObj) {
|
|
17
52
|
window.postMessage({
|
|
@@ -20,28 +55,5 @@ class JsBridgeExtInjected extends cross_inpage_provider_core_2.JsBridgeBase {
|
|
|
20
55
|
payload: payloadObj,
|
|
21
56
|
});
|
|
22
57
|
}
|
|
23
|
-
setupPostMessageListener() {
|
|
24
|
-
// TODO off event
|
|
25
|
-
// - receive
|
|
26
|
-
// #### content-script -> injected
|
|
27
|
-
window.addEventListener('message', (event) => {
|
|
28
|
-
var _a;
|
|
29
|
-
// We only accept messages from ourselves
|
|
30
|
-
if (event.source !== window) {
|
|
31
|
-
return;
|
|
32
|
-
}
|
|
33
|
-
const eventData = event.data;
|
|
34
|
-
// only accept extension messages
|
|
35
|
-
if (eventData.channel === JS_BRIDGE_MESSAGE_EXT_CHANNEL &&
|
|
36
|
-
eventData.direction === JS_BRIDGE_MESSAGE_DIRECTION.HOST_TO_INPAGE) {
|
|
37
|
-
cross_inpage_provider_core_1.debugLogger.extInjected('window on message', eventData);
|
|
38
|
-
const payload = eventData.payload;
|
|
39
|
-
const jsBridge = (_a = window === null || window === void 0 ? void 0 : window.$onekey) === null || _a === void 0 ? void 0 : _a.jsBridge;
|
|
40
|
-
if (jsBridge) {
|
|
41
|
-
jsBridge.receive(payload);
|
|
42
|
-
}
|
|
43
|
-
}
|
|
44
|
-
}, false);
|
|
45
|
-
}
|
|
46
58
|
}
|
|
47
59
|
exports.JsBridgeExtInjected = JsBridgeExtInjected;
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@onekeyfe/extension-bridge-injected",
|
|
3
|
-
"version": "0.0.
|
|
3
|
+
"version": "0.0.2",
|
|
4
4
|
"keywords": [
|
|
5
5
|
"cross-inpage-provider"
|
|
6
6
|
],
|
|
@@ -28,8 +28,8 @@
|
|
|
28
28
|
"start": "tsc --watch"
|
|
29
29
|
},
|
|
30
30
|
"dependencies": {
|
|
31
|
-
"@onekeyfe/cross-inpage-provider-core": "^0.0.
|
|
32
|
-
"@onekeyfe/cross-inpage-provider-types": "^0.0.
|
|
31
|
+
"@onekeyfe/cross-inpage-provider-core": "^0.0.2",
|
|
32
|
+
"@onekeyfe/cross-inpage-provider-types": "^0.0.2"
|
|
33
33
|
},
|
|
34
|
-
"gitHead": "
|
|
34
|
+
"gitHead": "97aa3c911466f5c3a46b005a69d5b3edd1d8fd7c"
|
|
35
35
|
}
|