@onekeyfe/cross-inpage-provider-core 0.0.9 → 0.0.12

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.
@@ -32,6 +32,7 @@ function isLegacyExtMessage(payload) {
32
32
  return (Boolean(payloadObj.name) &&
33
33
  ['onekey-provider-eth', 'onekey-provider-cfx', 'publicConfig'].includes(payloadObj.name));
34
34
  }
35
+ const globalWindow = typeof window !== 'undefined' ? window : global;
35
36
  const BRIDGE_EVENTS = {
36
37
  message: 'message',
37
38
  error: 'error',
@@ -152,7 +153,7 @@ class JsBridgeBase extends CrossEventEmitter {
152
153
  data,
153
154
  error,
154
155
  type,
155
- origin: ((_a = global === null || global === void 0 ? void 0 : global.location) === null || _a === void 0 ? void 0 : _a.origin) || '',
156
+ origin: ((_a = globalWindow === null || globalWindow === void 0 ? void 0 : globalWindow.location) === null || _a === void 0 ? void 0 : _a.origin) || '',
156
157
  remoteId,
157
158
  scope,
158
159
  }, { resolve, reject });
@@ -0,0 +1,22 @@
1
+ import { IJsBridgeConfig, IJsBridgeMessagePayload, IPostMessageEventData, IOptionsWithDebugLogger } from '@onekeyfe/cross-inpage-provider-types';
2
+ import { JsBridgeBase } from './JsBridgeBase';
3
+ export declare type ISetupPostMessageListenerOptions = IOptionsWithDebugLogger & {
4
+ bridge?: JsBridgeIframe;
5
+ };
6
+ export declare type IPostMessageEventDataIframe = IPostMessageEventData & {
7
+ frameName: string;
8
+ };
9
+ export declare type IJsBridgeIframeConfig = IJsBridgeConfig & {
10
+ remoteFrame: Window;
11
+ selfFrameName: string;
12
+ remoteFrameName: string;
13
+ channel: string;
14
+ };
15
+ declare class JsBridgeIframe extends JsBridgeBase {
16
+ constructor(config: IJsBridgeIframeConfig);
17
+ bridgeConfig: IJsBridgeIframeConfig;
18
+ sendAsString: boolean;
19
+ isInjected: boolean;
20
+ sendPayload(payloadObj: IJsBridgeMessagePayload | string): void;
21
+ }
22
+ export { JsBridgeIframe };
@@ -0,0 +1,55 @@
1
+ /* eslint-disable @typescript-eslint/no-unsafe-call */
2
+ /* eslint-disable @typescript-eslint/no-unsafe-member-access */
3
+ import { JsBridgeBase } from './JsBridgeBase';
4
+ import { fakeDebugLogger } from './index';
5
+ let postMessageListenerAdded = false;
6
+ function setupPostMessageListener(options = {}) {
7
+ const debugLogger = options.debugLogger || fakeDebugLogger;
8
+ if (postMessageListenerAdded) {
9
+ return;
10
+ }
11
+ postMessageListenerAdded = true;
12
+ // - receive
13
+ window.addEventListener('message', (event) => {
14
+ var _a, _b, _c;
15
+ // TODO source whitelist
16
+ if (event.source !== window) {
17
+ // return;
18
+ }
19
+ const eventData = event.data;
20
+ const config = (_a = options.bridge) === null || _a === void 0 ? void 0 : _a.bridgeConfig;
21
+ if (config &&
22
+ eventData.channel === config.channel &&
23
+ eventData.frameName === config.remoteFrameName) {
24
+ const payload = eventData.payload;
25
+ const jsBridge = (_b = options.bridge) !== null && _b !== void 0 ? _b : (_c = window === null || window === void 0 ? void 0 : window.$onekey) === null || _c === void 0 ? void 0 : _c.jsBridge;
26
+ if (jsBridge) {
27
+ jsBridge.receive(payload);
28
+ }
29
+ }
30
+ }, false);
31
+ }
32
+ class JsBridgeIframe extends JsBridgeBase {
33
+ constructor(config) {
34
+ super(config);
35
+ this.sendAsString = false;
36
+ this.isInjected = true;
37
+ this.bridgeConfig = config;
38
+ // receive message
39
+ setupPostMessageListener({
40
+ debugLogger: this.debugLogger,
41
+ bridge: this,
42
+ });
43
+ }
44
+ // send message
45
+ sendPayload(payloadObj) {
46
+ const eventData = {
47
+ channel: this.bridgeConfig.channel,
48
+ frameName: this.bridgeConfig.selfFrameName,
49
+ payload: payloadObj,
50
+ direction: '',
51
+ };
52
+ this.bridgeConfig.remoteFrame.postMessage(eventData);
53
+ }
54
+ }
55
+ export { JsBridgeIframe };
@@ -39,6 +39,7 @@ function isLegacyExtMessage(payload) {
39
39
  ['onekey-provider-eth', 'onekey-provider-cfx', 'publicConfig'].includes(payloadObj.name));
