1
0
mirror of https://github.com/kremalicious/metamask-extension.git synced 2024-11-25 11:28:51 +01:00
metamask-extension/app/scripts/detect-multiple-instances.js
Hennadii Ivtushok 72d2977e72
Warn about multiple MetaMask instances running (#13836)
* Add text warning on startup page

* Try to detect extensions with browser API

* Setup messaging between different versions of extension

* Cleanup

* Cleanup

* Simplify check for multiple instances running

* Fix a doc string + use webextension-polyfill

* Fix test

* Mock webextension-polyfill

* Mock correctly

* Catch error and show warning in both extensions

* Mock as promise

* Address comments

* Rename build ids

* Run detection code only if Chrome

* Add Firefox warnings

* Cleanup imports

* Update connection ids

* Run detection code for Firefox

* Add test

* Add missing await

* Update tests

* Cleanup

* Cleanup

* Improve testing

* Improve tests

* Log errors from sendMessage

* Cleanup

Co-authored-by: Frederik Bolding <frederik.bolding@gmail.com>
2022-04-27 20:14:10 +02:00

56 lines
1.5 KiB
JavaScript

/**
* Sets up two-way communication between the
* mainline version of extension and Flask build
* in order to detect & warn if there are two different
* versions running simultaneously.
*/
import browser from 'webextension-polyfill';
import {
PLATFORM_CHROME,
PLATFORM_FIREFOX,
CHROME_BUILD_IDS,
FIREFOX_BUILD_IDS,
} from '../../shared/constants/app';
import { getPlatform } from './lib/util';
const MESSAGE_TEXT = 'isRunning';
const showWarning = () =>
console.warn('Warning! You have multiple instances of MetaMask running!');
/**
* Handles the ping message sent from other extension.
* Displays console warning if it's active.
*
* @param message - The message received from the other extension
*/
export const onMessageReceived = (message) => {
if (message === MESSAGE_TEXT) {
showWarning();
}
};
/**
* Sends the ping message sent to other extensions to detect whether it's active or not.
*/
export const checkForMultipleVersionsRunning = async () => {
if (getPlatform() !== PLATFORM_CHROME && getPlatform() !== PLATFORM_FIREFOX) {
return;
}
const buildIds =
getPlatform() === PLATFORM_CHROME ? CHROME_BUILD_IDS : FIREFOX_BUILD_IDS;
const thisBuild = browser.runtime.id;
for (const id of buildIds) {
if (id !== thisBuild) {
try {
await browser.runtime.sendMessage(id, MESSAGE_TEXT);
} catch (error) {
// Should do nothing if receiving end was not reached (no other instances running)
}
}
}
};