ghost-keychain/packages/extension/src/content.ts
Tarik Gul b6d42c28b5
Transition from manifest v2 to v3 for chrome (#1367)
* Update manifest.json

* replace `extension.getUrl` to `runtime.getUrl`

* Update localStorage to async chrome.storage.local

* Change action to browserAction

* fix bug

* revert async changes

* revert test change

* Fix localStorage breaking change

* .extension -> .runtime

* fix undefined behaviour

* fixed tests

* lint

* Up some callbacks to async

* Change where extractMetadata is called

* fix  in mock

* Update background field to take in type

* Change background.js to service_worker.js

* Update id and request origin for service worker

* updated chrome mock

* 13 tests passing

* Fix async issue

* switch naming back to original

* linting

* Fix linting in base

* remove timeOut

* added permission to alarms in manifest

* fixed alarms uncaught errors

* Cleanup addr for tests

---------

Co-authored-by: bee344 <alberto.penayo@parity.io>
2024-07-07 22:32:42 -04:00

40 lines
1.2 KiB
TypeScript

// Copyright 2019-2024 @polkadot/extension authors & contributors
// SPDX-License-Identifier: Apache-2.0
import type { Message } from '@polkadot/extension-base/types';
import { MESSAGE_ORIGIN_CONTENT, MESSAGE_ORIGIN_PAGE, 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: MESSAGE_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 !== MESSAGE_ORIGIN_PAGE) {
return;
}
port.postMessage(data);
});
// inject our data injector
const script = document.createElement('script');
script.src = chrome.runtime.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);