40
40
  }
41
41
  exports.isLegacyExtMessage = isLegacyExtMessage;
42
+ const globalWindow = typeof window !== 'undefined' ? window : global;
42
43
  const BRIDGE_EVENTS = {
43
44
  message: 'message',
44
45
  error: 'error',
@@ -159,7 +160,7 @@ class JsBridgeBase extends CrossEventEmitter_1.CrossEventEmitter {
159
160
  data,
160
161
  error,
161
162
  type,
162
- origin: ((_a = global === null || global === void 0 ? void 0 : global.location) === null || _a === void 0 ? void 0 : _a.origin) || '',
163
+ origin: ((_a = globalWindow === null || globalWindow === void 0 ? void 0 : globalWindow.location) === null || _a === void 0 ? void 0 : _a.origin) || '',
163
164
  remoteId,
164
165
  scope,
165
166
  }, { resolve, reject });
@@ -0,0 +1,58 @@
1
+ "use strict";
2
+ /* eslint-disable @typescript-eslint/no-unsafe-call */
3
+ /* eslint-disable @typescript-eslint/no-unsafe-member-access */
4
+ Object.defineProperty(exports, "__esModule", { value: true });
5
+ exports.JsBridgeIframe = void 0;
6
+ const JsBridgeBase_1 = require("./JsBridgeBase");
7
+ const index_1 = require("./index");
8
+ let postMessageListenerAdded = false;
9
+ function setupPostMessageListener(options = {}) {
10
+ const debugLogger = options.debugLogger || index_1.fakeDebugLogger;
11
+ if (postMessageListenerAdded) {
12
+ return;
13
+ }
14
+ postMessageListenerAdded = true;
15
+ // - receive
16
+ window.addEventListener('message', (event) => {
17
+ var _a, _b, _c;
18
+ // TODO source whitelist
19
+ if (event.source !== window) {
20
+ // return;
21
+ }
22
+ const eventData = event.data;
23
+ const config = (_a = options.bridge) === null || _a === void 0 ? void 0 : _a.bridgeConfig;
24
+ if (config &&
25
+ eventData.channel === config.channel &&
26
+ eventData.frameName === config.remoteFrameName) {
27
+ const payload = eventData.payload;
28
+ const jsBridge = (_b = options.bridge) !== null && _b !== void 0 ? _b : (_c = window === null || window === void 0 ? void 0 : window.$onekey) === null || _c === void 0 ? void 0 : _c.jsBridge;
29
+ if (jsBridge) {
30
+ jsBridge.receive(payload);
31
+ }
32
+ }
33
+ }, false);
34
+ }
35
+ class JsBridgeIframe extends JsBridgeBase_1.JsBridgeBase {
36
+ constructor(config) {
37
+ super(config);
38
+ this.sendAsString = false;
39
+ this.isInjected = true;
40
+ this.bridgeConfig = config;
41
+ // receive message
42
+ setupPostMessageListener({
43
+ debugLogger: this.debugLogger,
44
+ bridge: this,
45
+ });
46
+ }
47
+ // send message
48
+ sendPayload(payloadObj) {
49
+ const eventData = {
50
+ channel: this.bridgeConfig.channel,
51
+ frameName: this.bridgeConfig.selfFrameName,
52
+ payload: payloadObj,
53
+ direction: '',
54
+ };
55
+ this.bridgeConfig.remoteFrame.postMessage(eventData);
56
+ }
57
+ }
58
+ exports.JsBridgeIframe = JsBridgeIframe;
package/dist/cjs/index.js CHANGED
@@ -32,6 +32,7 @@ __exportStar(require("./loggers"), exports);
32
32
  __exportStar(require("./injectJsBridge"), exports);
33
33
  __exportStar(require("./injectedProviderReceiveHandler"), exports);
34
34
  __exportStar(require("./JsBridgeSimple"), exports);
35
+ __exportStar(require("./JsBridgeIframe"), exports);
35
36
  __exportStar(require("./CrossEventEmitter"), exports);
36
37
  const consts = __importStar(require("./consts"));
37
38
  exports.consts = consts;
@@ -1,5 +1,6 @@
1
1
  "use strict";
