diff --git a/packages/selenium-ide/src/api/v1/index.js b/packages/selenium-ide/src/api/v1/index.js index 695a32a248..d0ea2f1a70 100644 --- a/packages/selenium-ide/src/api/v1/index.js +++ b/packages/selenium-ide/src/api/v1/index.js @@ -34,38 +34,37 @@ const router = new Router() const errors = { cannotAccessInControlMode: { errorCode: 'CannotAccessInControlMode', - error: 'Selenium IDE is controlled by a different extension.' + error: 'Selenium IDE is controlled by a different extension.', }, missingPlugin: { errorCode: 'MissingPlugin', - error: 'Plugin is not registered' + error: 'Plugin is not registered', }, missingConnectionId: { errorCode: 'MissingConnectionId', - error: 'No connection Id specified' + error: 'No connection Id specified', }, missingProject: { errorCode: 'MissingProject', - error: 'Plugin is not registered' - } + error: 'Plugin is not registered', + }, } function checkControl(req) { - return Manager.controller && Manager.controller.connectionId != req.connectionId + return Manager.controller && + Manager.controller.connectionId != req.connectionId ? Promise.reject() : Promise.resolve() } function controlledOnly(req, res) { - return checkControl(req) - .catch(() => { - res(errors.cannotAccessInControlMode) - return errors.cannotAccessInControlMode - }) + return checkControl(req).catch(() => { + res(errors.cannotAccessInControlMode) + return errors.cannotAccessInControlMode + }) } -function tryOverrideControl(req) -{ +function tryOverrideControl(req) { if (!req.connectionId) return if (!ModalState.welcomeState.completed) { ModalState.hideWelcome() @@ -76,8 +75,7 @@ function tryOverrideControl(req) description: `${req.name} is trying to control Selenium IDE`, confirmLabel: 'Restart and Allow access', cancelLabel: 'Deny access', - }) - .then(r => { + }).then(r => { if (r) { const plugin = { id: req.sender, @@ -98,77 +96,66 @@ router.get('/health', (req, res) => { res(Manager.hasPlugin(req.sender)) }) -router.post( - '/register', - (req, res) => { - controlledOnly(req, res).then(() => { - const plugin = { - id: req.sender, - name: req.name, - version: req.version, - commands: req.commands, - dependencies: req.dependencies, - jest: req.jest, - exports: req.exports, - } - Manager.registerPlugin(plugin) - res(true) - }); - } -) +router.post('/register', (req, res) => { + controlledOnly(req, res).then(() => { + const plugin = { + id: req.sender, + name: req.name, + version: req.version, + commands: req.commands, + dependencies: req.dependencies, + jest: req.jest, + exports: req.exports, + } + Manager.registerPlugin(plugin) + res(true) + }) +}) -router.post( - '/log', - (req, res) => { - controlledOnly(req, res).then(() => { - if (req.type === LogTypes.Error) { - logger.error(`${Manager.getPlugin(req.sender).name}: ${req.message}`) - } else if (req.type === LogTypes.Warning) { - logger.warn(`${Manager.getPlugin(req.sender).name}: ${req.message}`) - } else { - logger.log(`${Manager.getPlugin(req.sender).name}: ${req.message}`) - } - res(true) - }) - } -) +router.post('/log', (req, res) => { + controlledOnly(req, res).then(() => { + if (req.type === LogTypes.Error) { + logger.error(`${Manager.getPlugin(req.sender).name}: ${req.message}`) + } else if (req.type === LogTypes.Warning) { + logger.warn(`${Manager.getPlugin(req.sender).name}: ${req.message}`) + } else { + logger.log(`${Manager.getPlugin(req.sender).name}: ${req.message}`) + } + res(true) + }) +}) -router.get( - '/project', - (_req, res) => { - controlledOnly(_req, res).then(() => { - res({ id: UiState.project.id, name: UiState.project.name }) - }) - } -) +router.get('/project', (_req, res) => { + controlledOnly(_req, res).then(() => { + res({ id: UiState.project.id, name: UiState.project.name }) + }) +}) router.post('/control', (req, res) => { - checkControl(req).then(() =>{ - if(UiState.isControlled) - { - // Already in control mode with the same connection id. - res(true) - } - else - { - // Already in non-control mode. + checkControl(req) + .then(() => { + if (UiState.isControlled) { + // Already in control mode with the same connection id. + res(true) + } else { + // Already in non-control mode. + tryOverrideControl(req) + .then(() => res(true)) + .catch(() => res(false)) + } + }) + .catch(() => { + // Already in control mode but another connection come. tryOverrideControl(req) .then(() => res(true)) .catch(() => res(false)) - } - }) - .catch(() => { - // Already in control mode but another connection come. - tryOverrideControl(req) - .then(() => res(true)) - .catch(() => res(false)) - }) + }) }) router.post('/close', (req, res) => { - controlledOnly(req, res).then(() =>{ + controlledOnly(req, res).then(() => { // Not allow close if is not control mode. - if(!UiState.isControlled) { + if (!UiState.isControlled) { return res(false) } const plugin = Manager.getPlugin(req.sender) @@ -176,9 +163,7 @@ router.post('/close', (req, res) => { if (!UiState.isSaved()) { ModalState.showAlert({ title: 'Close project without saving', - description: `${ - plugin.name - } is trying to close a project, are you sure you want to load this project and lose all unsaved changes?`, + description: `${plugin.name} is trying to close a project, are you sure you want to load this project and lose all unsaved changes?`, confirmLabel: 'proceed', cancelLabel: 'cancel', }).then(result => { @@ -189,9 +174,7 @@ router.post('/close', (req, res) => { }) res(false) - } - else - { + } else { window.close() res(true) } @@ -214,42 +197,35 @@ router.post('/_connect', (req, res) => { } }) -router.post( - '/project', - (req, res) => { - controlledOnly(req, res).then(() => { - const plugin = Manager.getPlugin(req.sender) - if (!plugin) return res(errors.missingPlugin) - if (!req.project) return res(errors.missingProject) - if (req.project) { - if (!UiState.isSaved()) { - WindowSession.focusIDEWindow() - ModalState.showAlert({ - title: 'Open project without saving', - description: `${ - plugin.name - } is trying to load a project, are you sure you want to load this project and lose all unsaved changes?`, - confirmLabel: 'proceed', - cancelLabel: 'cancel', - }) - .then(result => { - if (result) { - loadJSProject(UiState.project, req.project) - ModalState.completeWelcome() - res(true) - } - }) - } else { - WindowSession.focusIDEWindow() +router.post('/project', (req, res) => { + controlledOnly(req, res).then(() => { + const plugin = Manager.getPlugin(req.sender) + if (!plugin) return res(errors.missingPlugin) + if (!req.project) return res(errors.missingProject) + + if (!UiState.isSaved()) { + WindowSession.focusIDEWindow() + ModalState.showAlert({ + title: 'Open project without saving', + description: `${plugin.name} is trying to load a project, are you sure you want to load this project and lose all unsaved changes?`, + confirmLabel: 'proceed', + cancelLabel: 'cancel', + }).then(result => { + if (result) { loadJSProject(UiState.project, req.project) ModalState.completeWelcome() res(true) } - } - res(false) - }) - } -) + }) + } else { + WindowSession.focusIDEWindow() + loadJSProject(UiState.project, req.project) + ModalState.completeWelcome() + res(true) + } + res(false) + }) +}) router.use('/playback', playbackRouter) router.use('/record', recordRouter) diff --git a/packages/selenium-ide/src/background/background.js b/packages/selenium-ide/src/background/background.js index faa1be2708..9b9cacc24e 100644 --- a/packages/selenium-ide/src/background/background.js +++ b/packages/selenium-ide/src/background/background.js @@ -199,8 +199,8 @@ function handleInternalMessage(message) { }) .then(() => { openPanel({ windowId: 0 }).then(() => { - var payload = { ...message }; - delete payload.restart; + var payload = { ...message } + delete payload.restart const newMessage = { uri: '/_connect', @@ -234,9 +234,8 @@ browser.runtime.onMessageExternal.addListener( let payload = message.payload payload.sender = sender.id - if (message.uri === '/_connect' || message.uri === '/_close') - { - return sendResponse(false); + if (message.uri === '/_connect' || message.uri === '/_close') { + return sendResponse(false) } browser.runtime .sendMessage(message)