1
0
mirror of https://github.com/kremalicious/metamask-extension.git synced 2024-11-25 11:28:51 +01:00
metamask-extension/app/scripts/inpage.js
Ariella Vu d3c7b9fb32
MV3: contentscript.js - re-activate streams when Service Worker terminates and then resets (#15494)
* contentscript: reactivate streams pt.1

* contentscript: reactivate streams pt. 2

* scripts/inpage.js: use const

* clean: rm unused @param

* contentscript: reactivate streams pt. 3
- reorganize functions

* resetSteamAndListeners -> resetStreamAndListeners

* contentscript: skip Legacy Provider while WIP

* contentscript: rm comment sentence

* initPageStreams -> initStreams

* setupPageStreams -> setupStreams

* contentscript: only destroy extension streams WIP

* remove pageMuxChannel listeners (It works!)
- credits to @gudhatt for this missing piece

Co-authored-by: Mark Stacey <markjstacey@gmail.com>

* contentscript: cleanup

* contentscript: support legacy streams
w/ service workers

* contentscript: support phishing streams
w/ service workers

* contentscript: muxChannel -> channel

* contentscript: phishingExension -> phishingExt

* contentscript: add section comments

* contentscript: revert condensing comment

* contentscript: pagePhishing -> phishingPage

* contentscript: update comments

* contentscript: fix phishingExtPort

* contentscript: stream -> streams

* contentscript: update SW keep alive logic
should only keep alive when dapp is open / contentscript page

* rm console.log

Co-authored-by: Mark Stacey <markjstacey@gmail.com>
Co-authored-by: Jyoti Puri <jyotipuri@gmail.com>
2022-09-13 14:20:08 -07:00

64 lines
1.6 KiB
JavaScript

// need to make sure we aren't affected by overlapping namespaces
// and that we dont affect the app with our namespace
// mostly a fix for web3's BigNumber if AMD's "define" is defined...
let __define;
/**
* Caches reference to global define object and deletes it to
* avoid conflicts with other global define objects, such as
* AMD's define function
*/
const cleanContextForImports = () => {
__define = global.define;
try {
global.define = undefined;
} catch (_) {
console.warn('MetaMask - global.define could not be deleted.');
}
};
/**
* Restores global define object from cached reference
*/
const restoreContextAfterImports = () => {
try {
global.define = __define;
} catch (_) {
console.warn('MetaMask - global.define could not be overwritten.');
}
};
cleanContextForImports();
/* eslint-disable import/first */
import log from 'loglevel';
import { WindowPostMessageStream } from '@metamask/post-message-stream';
import { initializeProvider } from '@metamask/providers/dist/initializeInpageProvider';
import shouldInjectProvider from '../../shared/modules/provider-injection';
// contexts
const CONTENT_SCRIPT = 'metamask-contentscript';
const INPAGE = 'metamask-inpage';
restoreContextAfterImports();
log.setDefaultLevel(process.env.METAMASK_DEBUG ? 'debug' : 'warn');
//
// setup plugin communication
//
if (shouldInjectProvider()) {
// setup background connection
const metamaskStream = new WindowPostMessageStream({
name: INPAGE,
target: CONTENT_SCRIPT,
});
initializeProvider({
connectionStream: metamaskStream,
logger: log,
shouldShimWeb3: true,
});
}