2
2
  Object.defineProperty(exports, "__esModule", { value: true });
3
+ /* eslint-disable @typescript-eslint/ban-ts-comment */
3
4
  function createCodeWithScriptTag({ code }) {
4
5
  // script id check, only inject once.
5
6
  return `
@@ -16,11 +17,11 @@ function createCodeWithScriptTag({ code }) {
16
17
  function injectCodeWithScriptTag({ code, file, remove = true, }) {
17
18
  (function () {
18
19
  const s = document.createElement('script');
19
- s.setAttribute('async', 'false');
20
- s.setAttribute('defer', 'false');
21
- s.setAttribute('data-onekey-injected', 'true');
20
+ s.removeAttribute('async');
21
+ s.removeAttribute('defer');
22
+ s.setAttribute('data-onekey-injected', 'yes');
22
23
  if (code) {
23
- s.textContent = JSON.stringify(code);
24
+ s.textContent = code;
24
25
  }
25
26
  if (file) {
26
27
  s.src = file;
@@ -1,6 +1,6 @@
1
1
  "use strict";
2
2
  Object.defineProperty(exports, "__esModule", { value: true });
3
- const version = '0.0.9';
3
+ const version = '0.0.12';
4
4
  const versionBuild = '2020-0101-1';
5
5
  exports.default = {
6
6
  version,
package/dist/index.d.ts CHANGED
@@ -4,6 +4,7 @@ export * from './loggers';
4
4
  export * from './injectJsBridge';
5
5
  export * from './injectedProviderReceiveHandler';
6
6
  export * from './JsBridgeSimple';
7
+ export * from './JsBridgeIframe';
7
8
  export * from './CrossEventEmitter';
8
9
  import * as consts from './consts';
9
10
  export { consts };
package/dist/index.js CHANGED
@@ -4,6 +4,7 @@ export * from './loggers';
4
4
  export * from './injectJsBridge';
5
5
  export * from './injectedProviderReceiveHandler';
6
6
  export * from './JsBridgeSimple';
7
+ export * from './JsBridgeIframe';
7
8
  export * from './CrossEventEmitter';
8
9
  import * as consts from './consts';
9
10
  export { consts };
@@ -1,3 +1,4 @@
1
+ /* eslint-disable @typescript-eslint/ban-ts-comment */
1
2
  function createCodeWithScriptTag({ code }) {
2
3
  // script id check, only inject once.
3
4
  return `
@@ -14,11 +15,11 @@ function createCodeWithScriptTag({ code }) {
14
15
  function injectCodeWithScriptTag({ code, file, remove = true, }) {
15
16
  (function () {
16
17
  const s = document.createElement('script');
17
- s.setAttribute('async', 'false');
18
- s.setAttribute('defer', 'false');
19
- s.setAttribute('data-onekey-injected', 'true');
18
+ s.removeAttribute('async');
19
+ s.removeAttribute('defer');
20
+ s.setAttribute('data-onekey-injected', 'yes');
20
21
  if (code) {
21
- s.textContent = JSON.stringify(code);
22
+ s.textContent = code;
22
23
  }
23
24
  if (file) {
24
25
  s.src = file;
@@ -1,4 +1,4 @@
1
- const version = '0.0.9';
1
+ const version = '0.0.12';
2
2
  const versionBuild = '2020-0101-1';
3
3
  export default {
4
4
  version,
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@onekeyfe/cross-inpage-provider-core",
3
- "version": "0.0.9",
3
+ "version": "0.0.12",
4
4
  "keywords": [
5
5
  "cross-inpage-provider"
6
6
  ],
@@ -29,12 +29,12 @@
29
29
  "build-version-info": "node ./scripts/buildVersionInfo.js"
30
30
  },
31
31
  "dependencies": {
32
- "@onekeyfe/cross-inpage-provider-errors": "^0.0.9",
33
- "@onekeyfe/cross-inpage-provider-events": "^0.0.9",
34
- "@onekeyfe/cross-inpage-provider-types": "^0.0.9",
32
+ "@onekeyfe/cross-inpage-provider-errors": "^0.0.12",
33
+ "@onekeyfe/cross-inpage-provider-events": "^0.0.12",
34
+ "@onekeyfe/cross-inpage-provider-types": "^0.0.12",
35
35
  "events": "^3.3.0",
36
36
  "lodash": "^4.17.21",
37
37
  "ms": "^2.1.3"
38
38
  },
39
- "gitHead": "1a531ca7434eca9b6704760cb0221df3cfdbe4d5"
39
+ "gitHead": "8d2544a62c77934ce19b19439476bf35d88bd5b8"
40
40
  }