diff --git a/package.json b/package.json index 842b6bb6e..376ead9d3 100644 --- a/package.json +++ b/package.json @@ -55,7 +55,7 @@ "no-unsanitized" ], "rules": { - "no-negated-condition": "warn", + "no-negated-condition": "error", "no-unsanitized/method": [ "error", { @@ -76,13 +76,13 @@ } } ], - "no-unused-vars": "warn", - "prefer-destructuring": "warn", - "unicorn/no-for-loop": "warn", - "unicorn/prefer-includes": "warn", - "unicorn/prefer-string-slice": "warn", + "no-unused-vars": "error", + "prefer-destructuring": "error", + "unicorn/no-for-loop": "error", + "unicorn/prefer-includes": "error", + "unicorn/prefer-string-slice": "error", "unicorn/filename-case": "warn", - "unicorn/prefer-query-selector": "warn" + "unicorn/prefer-query-selector": "error" } }, "repository": { diff --git a/src/main/zapHomeFiles/hud/display.js b/src/main/zapHomeFiles/hud/display.js index dab7e9e18..22641ea80 100644 --- a/src/main/zapHomeFiles/hud/display.js +++ b/src/main/zapHomeFiles/hud/display.js @@ -27,7 +27,7 @@ Vue.component('modal', { close() { this.$emit('close'); }, - afterLeave(element) { + afterLeave(_element) { if (!app.keepShowing) { app.backStack = []; hideDisplayFrame(); @@ -402,13 +402,13 @@ Vue.component('http-message-modal', { let header = ''; let body = ''; - if (!this.response.isReadonly) { - header = this.response.header; - body = this.response.body; - } else { + if (this.response.isReadonly) { method = this.request.method; header = this.request.header; body = this.request.body; + } else { + header = this.response.header; + body = this.response.body; } return {method, header, body}; @@ -529,7 +529,7 @@ Vue.component('history-message-modal', { }, [channel.port2]); }, ascanRequest() { - const request = this.request; + const {request} = this; this.$emit('close'); navigator.serviceWorker.controller.postMessage( { @@ -582,7 +582,7 @@ Vue.component('ws-message-modal', { }, computed: { currentMessage() { - const payload = this.payload; + const {payload} = this; return {payload}; } } @@ -736,10 +736,9 @@ Vue.component('site-tree-node', { channel.port1.addEventListener('message', event => { // Remove the ..loading.. child Vue.set(treeNode.model, 'children', []); - for (let i = 0; i < event.data.childNodes.length; i++) { - const child = event.data.childNodes[i]; + event.data.childNodes.forEach(child => { treeNode.addChild(child.name, child.method, child.isLeaf, child.hrefId); - } + }); }); navigator.serviceWorker.controller.postMessage({ @@ -755,7 +754,7 @@ Vue.component('site-tree-node', { if ((name.match(/\//g) || []).length > 2) { // If there are more than 2 slashes just show last url element // The first 2 slashes will be http(s)://... - name = name.substring(name.lastIndexOf('/') + 1); + name = name.slice(name.lastIndexOf('/') + 1); } if (isLeaf) { @@ -921,8 +920,8 @@ document.addEventListener('DOMContentLoaded', () => { }); navigator.serviceWorker.addEventListener('message', event => { - const action = event.data.action; - const config = event.data.config; + const {data: {action}} = event; + const {data: {config}} = event; const port = event.ports[0]; let show; diff --git a/src/main/zapHomeFiles/hud/drawer.js b/src/main/zapHomeFiles/hud/drawer.js index 7a6729931..391ec51c6 100644 --- a/src/main/zapHomeFiles/hud/drawer.js +++ b/src/main/zapHomeFiles/hud/drawer.js @@ -1,4 +1,5 @@ // App is the main Vue object controlling everything + let app; const eventBus = new Vue(); let frameId = ''; @@ -55,7 +56,7 @@ Vue.component('history', { return re.test(message.url); } - return message.url.indexOf(self.filter) >= 0; + return message.url.includes(self.filter); }); } }, @@ -72,12 +73,12 @@ Vue.component('history', { }, watch: { regexEnabled() { - this.isRegExError = !this.regexEnabled ? false : this.isRegExError; + this.isRegExError = this.regexEnabled ? this.isRegExError : false; }, filteredMessages() { this.$nextTick(function () { const visibleMessages = document.querySelectorAll('#history-messages .message-tr'); - this.hiddenMessageCount = (!this.messages) ? 0 : this.messages.length - visibleMessages.length; + this.hiddenMessageCount = this.messages ? this.messages.length - visibleMessages.length : 0; this.historyItemsFiltered(); }); } @@ -104,7 +105,7 @@ Vue.component('history', { if (this.messages.length > 0) { const lastMessage = this.messages[this.messages.length - 1]; const lastid = 'message-tr-' + lastMessage.id; - const lastIdElement = document.getElementById(lastid); + const lastIdElement = document.querySelector('#' + lastid); if (lastIdElement) { lastIdElement.scrollIntoView({block: 'end', behavior: 'smooth'}); @@ -169,7 +170,7 @@ Vue.component('websockets', { return re.test(message.messageSummary); } - return message.messageSummary.indexOf(self.filter) >= 0; + return message.messageSummary.includes(self.filter); }); } }, @@ -186,12 +187,12 @@ Vue.component('websockets', { }, watch: { regexEnabled() { - this.isRegExError = !this.regexEnabled ? false : this.isRegExError; + this.isRegExError = this.regexEnabled ? this.isRegExError : false; }, filteredMessages() { this.$nextTick(function () { const visibleMessages = document.querySelectorAll('#websockets-messages .message-tr'); - this.hiddenMessageCount = (!this.messages) ? 0 : this.messages.length - visibleMessages.length; + this.hiddenMessageCount = this.messages ? this.messages.length - visibleMessages.length : 0; this.websocketsItemsFiltered(); }); } @@ -315,10 +316,10 @@ Vue.component('tabs', { }) .catch(utils.errorHandler); - eventBus.$on('showTabs', data => { + eventBus.$on('showTabs', _data => { this.tabsVisible = true; }); - eventBus.$on('hideTabs', data => { + eventBus.$on('hideTabs', _data => { this.tabsVisible = false; if (this.isOpen) { this.closeDrawer(); @@ -431,7 +432,7 @@ Vue.component('drawer-button-showhide', { showHud() { this.isHudVisible = true; localforage.setItem('settings.isHudVisible', true) - .then(function (value) { + .then(function (_value) { this.icon = utils.getZapImagePath('radar.png'); parent.postMessage({tabId, frameId, action: 'showHudPanels'}, context.url); eventBus.$emit('showTabs', {}); @@ -441,7 +442,7 @@ Vue.component('drawer-button-showhide', { hideHud() { this.isHudVisible = false; localforage.setItem('settings.isHudVisible', false) - .then(function (value) { + .then(function (_value) { this.icon = utils.getZapImagePath('radar-grey.png'); parent.postMessage({tabId, frameId, action: 'hideHudPanels'}, context.url); eventBus.$emit('hideTabs', {}); @@ -477,6 +478,7 @@ document.addEventListener('DOMContentLoaded', () => { tabId = parameters.get('tabId'); /* Vue app */ + // eslint-disable-next-line no-unused-vars app = new Vue({ i18n: I18n.i18n, el: '#app', @@ -485,7 +487,7 @@ document.addEventListener('DOMContentLoaded', () => { }); navigator.serviceWorker.addEventListener('message', event => { - const action = event.data.action; + const {data: {action}} = event; const port = event.ports[0]; switch (action) { diff --git a/src/main/zapHomeFiles/hud/management.js b/src/main/zapHomeFiles/hud/management.js index 4bc90ac58..d98e8dd10 100644 --- a/src/main/zapHomeFiles/hud/management.js +++ b/src/main/zapHomeFiles/hud/management.js @@ -15,7 +15,6 @@ const ZAP_SHARED_SECRET = '<>'; let app; let tabId = ''; -let frameId = ''; const urlParameter = utils.getParameter(document.location.href, 'url'); const context = { url: urlParameter, @@ -56,7 +55,6 @@ function showTutorial() { document.addEventListener('DOMContentLoaded', () => { const parameters = new URL(document.location).searchParams; - frameId = parameters.get('frameId'); tabId = parameters.get('tabId'); app = new Vue({ diff --git a/src/main/zapHomeFiles/hud/panel.js b/src/main/zapHomeFiles/hud/panel.js index 32831481d..beee370b2 100644 --- a/src/main/zapHomeFiles/hud/panel.js +++ b/src/main/zapHomeFiles/hud/panel.js @@ -212,6 +212,7 @@ document.addEventListener('DOMContentLoaded', () => { window.name = panelKey; // Initialize vue app + // eslint-disable-next-line no-unused-vars app = new Vue({ i18n: I18n.i18n, el: '#app', diff --git a/src/main/zapHomeFiles/hud/serviceworker.js b/src/main/zapHomeFiles/hud/serviceworker.js index 89ecd0f99..8962fe2b1 100644 --- a/src/main/zapHomeFiles/hud/serviceworker.js +++ b/src/main/zapHomeFiles/hud/serviceworker.js @@ -97,7 +97,7 @@ function initWebSockets() { self.dispatchEvent(ev); } else if ('id' in jevent && 'response' in jevent) { const pFunctions = webSocketCallbacks[jevent.id]; - const response = jevent.response; + const {response} = jevent; if ('code' in response && 'message' in response) { // These always indicate a failure const error = new Error(I18n.t('error_with_message', [response.message])); @@ -234,6 +234,7 @@ self.addEventListener('error', utils.errorHandler); self.addEventListener('hud.log', logHandler); self.addEventListener('hud.backup', backupHandler); +// eslint-disable-next-line no-unused-vars function registerForZapEvents(publisher) { apiCall('event', 'register', publisher); } diff --git a/src/main/zapHomeFiles/hud/target/inject.js b/src/main/zapHomeFiles/hud/target/inject.js index 228818c91..dcb2ee3a2 100644 --- a/src/main/zapHomeFiles/hud/target/inject.js +++ b/src/main/zapHomeFiles/hud/target/inject.js @@ -33,7 +33,7 @@ const injection = (function () { const r = Math.floor(Math.random() * 1000); const tabId = String(millis) + '-' + r; - return tabId.substring(6); + return tabId.slice(6); } /* TARGET INTERACTIONS */ @@ -46,27 +46,27 @@ const injection = (function () { /* Change width of iframe for expanding buttons */ function expandPanel(panelOrientation) { // Todo: is this too hacky? - const panel = document.getElementById(HUD_PREFIX + panelOrientation + '-panel'); + const panel = document.querySelector('#' + HUD_PREFIX + panelOrientation + '-panel'); panel.style.width = '300px'; } function contractPanel(panelOrientation) { - const panel = document.getElementById(HUD_PREFIX + panelOrientation + '-panel'); + const panel = document.querySelector('#' + HUD_PREFIX + panelOrientation + '-panel'); panel.style.width = '110px'; } /* Dynamically size growler iframes with number of alerts */ function heightenGrowlerFrame(lines) { - const panel = document.getElementById(GROWLER_ALERTS); + const panel = document.querySelector('#' + GROWLER_ALERTS); const offset = 56 + (30 * lines); panel.style.height = (panel.offsetHeight + offset) + 'px'; } function shortenGrowlerFrame(lines) { - const panel = document.getElementById(GROWLER_ALERTS); + const panel = document.querySelector('#' + GROWLER_ALERTS); const offset = 56 + (30 * lines); panel.style.height = (panel.offsetHeight - offset) + 'px'; @@ -74,95 +74,95 @@ const injection = (function () { /* Dynamically size iframes with number of buttons */ function heighten(panelOrientation) { - const panel = document.getElementById(HUD_PREFIX + panelOrientation + '-panel'); + const panel = document.querySelector('#' + HUD_PREFIX + panelOrientation + '-panel'); panel.style.height = (panel.offsetHeight + 33) + 'px'; } function shorten(panelOrientation) { - const panel = document.getElementById(HUD_PREFIX + panelOrientation + '-panel'); + const panel = document.querySelector('#' + HUD_PREFIX + panelOrientation + '-panel'); panel.style.height = (panel.offsetHeight - 33) + 'px'; } function showHudPanels() { - document.getElementById(LEFT_PANEL).style.display = ''; - document.getElementById(RIGHT_PANEL).style.display = ''; - const panel = document.getElementById(BOTTOM_DRAWER); + document.querySelector('#' + LEFT_PANEL).style.display = ''; + document.querySelector('#' + RIGHT_PANEL).style.display = ''; + const panel = document.querySelector('#' + BOTTOM_DRAWER); panel.style.width = '100%'; panel.style.left = '0px'; panel.style.right = ''; } function hideHudPanels() { - document.getElementById(LEFT_PANEL).style.display = 'none'; - document.getElementById(RIGHT_PANEL).style.display = 'none'; - const panel = document.getElementById(BOTTOM_DRAWER); + document.querySelector('#' + LEFT_PANEL).style.display = 'none'; + document.querySelector('#' + RIGHT_PANEL).style.display = 'none'; + const panel = document.querySelector('#' + BOTTOM_DRAWER); panel.style.width = '90px'; panel.style.left = ''; panel.style.right = '0px'; } function hideAllDisplayFrames() { - document.getElementById(LEFT_PANEL).style.display = 'none'; - document.getElementById(RIGHT_PANEL).style.display = 'none'; - document.getElementById(BOTTOM_DRAWER).style.display = 'none'; - document.getElementById(GROWLER_ALERTS).style.display = 'none'; + document.querySelector('#' + LEFT_PANEL).style.display = 'none'; + document.querySelector('#' + RIGHT_PANEL).style.display = 'none'; + document.querySelector('#' + BOTTOM_DRAWER).style.display = 'none'; + document.querySelector('#' + GROWLER_ALERTS).style.display = 'none'; } function showAllDisplayFrames() { - document.getElementById(LEFT_PANEL).style.display = ''; - document.getElementById(RIGHT_PANEL).style.display = ''; - document.getElementById(BOTTOM_DRAWER).style.display = ''; - document.getElementById(GROWLER_ALERTS).style.display = ''; + document.querySelector('#' + LEFT_PANEL).style.display = ''; + document.querySelector('#' + RIGHT_PANEL).style.display = ''; + document.querySelector('#' + BOTTOM_DRAWER).style.display = ''; + document.querySelector('#' + GROWLER_ALERTS).style.display = ''; } function refreshAllFrames() { - document.getElementById(LEFT_PANEL).src = document.getElementById(LEFT_PANEL).src; - document.getElementById(RIGHT_PANEL).src = document.getElementById(RIGHT_PANEL).src; - document.getElementById(MAIN_DISPLAY).src = document.getElementById(MAIN_DISPLAY).src; - document.getElementById(BOTTOM_DRAWER).src = document.getElementById(BOTTOM_DRAWER).src; - document.getElementById(GROWLER_ALERTS).src = document.getElementById(GROWLER_ALERTS).src; - document.getElementById(MANAGEMENT).src = document.getElementById(MANAGEMENT).src; + document.querySelector('#' + LEFT_PANEL).src = document.querySelector('#' + LEFT_PANEL).src; + document.querySelector('#' + RIGHT_PANEL).src = document.querySelector('#' + RIGHT_PANEL).src; + document.querySelector('#' + MAIN_DISPLAY).src = document.querySelector('#' + MAIN_DISPLAY).src; + document.querySelector('#' + BOTTOM_DRAWER).src = document.querySelector('#' + BOTTOM_DRAWER).src; + document.querySelector('#' + GROWLER_ALERTS).src = document.querySelector('#' + GROWLER_ALERTS).src; + document.querySelector('#' + MANAGEMENT).src = document.querySelector('#' + MANAGEMENT).src; } function refreshDisplayFrames() { - document.getElementById(LEFT_PANEL).src = document.getElementById(LEFT_PANEL).src; - document.getElementById(RIGHT_PANEL).src = document.getElementById(RIGHT_PANEL).src; - document.getElementById(MAIN_DISPLAY).src = document.getElementById(MAIN_DISPLAY).src; - document.getElementById(BOTTOM_DRAWER).src = document.getElementById(BOTTOM_DRAWER).src; - document.getElementById(GROWLER_ALERTS).src = document.getElementById(GROWLER_ALERTS).src; + document.querySelector('#' + LEFT_PANEL).src = document.querySelector('#' + LEFT_PANEL).src; + document.querySelector('#' + RIGHT_PANEL).src = document.querySelector('#' + RIGHT_PANEL).src; + document.querySelector('#' + MAIN_DISPLAY).src = document.querySelector('#' + MAIN_DISPLAY).src; + document.querySelector('#' + BOTTOM_DRAWER).src = document.querySelector('#' + BOTTOM_DRAWER).src; + document.querySelector('#' + GROWLER_ALERTS).src = document.querySelector('#' + GROWLER_ALERTS).src; } function refreshManagementFrame() { - document.getElementById(MANAGEMENT).src = document.getElementById(MANAGEMENT).src; + document.querySelector('#' + MANAGEMENT).src = document.querySelector('#' + MANAGEMENT).src; } /* Hide or show main iframe for popups and dialogs */ function showMainDisplay() { - document.getElementById(MAIN_DISPLAY).style.display = ''; + document.querySelector('#' + MAIN_DISPLAY).style.display = ''; } function hideMainDisplay() { - document.getElementById(MAIN_DISPLAY).style.display = 'none'; + document.querySelector('#' + MAIN_DISPLAY).style.display = 'none'; } function showBottomDrawer() { - document.getElementById(BOTTOM_DRAWER).style.height = '30%'; + document.querySelector('#' + BOTTOM_DRAWER).style.height = '30%'; } function hideBottomDrawer() { - document.getElementById(BOTTOM_DRAWER).style.height = '50px'; + document.querySelector('#' + BOTTOM_DRAWER).style.height = '50px'; } function expandManagement() { - document.getElementById(MANAGEMENT).style.width = '100%'; - document.getElementById(MANAGEMENT).style.height = '100%'; + document.querySelector('#' + MANAGEMENT).style.width = '100%'; + document.querySelector('#' + MANAGEMENT).style.height = '100%'; } function contractManagement() { - document.getElementById(MANAGEMENT).style.width = '0px'; - document.getElementById(MANAGEMENT).style.height = '0px'; + document.querySelector('#' + MANAGEMENT).style.width = '0px'; + document.querySelector('#' + MANAGEMENT).style.height = '0px'; } // TODO showEnable section - put this code in a separate file and inject ? @@ -281,13 +281,13 @@ const injection = (function () { } // Send to the management frame with the shared secret - const iframe = document.getElementById(MANAGEMENT); + const iframe = document.querySelector('#' + MANAGEMENT); iframe.contentWindow.postMessage({action: 'showEnable.count', tabId, count, sharedSecret: ZAP_SHARED_SECRET}, ZAP_HUD_FILES); } function highlightAlert(alert) { const id = alert.param; - let element = document.getElementById(id); + let element = document.querySelector('#' + id); if (!element) { const els = document.getElementsByName(id); for (const namedElement of els) { @@ -416,7 +416,7 @@ const injection = (function () { } // Send to the management frame with the shared secret - const iframe = document.getElementById(MANAGEMENT); + const iframe = document.querySelector('#' + MANAGEMENT); iframe.contentWindow.postMessage({action: 'showComments.count', tabId, count, suspicious: sus, sharedSecret: ZAP_SHARED_SECRET}, ZAP_HUD_FILES); } @@ -425,7 +425,7 @@ const injection = (function () { function showZapAlertInternal(alertId) { // Send to the management frame with the shared secret - const iframe = document.getElementById(MANAGEMENT); + const iframe = document.querySelector('#' + MANAGEMENT); iframe.contentWindow.postMessage({action: 'commonAlerts.showAlert', alertId, tabId, sharedSecret: ZAP_SHARED_SECRET}, ZAP_HUD_FILES); } @@ -623,8 +623,8 @@ const injection = (function () { if (zapReplaceOffset > 0) { // Hide the zapHudReplaceReq injected when resending a message in the browser // But don't loose any fragment - const fragment = window.location.hash.substr(1); - let origUrl = window.location.href.substring(0, zapReplaceOffset - 1); + const fragment = window.location.hash.slice(1); + let origUrl = window.location.href.slice(0, zapReplaceOffset - 1); if (fragment) { origUrl += '#' + fragment; } diff --git a/src/main/zapHomeFiles/hud/tools/activeScan.js b/src/main/zapHomeFiles/hud/tools/activeScan.js index 8d92df7bf..45cdb1a03 100644 --- a/src/main/zapHomeFiles/hud/tools/activeScan.js +++ b/src/main/zapHomeFiles/hud/tools/activeScan.js @@ -51,17 +51,15 @@ const ActiveScan = (function () { const config = {}; config.buttons = [{text: I18n.t('common_cancel'), id: 'cancel'}]; - if (!isRunning) { - if (!isInScope) { - config.text = DIALOG.START_ADD_SCOPE_1 + domain + DIALOG.START_ADD_SCOPE_2 + domain + DIALOG.START_ADD_SCOPE_3; - config.buttons.unshift({text: I18n.t('common_start'), id: 'start-add-to-scope'}); - } else { - config.text = DIALOG.START_1 + domain + DIALOG.START_2; - config.buttons.unshift({text: I18n.t('common_start'), id: 'start'}); - } - } else { + if (isRunning) { config.text = DIALOG.STOP_1 + tool.runningScope[0] + DIALOG.STOP_2; config.buttons.unshift({text: I18n.t('common_stop'), id: 'stop'}); + } else if (isInScope) { + config.text = DIALOG.START_1 + domain + DIALOG.START_2; + config.buttons.unshift({text: I18n.t('common_start'), id: 'start'}); + } else { + config.text = DIALOG.START_ADD_SCOPE_1 + domain + DIALOG.START_ADD_SCOPE_2 + domain + DIALOG.START_ADD_SCOPE_3; + config.buttons.unshift({text: I18n.t('common_start'), id: 'start-add-to-scope'}); } return config; @@ -150,10 +148,10 @@ const ActiveScan = (function () { function checkIsRunning(tabId) { return new Promise(resolve => { utils.loadTool(NAME).then(tool => { - if (tabId !== undefined) { - resolve(tool.runningTabId === tabId); - } else { + if (tabId === undefined) { resolve(tool.isRunning); + } else { + resolve(tool.runningTabId === tabId); } }); }); @@ -207,7 +205,7 @@ const ActiveScan = (function () { .catch(utils.errorHandler); } - self.addEventListener('activate', event => { + self.addEventListener('activate', _event => { initializeStorage(); registerForZapEvents(ACTIVE_SCAN_EVENT); }); diff --git a/src/main/zapHomeFiles/hud/tools/ajaxspider.js b/src/main/zapHomeFiles/hud/tools/ajaxspider.js index 054e543e9..937141635 100644 --- a/src/main/zapHomeFiles/hud/tools/ajaxspider.js +++ b/src/main/zapHomeFiles/hud/tools/ajaxspider.js @@ -49,19 +49,19 @@ const AjaxSpider = (function () { config.buttons = [{text: I18n.t('common_cancel'), id: 'cancel'}]; config.status = ''; - if (!isRunning) { + if (isRunning) { + config.status = 'running'; + config.text = DIALOG.STOP_1 + tool.runningScope[0] + DIALOG.STOP_2; + config.buttons.unshift({text: I18n.t('common_stop'), id: 'stop'}); + } else { config.status = 'stopped'; - if (!isInScope) { - config.text = DIALOG.START_ADD_SCOPE_1 + domain + DIALOG.START_ADD_SCOPE_2 + domain + DIALOG.START_ADD_SCOPE_3; - config.buttons.unshift({text: I18n.t('common_start'), id: 'start-add-to-scope'}); - } else { + if (isInScope) { config.text = DIALOG.START_1 + domain + DIALOG.START_2; config.buttons.unshift({text: I18n.t('common_start'), id: 'start'}); + } else { + config.text = DIALOG.START_ADD_SCOPE_1 + domain + DIALOG.START_ADD_SCOPE_2 + domain + DIALOG.START_ADD_SCOPE_3; + config.buttons.unshift({text: I18n.t('common_start'), id: 'start-add-to-scope'}); } - } else { - config.status = 'running'; - config.text = DIALOG.STOP_1 + tool.runningScope[0] + DIALOG.STOP_2; - config.buttons.unshift({text: I18n.t('common_stop'), id: 'stop'}); } return config; @@ -89,7 +89,7 @@ const AjaxSpider = (function () { utils.getUpgradedUrl(url) .then(upgradedUrl => { apiCallWithResponse('ajaxSpider', 'action', 'setOptionBrowserId', {String: browserId}); - apiCallWithResponse('ajaxSpider', 'action', 'scan', {url: upgradedUrl}).then(response => { + apiCallWithResponse('ajaxSpider', 'action', 'scan', {url: upgradedUrl}).then(_response => { spiderStarted(tabId, upgradedUrl); }) .catch(error => { @@ -114,7 +114,7 @@ const AjaxSpider = (function () { } function stopSpider(tabId) { - apiCallWithResponse('ajaxSpider', 'action', 'stop').then(response => { + apiCallWithResponse('ajaxSpider', 'action', 'stop').then(_response => { spiderStopped(tabId); }) .catch(error => { @@ -122,7 +122,7 @@ const AjaxSpider = (function () { }); } - function spiderStopped(tabId) { + function spiderStopped(_tabId) { utils.loadTool(NAME) .then(tool => { tool.isRunning = false; @@ -177,7 +177,7 @@ const AjaxSpider = (function () { .catch(utils.errorHandler); } - self.addEventListener('activate', event => { + self.addEventListener('activate', _event => { initializeStorage(); registerForZapEvents('org.zaproxy.zap.extension.spiderAjax.SpiderEventPublisher'); }); diff --git a/src/main/zapHomeFiles/hud/tools/attack.js b/src/main/zapHomeFiles/hud/tools/attack.js index 35164d3c9..bf317ad11 100644 --- a/src/main/zapHomeFiles/hud/tools/attack.js +++ b/src/main/zapHomeFiles/hud/tools/attack.js @@ -38,19 +38,19 @@ const Attack = (function () { .then(isRunning => { const config = {}; - if (!isRunning) { - config.text = DIALOG.OFF; + if (isRunning) { + config.text = DIALOG.ON; config.buttons = [ - {text: I18n.t('common_turn_on'), - id: 'turnon'}, + {text: I18n.t('common_turn_off'), + id: 'turnoff'}, {text: I18n.t('common_cancel'), id: 'cancel'} ]; } else { - config.text = DIALOG.ON; + config.text = DIALOG.OFF; config.buttons = [ - {text: I18n.t('common_turn_off'), - id: 'turnoff'}, + {text: I18n.t('common_turn_on'), + id: 'turnon'}, {text: I18n.t('common_cancel'), id: 'cancel'} ]; @@ -76,7 +76,7 @@ const Attack = (function () { utils.zapApiErrorDialog(tabId, error); throw error; }) - .then(response => { + .then(_response => { utils.loadTool(NAME) .then(tool => { tool.isRunning = true; @@ -98,7 +98,7 @@ const Attack = (function () { utils.zapApiErrorDialog(tabId, error); throw error; }) - .then(response => { + .then(_response => { utils.loadTool(NAME) .then(tool => { tool.isRunning = false; @@ -155,7 +155,7 @@ const Attack = (function () { .catch(utils.errorHandler); } - self.addEventListener('activate', event => { + self.addEventListener('activate', _event => { initializeStorage(); }); diff --git a/src/main/zapHomeFiles/hud/tools/break.js b/src/main/zapHomeFiles/hud/tools/break.js index 7ac12b134..3f9db1e35 100644 --- a/src/main/zapHomeFiles/hud/tools/break.js +++ b/src/main/zapHomeFiles/hud/tools/break.js @@ -47,7 +47,7 @@ const Break = (function () { utils.zapApiErrorDialog(tabId, error); throw error; }) - .then(response => { + .then(_response => { utils.loadTool(NAME) .then(tool => { tool.isRunning = true; @@ -68,7 +68,7 @@ const Break = (function () { utils.zapApiErrorDialog(tabId, error); throw error; }) - .then(response => { + .then(_response => { utils.loadTool(NAME) .then(tool => { tool.isRunning = false; @@ -136,15 +136,15 @@ const Break = (function () { utils.getAllClients('display') .then(clients => { - const isFirefox = this.navigator.userAgent.indexOf('Firefox') > -1; + const isFirefox = this.navigator.userAgent.includes('Firefox'); let r = false; if (isFirefox) { - for (let i = 0; i < clients.length; i++) { - if (clients[i].visibilityState === 'visible') { + clients.forEach(client => { + if (client.visibilityState === 'visible') { r = true; } - } + }); } else if (clients.length > 0) { r = true; } @@ -198,15 +198,15 @@ const Break = (function () { function showBreakWebSocketDisplay(data) { utils.getAllClients('display') .then(clients => { - const isFirefox = this.navigator.userAgent.indexOf('Firefox') > -1; + const isFirefox = this.navigator.userAgent.includes('Firefox'); let r = false; if (isFirefox) { - for (let i = 0; i < clients.length; i++) { - if (clients[i].visibilityState === 'visible') { + clients.forEach(client => { + if (client.visibilityState === 'visible') { r = true; } - } + }); } else if (clients.length > 0) { r = true; } @@ -266,7 +266,7 @@ const Break = (function () { .catch(utils.errorHandler); } - self.addEventListener('activate', event => { + self.addEventListener('activate', _event => { initializeStorage(); registerForZapEvents('org.zaproxy.zap.extension.brk.BreakEventPublisher'); }); diff --git a/src/main/zapHomeFiles/hud/tools/commonAlerts.js b/src/main/zapHomeFiles/hud/tools/commonAlerts.js index 346442d61..5ecdd593f 100644 --- a/src/main/zapHomeFiles/hud/tools/commonAlerts.js +++ b/src/main/zapHomeFiles/hud/tools/commonAlerts.js @@ -31,7 +31,7 @@ const CommonAlerts = (function () { return utils.messageAllTabs('growlerAlerts', {action: 'showGrowlerAlert', alert}); } - self.addEventListener('activate', event => { + self.addEventListener('activate', _event => { initializeStorage(); registerForZapEvents('org.zaproxy.zap.extension.alert.AlertEventPublisher'); registerForZapEvents('org.zaproxy.zap.extension.hud.HudEventPublisher'); @@ -85,7 +85,7 @@ const CommonAlerts = (function () { if (zapReplaceOffset > 0) { // Strip off the string used for resending in the browser // Will be preceded by ? or & - origTarget = origTarget.substring(0, zapReplaceOffset - 1); + origTarget = origTarget.slice(0, zapReplaceOffset - 1); } let target = origTarget; @@ -145,8 +145,8 @@ const CommonAlerts = (function () { for (const alertName in pageAlerts[alertRisk]) { if (Object.prototype.hasOwnProperty.call(pageAlerts[alertRisk], alertName)) { const reportedParameters = new Set(); - for (let i = 0; i < pageAlerts[alertRisk][alertName].length; i++) { - const alert = pageAlerts[alertRisk][alertName][i]; + + pageAlerts[alertRisk][alertName].forEach(alert => { if (alert.param.length > 0 && !reportedParameters.has(alert.param)) { reportedParameters.add(alert.param); utils.messageFrame(event.detail.tabId, 'management', { @@ -156,7 +156,7 @@ const CommonAlerts = (function () { risk: alert.risk, param: alert.param}); } - } + }); } } } @@ -196,7 +196,7 @@ const CommonAlerts = (function () { } const risk = event.detail.riskString; - const name = event.detail.name; + const {detail: {name}} = event; if (alertCache[targetDomain][risk][name] === undefined) { alertCache[targetDomain][risk][name] = {}; diff --git a/src/main/zapHomeFiles/hud/tools/history.js b/src/main/zapHomeFiles/hud/tools/history.js index 37737839c..9d8817e8b 100644 --- a/src/main/zapHomeFiles/hud/tools/history.js +++ b/src/main/zapHomeFiles/hud/tools/history.js @@ -142,7 +142,7 @@ const History = (function () { utils.writeTool(tool); }); - self.addEventListener('activate', event => { + self.addEventListener('activate', _event => { initializeStorage(); registerForZapEvents('org.parosproxy.paros.extension.history.ProxyListenerLogEventPublisher'); }); diff --git a/src/main/zapHomeFiles/hud/tools/htmlReport.js b/src/main/zapHomeFiles/hud/tools/htmlReport.js index 8f9da8a71..c26a91bb2 100644 --- a/src/main/zapHomeFiles/hud/tools/htmlReport.js +++ b/src/main/zapHomeFiles/hud/tools/htmlReport.js @@ -43,7 +43,7 @@ const HtmlReport = (function () { .catch(utils.errorHandler); } - self.addEventListener('activate', event => { + self.addEventListener('activate', _event => { initializeStorage(); }); diff --git a/src/main/zapHomeFiles/hud/tools/hudErrors.js b/src/main/zapHomeFiles/hud/tools/hudErrors.js index b84468f20..fa864c06e 100644 --- a/src/main/zapHomeFiles/hud/tools/hudErrors.js +++ b/src/main/zapHomeFiles/hud/tools/hudErrors.js @@ -82,7 +82,7 @@ const HudErrors = (function () { .catch(utils.errorHandler); } - self.addEventListener('activate', event => { + self.addEventListener('activate', _event => { initializeStorage(); }); diff --git a/src/main/zapHomeFiles/hud/tools/pageAlertsHigh.js b/src/main/zapHomeFiles/hud/tools/pageAlertsHigh.js index 2f152d0ad..bfa70bb64 100644 --- a/src/main/zapHomeFiles/hud/tools/pageAlertsHigh.js +++ b/src/main/zapHomeFiles/hud/tools/pageAlertsHigh.js @@ -42,7 +42,7 @@ const PageAlertsHigh = (function () { alertUtils.showOptions(tabId, NAME, LABEL); } - self.addEventListener('activate', event => { + self.addEventListener('activate', _event => { initializeStorage(); }); diff --git a/src/main/zapHomeFiles/hud/tools/pageAlertsInformational.js b/src/main/zapHomeFiles/hud/tools/pageAlertsInformational.js index d4d5cb805..f608ae7e6 100644 --- a/src/main/zapHomeFiles/hud/tools/pageAlertsInformational.js +++ b/src/main/zapHomeFiles/hud/tools/pageAlertsInformational.js @@ -43,7 +43,7 @@ const PageAlertsInformational = (function () { alertUtils.showOptions(tabId, NAME, LABEL); } - self.addEventListener('activate', event => { + self.addEventListener('activate', _event => { initializeStorage(); }); diff --git a/src/main/zapHomeFiles/hud/tools/pageAlertsLow.js b/src/main/zapHomeFiles/hud/tools/pageAlertsLow.js index 1b2133009..a959ff5bb 100644 --- a/src/main/zapHomeFiles/hud/tools/pageAlertsLow.js +++ b/src/main/zapHomeFiles/hud/tools/pageAlertsLow.js @@ -42,7 +42,7 @@ const PageAlertsLow = (function () { alertUtils.showOptions(tabId, NAME, LABEL); } - self.addEventListener('activate', event => { + self.addEventListener('activate', _event => { initializeStorage(); }); diff --git a/src/main/zapHomeFiles/hud/tools/pageAlertsMedium.js b/src/main/zapHomeFiles/hud/tools/pageAlertsMedium.js index 94e458fd3..4c3fee740 100644 --- a/src/main/zapHomeFiles/hud/tools/pageAlertsMedium.js +++ b/src/main/zapHomeFiles/hud/tools/pageAlertsMedium.js @@ -42,7 +42,7 @@ const PageAlertsMedium = (function () { alertUtils.showOptions(tabId, NAME, LABEL); } - self.addEventListener('activate', event => { + self.addEventListener('activate', _event => { initializeStorage(); }); diff --git a/src/main/zapHomeFiles/hud/tools/scope.js b/src/main/zapHomeFiles/hud/tools/scope.js index 829ca0951..4670dbb78 100644 --- a/src/main/zapHomeFiles/hud/tools/scope.js +++ b/src/main/zapHomeFiles/hud/tools/scope.js @@ -39,20 +39,20 @@ const Scope = (function () { .then(isInScope => { const config = {}; - if (!isInScope) { - config.title = LABEL; - config.text = DIALOG.OUT; + if (isInScope) { + config.text = DIALOG.IN; config.buttons = [ - {text: I18n.t('common_add'), - id: 'add'}, + {text: I18n.t('common_remove'), + id: 'remove'}, {text: I18n.t('common_cancel'), id: 'cancel'} ]; } else { - config.text = DIALOG.IN; + config.title = LABEL; + config.text = DIALOG.OUT; config.buttons = [ - {text: I18n.t('common_remove'), - id: 'remove'}, + {text: I18n.t('common_add'), + id: 'add'}, {text: I18n.t('common_cancel'), id: 'cancel'} ]; @@ -107,7 +107,7 @@ const Scope = (function () { utils.zapApiErrorDialog(tabId, error); throw error; }) - .then(response => { + .then(_response => { utils.messageAllTabs(tool.panel, {action: 'broadcastUpdate', context: {domain}, tool: {name: NAME, data: DATA.IN, icon: ICONS.IN, label: LABEL}}); return utils.writeTool(tool); }) @@ -125,7 +125,7 @@ const Scope = (function () { utils.zapApiErrorDialog(tabId, error); throw error; }) - .then(response => { + .then(_response => { // Remove from list and save utils.loadTool(NAME) .then(tool => { @@ -169,7 +169,7 @@ const Scope = (function () { .catch(utils.errorHandler); } - self.addEventListener('activate', event => { + self.addEventListener('activate', _event => { initializeStorage(); }); diff --git a/src/main/zapHomeFiles/hud/tools/showComments.js b/src/main/zapHomeFiles/hud/tools/showComments.js index 1c46f33a1..d3340937a 100644 --- a/src/main/zapHomeFiles/hud/tools/showComments.js +++ b/src/main/zapHomeFiles/hud/tools/showComments.js @@ -46,10 +46,10 @@ const ShowComments = (function () { } function switchState(tabId) { - if (!loc.isRunning) { - switchOn(tabId); - } else { + if (loc.isRunning) { switchOff(tabId); + } else { + switchOn(tabId); } } @@ -129,7 +129,7 @@ const ShowComments = (function () { .catch(utils.errorHandler); } - self.addEventListener('activate', event => { + self.addEventListener('activate', _event => { initializeStorage(); }); diff --git a/src/main/zapHomeFiles/hud/tools/showEnable.js b/src/main/zapHomeFiles/hud/tools/showEnable.js index 6b8a816f2..c12138578 100644 --- a/src/main/zapHomeFiles/hud/tools/showEnable.js +++ b/src/main/zapHomeFiles/hud/tools/showEnable.js @@ -39,10 +39,10 @@ const ShowEnable = (function () { function switchState() { checkIsRunning() .then(isRunning => { - if (!isRunning) { - switchOn(); - } else { + if (isRunning) { switchOff(); + } else { + switchOn(); } }) .catch(utils.errorHandler); @@ -106,7 +106,7 @@ const ShowEnable = (function () { .catch(utils.errorHandler); } - self.addEventListener('activate', event => { + self.addEventListener('activate', _event => { initializeStorage(); }); diff --git a/src/main/zapHomeFiles/hud/tools/siteAlertsHigh.js b/src/main/zapHomeFiles/hud/tools/siteAlertsHigh.js index 84e415732..c5eb274d8 100644 --- a/src/main/zapHomeFiles/hud/tools/siteAlertsHigh.js +++ b/src/main/zapHomeFiles/hud/tools/siteAlertsHigh.js @@ -42,7 +42,7 @@ const SiteAlertsHigh = (function () { alertUtils.showOptions(tabId, NAME, LABEL); } - self.addEventListener('activate', event => { + self.addEventListener('activate', _event => { initializeStorage(); }); diff --git a/src/main/zapHomeFiles/hud/tools/siteAlertsInformational.js b/src/main/zapHomeFiles/hud/tools/siteAlertsInformational.js index 8c8573c16..96a304109 100644 --- a/src/main/zapHomeFiles/hud/tools/siteAlertsInformational.js +++ b/src/main/zapHomeFiles/hud/tools/siteAlertsInformational.js @@ -42,7 +42,7 @@ const SiteAlertsInformational = (function () { alertUtils.showOptions(tabId, NAME, LABEL); } - self.addEventListener('activate', event => { + self.addEventListener('activate', _event => { initializeStorage(); }); diff --git a/src/main/zapHomeFiles/hud/tools/siteAlertsLow.js b/src/main/zapHomeFiles/hud/tools/siteAlertsLow.js index a91854bcd..7021a5f79 100644 --- a/src/main/zapHomeFiles/hud/tools/siteAlertsLow.js +++ b/src/main/zapHomeFiles/hud/tools/siteAlertsLow.js @@ -42,7 +42,7 @@ const SiteAlertsLow = (function () { alertUtils.showOptions(tabId, NAME, LABEL); } - self.addEventListener('activate', event => { + self.addEventListener('activate', _event => { initializeStorage(); }); diff --git a/src/main/zapHomeFiles/hud/tools/siteAlertsMedium.js b/src/main/zapHomeFiles/hud/tools/siteAlertsMedium.js index 7320dd505..8aabc47c3 100644 --- a/src/main/zapHomeFiles/hud/tools/siteAlertsMedium.js +++ b/src/main/zapHomeFiles/hud/tools/siteAlertsMedium.js @@ -42,7 +42,7 @@ const SiteAlertsMedium = (function () { alertUtils.showOptions(tabId, NAME, LABEL); } - self.addEventListener('activate', event => { + self.addEventListener('activate', _event => { initializeStorage(); }); diff --git a/src/main/zapHomeFiles/hud/tools/siteTree.js b/src/main/zapHomeFiles/hud/tools/siteTree.js index f11d2d533..985436417 100644 --- a/src/main/zapHomeFiles/hud/tools/siteTree.js +++ b/src/main/zapHomeFiles/hud/tools/siteTree.js @@ -53,13 +53,13 @@ const SiteTree = (function () { function getTool(port) { utils.loadTool(NAME) - .then(tool => { + .then(_tool => { port.postMessage({label: LABEL, data: DATA.SITES, icon: ICONS.WORLD}); }) .catch(utils.errorHandler); } - self.addEventListener('activate', event => { + self.addEventListener('activate', _event => { initializeStorage(); }); diff --git a/src/main/zapHomeFiles/hud/tools/spider.js b/src/main/zapHomeFiles/hud/tools/spider.js index b11c823a3..f57c35343 100644 --- a/src/main/zapHomeFiles/hud/tools/spider.js +++ b/src/main/zapHomeFiles/hud/tools/spider.js @@ -48,17 +48,15 @@ const Spider = (function () { const config = {}; config.buttons = [{text: I18n.t('common_cancel'), id: 'cancel'}]; - if (!isRunning) { - if (!isInScope) { - config.text = DIALOG.START_ADD_SCOPE_1 + domain + DIALOG.START_ADD_SCOPE_2 + domain + DIALOG.START_ADD_SCOPE_3; - config.buttons.unshift({text: I18n.t('common_start'), id: 'start-add-to-scope'}); - } else { - config.text = DIALOG.START_1 + domain + DIALOG.START_2; - config.buttons.unshift({text: I18n.t('common_start'), id: 'start'}); - } - } else { + if (isRunning) { config.text = DIALOG.STOP_1 + tool.runningScope[0] + DIALOG.STOP_2; config.buttons.unshift({text: I18n.t('common_stop'), id: 'stop'}); + } else if (isInScope) { + config.text = DIALOG.START_1 + domain + DIALOG.START_2; + config.buttons.unshift({text: I18n.t('common_start'), id: 'start'}); + } else { + config.text = DIALOG.START_ADD_SCOPE_1 + domain + DIALOG.START_ADD_SCOPE_2 + domain + DIALOG.START_ADD_SCOPE_3; + config.buttons.unshift({text: I18n.t('common_start'), id: 'start-add-to-scope'}); } return config; @@ -85,7 +83,7 @@ const Spider = (function () { function startSpider(tabId, domain) { utils.getUpgradedDomain(domain) .then(upgradedDomain => { - apiCallWithResponse('spider', 'action', 'scan', {url: upgradedDomain}).then(response => { + apiCallWithResponse('spider', 'action', 'scan', {url: upgradedDomain}).then(_response => { spiderStarted(tabId, domain); }) .catch(error => { @@ -110,7 +108,7 @@ const Spider = (function () { } function stopSpider(tabId) { - apiCallWithResponse('spider', 'action', 'stop').then(response => { + apiCallWithResponse('spider', 'action', 'stop').then(_response => { spiderStopped(tabId); }) .catch(error => { @@ -118,7 +116,7 @@ const Spider = (function () { }); } - function spiderStopped(tabId) { + function spiderStopped(_tabId) { utils.loadTool(NAME) .then(tool => { tool.isRunning = false; @@ -200,7 +198,7 @@ const Spider = (function () { } } - self.addEventListener('activate', event => { + self.addEventListener('activate', _event => { initializeStorage(); // Events up to 2.11.1 registerForZapEvents('org.zaproxy.zap.extension.spider.SpiderEventPublisher'); diff --git a/src/main/zapHomeFiles/hud/tools/toggleScript.js b/src/main/zapHomeFiles/hud/tools/toggleScript.js index adcb3e7ca..a332f690c 100644 --- a/src/main/zapHomeFiles/hud/tools/toggleScript.js +++ b/src/main/zapHomeFiles/hud/tools/toggleScript.js @@ -147,7 +147,7 @@ const ToggleScript = (function () { .catch(utils.errorHandler); } - self.addEventListener('activate', event => { + self.addEventListener('activate', _event => { initializeStorage(); }); diff --git a/src/main/zapHomeFiles/hud/tools/utils/alertUtils.js b/src/main/zapHomeFiles/hud/tools/utils/alertUtils.js index 0b6c9826d..e87cb9e39 100644 --- a/src/main/zapHomeFiles/hud/tools/utils/alertUtils.js +++ b/src/main/zapHomeFiles/hud/tools/utils/alertUtils.js @@ -1,3 +1,4 @@ +// eslint-disable-next-line no-unused-vars const alertUtils = (function () { function showSiteAlerts(tabId, title, target, alertRisk) { // Note that theres no need to load any tool data here @@ -39,14 +40,14 @@ const alertUtils = (function () { function flattenAlerts(alerts) { const json = {}; - for (let i = 0; i < alerts.length; i++) { - const alert = alerts[i]; + + alerts.forEach(alert => { for (const key in alert) { if (Object.prototype.hasOwnProperty.call(alert, key)) { json[key] = alert[key]; } } - } + }); return json; } @@ -69,7 +70,7 @@ const alertUtils = (function () { if (target.indexOf('?') > 0) { // Remove any url params - target = target.substring(0, target.indexOf('?')); + target = target.slice(0, target.indexOf('?')); } return apiCallWithResponse('alert', 'view', 'alertsByRisk', {url: target, recurse: 'false'}); diff --git a/src/main/zapHomeFiles/hud/tools/websockets.js b/src/main/zapHomeFiles/hud/tools/websockets.js index 7e16eb46d..25bc558ab 100644 --- a/src/main/zapHomeFiles/hud/tools/websockets.js +++ b/src/main/zapHomeFiles/hud/tools/websockets.js @@ -99,7 +99,7 @@ const WebSockets = (function () { } }); - self.addEventListener('activate', event => { + self.addEventListener('activate', _event => { initializeStorage(); registerForZapEvents('org.zaproxy.zap.extension.websocket.WebSocketEventPublisher'); }); diff --git a/src/main/zapHomeFiles/hud/utils.js b/src/main/zapHomeFiles/hud/utils.js index 5ac5c3184..ce71b533c 100644 --- a/src/main/zapHomeFiles/hud/utils.js +++ b/src/main/zapHomeFiles/hud/utils.js @@ -3,8 +3,10 @@ const IS_HUD_INITIALIZED = 'isHudInitialized'; const IS_FIRST_TIME = 'isFirstTime'; const IS_SERVICEWORKER_REFRESHED = 'isServiceWorkerRefreshed'; +// eslint-disable-next-line no-unused-vars const LOG_OFF = 0; // Just use for setting the level, nothing will be logged const LOG_ERROR = 1; // Errors that should be addressed +// eslint-disable-next-line no-unused-vars const LOG_WARN = 2; // A potential problem const LOG_INFO = 3; // Significant but infrequent events const LOG_DEBUG = 4; // Relatively fine grain events which can help debug problems @@ -13,8 +15,7 @@ const LOG_STRS = ['OFF', 'ERROR', 'WARN', 'INFO', 'DEBUG', 'TRACE']; class NoClientIdError extends Error {} -/* exported utils */ - +// eslint-disable-next-line no-unused-vars const utils = (function () { /* * Utility Functions @@ -42,27 +43,27 @@ const utils = (function () { function parseRequestHeader(headerText) { const header = {}; - header.method = headerText.substring(0, headerText.indexOf(' ')); - headerText = headerText.substring(headerText.indexOf(' ') + 1); + header.method = headerText.slice(0, headerText.indexOf(' ')); + headerText = headerText.slice(headerText.indexOf(' ') + 1); - header.uri = headerText.substring(0, headerText.indexOf(' ')); - headerText = headerText.substring(headerText.indexOf(' ') + 1); + header.uri = headerText.slice(0, headerText.indexOf(' ')); + headerText = headerText.slice(headerText.indexOf(' ') + 1); - header.version = headerText.substring(0, headerText.indexOf('\r')); - headerText = headerText.substring(headerText.indexOf('\n') + 1); + header.version = headerText.slice(0, headerText.indexOf('\r')); + headerText = headerText.slice(headerText.indexOf('\n') + 1); header.fields = {}; while (headerText !== '') { - const field = headerText.substring(0, headerText.indexOf(':')); - headerText = headerText.substring(headerText.indexOf(':') + 2); + const field = headerText.slice(0, headerText.indexOf(':')); + headerText = headerText.slice(headerText.indexOf(':') + 2); let value; - if (headerText.indexOf('\n') < 0) { + if (headerText.includes('\n')) { + value = headerText.slice(0, headerText.indexOf('\n')); + headerText = headerText.slice(headerText.indexOf('\n') + 1); + } else { value = headerText; headerText = ''; - } else { - value = headerText.substring(0, headerText.indexOf('\n')); - headerText = headerText.substring(headerText.indexOf('\n') + 1); } header.fields[field] = value; @@ -77,22 +78,22 @@ const utils = (function () { function parseResponseHeader(headerText) { const header = {}; - header.version = headerText.substring(0, headerText.indexOf(' ')); - headerText = headerText.substring(headerText.indexOf(' ') + 1); + header.version = headerText.slice(0, headerText.indexOf(' ')); + headerText = headerText.slice(headerText.indexOf(' ') + 1); - header.status = headerText.substring(0, headerText.indexOf(' ')); - headerText = headerText.substring(headerText.indexOf(' ') + 1); + header.status = headerText.slice(0, headerText.indexOf(' ')); + headerText = headerText.slice(headerText.indexOf(' ') + 1); - header.reason = headerText.substring(0, headerText.indexOf(' ')); - headerText = headerText.substring(headerText.indexOf(' ') + 1); + header.reason = headerText.slice(0, headerText.indexOf(' ')); + headerText = headerText.slice(headerText.indexOf(' ') + 1); header.fields = {}; while (headerText !== '') { - const field = headerText.substring(0, headerText.indexOf(':')); - headerText = headerText.substring(headerText.indexOf(':') + 2); + const field = headerText.slice(0, headerText.indexOf(':')); + headerText = headerText.slice(headerText.indexOf(':') + 2); - const value = headerText.substring(0, headerText.indexOf('\n')); - headerText = headerText.substring(headerText.indexOf('\n') + 1); + const value = headerText.slice(0, headerText.indexOf('\n')); + headerText = headerText.slice(headerText.indexOf('\n') + 1); header.fields[field] = value; } @@ -124,7 +125,7 @@ const utils = (function () { } function hasScheme(url) { - return url.indexOf('://') > -1; + return url.includes('://'); } /* @@ -135,7 +136,7 @@ const utils = (function () { let end = url.indexOf('&', start); end = end === -1 ? url.length : end; - return url.substring(start, end); + return url.slice(start, end); } /* STORAGE */ @@ -151,7 +152,7 @@ const utils = (function () { * Initialize all of the info that will be stored in indexeddb. */ function initializeHUD(leftTools, rightTools, drawer) { - if (IS_DEV_MODE && leftTools.indexOf('hudErrors') < 0) { + if (IS_DEV_MODE && !leftTools.includes('hudErrors')) { // Always add the error tool in dev mode leftTools.push('hudErrors'); } @@ -194,8 +195,8 @@ const utils = (function () { function setDefaultTools(leftTools, rightTools) { const promises = []; - for (let i = 0; i < leftTools.length; i++) { - loadTool(leftTools[i]) + leftTools.forEach((leftTool, i) => { + loadTool(leftTool) .then(tool => { if (!tool) { log(LOG_ERROR, 'utils.setDefaultTools', 'Failed to load tool.', tool.name); @@ -209,10 +210,10 @@ const utils = (function () { return writeTool(tool); }) .catch(errorHandler); - } + }); - for (let i = 0; i < rightTools.length; i++) { - loadTool(rightTools[i]) + rightTools.forEach((rightTool, i) => { + loadTool(rightTool) .then(tool => { if (!tool) { log(LOG_ERROR, 'utils.setDefaultTools', 'Failed to load tool.', tool.name); @@ -226,7 +227,7 @@ const utils = (function () { return writeTool(tool); }) .catch(errorHandler); - } + }); return Promise.all(promises); } @@ -429,8 +430,7 @@ const utils = (function () { function messageFrame(tabId, frameId, message) { return clients.matchAll({includeUncontrolled: true}) .then(clients => { - for (let i = 0; i < clients.length; i++) { - const client = clients[i]; + clients.forEach(client => { const parameters = new URL(client.url).searchParams; const tid = parameters.get('tabId'); @@ -439,7 +439,7 @@ const utils = (function () { if (tid === tabId && fid === frameId) { return client; } - } + }); throw new NoClientIdError('Could not find a ClientId for tabId: ' + tabId + ', frameId: ' + frameId); }) @@ -468,8 +468,7 @@ const utils = (function () { .then(clients => { const frameClients = []; - for (let i = 0; i < clients.length; i++) { - const client = clients[i]; + clients.forEach(client => { const parameters = new URL(client.url).searchParams; const fid = parameters.get('frameId'); @@ -477,7 +476,7 @@ const utils = (function () { if (fid === frameId) { frameClients.push(client); } - } + }); if (frameClients.length === 0) { log(LOG_DEBUG, 'utils.messageAllTabs', 'Could not find any clients for frameId: ' + frameId, message); @@ -488,9 +487,7 @@ const utils = (function () { }) .then(clients => { return new Promise(((resolve, reject) => { - for (let i = 0; i < clients.length; i++) { - const client = clients[i]; - + clients.forEach(client => { const channel = new MessageChannel(); channel.port1.start(); channel.port2.start(); @@ -504,7 +501,7 @@ const utils = (function () { }); client.postMessage(message, [channel.port2]); - } + }); })); }) .catch(errorHandler); @@ -523,8 +520,7 @@ const utils = (function () { .then(clients => { const frameClients = []; - for (let i = 0; i < clients.length; i++) { - const client = clients[i]; + clients.forEach(client => { const parameters = new URL(client.url).searchParams; const fid = parameters.get('frameId'); @@ -532,7 +528,7 @@ const utils = (function () { if (fid === frameId) { frameClients.push(client); } - } + }); return frameClients; }) @@ -654,7 +650,7 @@ const utils = (function () { scheme = 'http'; } - return scheme + '://' + domain + url.substring(url.indexOf(domain) + domain.length); + return scheme + '://' + domain + url.slice(url.indexOf(domain) + domain.length); }) .catch(errorHandler); } @@ -669,8 +665,8 @@ const utils = (function () { // Construct the stack trace const lines = error.stack.split('\n').slice(0, -1); lines.forEach(line => { - const functionName = line.substring(0, line.indexOf('/')); - const urlAndLineNo = line.substring(line.indexOf('http'), line.length - 1); + const functionName = line.slice(0, line.indexOf('/')); + const urlAndLineNo = line.slice(line.indexOf('http'), -1); const parts = urlAndLineNo.split(':'); let url = parts[0] + ':' + parts[1]; let lineNo = parts[2] + ':' + parts[3];