ghost-keychain/packages/extension/src/content.ts
2021-01-03 11:33:53 +01:00

40 lines
1.2 KiB
TypeScript

// Copyright 2019-2021 @polkadot/extension authors & contributors
// SPDX-License-Identifier: Apache-2.0
import type { Message } from '@polkadot/extension-base/types';
import { PORT_CONTENT } from '@polkadot/extension-base/defaults';
import chrome from '@polkadot/extension-inject/chrome';
// connect to the extension
const port = chrome.runtime.connect({ name: PORT_CONTENT });
// send any messages from the extension back to the page
port.onMessage.addListener((data): void => {
window.postMessage({ ...data, origin: 'content' }, '*');
});
// all messages from the page, pass them to the extension
window.addEventListener('message', ({ data, source }: Message): void => {
// only allow messages from our window, by the inject
if (source !== window || data.origin !== 'page') {
return;
}
port.postMessage(data);
});
// inject our data injector
const script = document.createElement('script');
script.src = chrome.extension.getURL('page.js');
script.onload = (): void => {
// remove the injecting tag when loaded
if (script.parentNode) {
script.parentNode.removeChild(script);
}
};
(document.head || document.documentElement).appendChild(script);