{"size": 4921, "ext": "js", "max_stars_repo_path": "src/main/zapHomeFiles/hud/tools/attack.js", "max_stars_repo_name": "njmulsqb/zap-hud", "max_stars_repo_stars_event_min_datetime": null, "max_stars_repo_stars_event_max_datetime": null, "max_issues_repo_path": "src/main/zapHomeFiles/hud/tools/attack.js", "max_issues_repo_name": "njmulsqb/zap-hud", "max_issues_repo_issues_event_min_datetime": null, "max_issues_repo_issues_event_max_datetime": null, "max_forks_repo_path": "src/main/zapHomeFiles/hud/tools/attack.js", "max_forks_repo_name": "njmulsqb/zap-hud", "max_forks_repo_forks_event_min_datetime": null, "max_forks_repo_forks_event_max_datetime": null}
{"max_stars_count": null, "max_issues_count": null, "max_forks_count": null, "avg_line_length": 24.2413793103, "max_line_length": 187, "alphanum_fraction": 0.6143060354}
/*
 * Attack mode Tool
 *
 * Allows the user to switch attack mode on and off
 */

const Attack = (function () {
	// Constants
	const NAME = 'attack';
	const LABEL = I18n.t('attack_tool');
	const DATA = {};
	DATA.ON = I18n.t('common_on');
	DATA.OFF = I18n.t('common_off');
	const ICONS = {};
	ICONS.ON = 'crosshairs.png';
	ICONS.OFF = 'crosshairs-grey.png';
	const DIALOG = {};
	DIALOG.ON = I18n.t('attack_start');
	DIALOG.OFF = I18n.t('attack_stop');

	function initializeStorage() {
		const tool = {};
		tool.name = NAME;
		tool.label = LABEL;
		tool.data = DATA.OFF;
		tool.icon = ICONS.OFF;
		tool.isSelected = false;
		tool.panel = '';
		tool.position = 0;
		tool.isRunning = false;
		tool.attackingDomain = '';

		utils.writeTool(tool);
	}

	function showDialog(tabId, domain) {
		checkIsRunning(domain)
			.then(isRunning => {
				const config = {};

				if (!isRunning) {
					config.text = DIALOG.OFF;
					config.buttons = [
						{text: I18n.t('common_turn_on'),
							id: 'turnon'},
						{text: I18n.t('common_cancel'),
							id: 'cancel'}
					];
				} else {
					config.text = DIALOG.ON;
					config.buttons = [
						{text: I18n.t('common_turn_off'),
							id: 'turnoff'},
						{text: I18n.t('common_cancel'),
							id: 'cancel'}
					];
				}

				utils.messageFrame(tabId, 'display', {action: 'showDialog', config})
					.then(response => {
						// Handle button choice
						if (response.id === 'turnon') {
							turnOnAttackMode(tabId, domain);
						} else if (response.id === 'turnoff') {
							turnOffAttackMode(tabId);
						}
					})
					.catch(utils.errorHandler);
			})
			.catch(utils.errorHandler);
	}

	function turnOnAttackMode(tabId, domain) {
		apiCallWithResponse('core', 'action', 'setMode', {mode: 'attack'})
			.catch(error => {
				utils.zapApiErrorDialog(tabId, error);
				throw error;
			})
			.then(response => {
				utils.loadTool(NAME)
					.then(tool => {
						tool.isRunning = true;
						tool.attackingDomain = domain;
						tool.icon = ICONS.ON;
						tool.data = DATA.ON;

						utils.writeTool(tool);
						utils.messageAllTabs(tool.panel, {action: 'broadcastUpdate', context: {notDomain: domain}, tool: {name: NAME, label: LABEL, data: DATA.OFF, icon: ICONS.OFF}, isToolDisabled: true});
						utils.messageAllTabs(tool.panel, {action: 'broadcastUpdate', context: {domain}, tool: {name: NAME, label: LABEL, data: DATA.ON, icon: ICONS.ON}});
					})
					.catch(utils.errorHandler);
			});
	}

	function turnOffAttackMode(tabId) {
		apiCallWithResponse('core', 'action', 'setMode', {mode: 'standard'})
			.catch(error => {
				utils.zapApiErrorDialog(tabId, error);
				throw error;
			})
			.then(response => {
				utils.loadTool(NAME)
					.then(tool => {
						tool.isRunning = false;
						tool.attackingDomain = '';
						tool.icon = ICONS.OFF;
						tool.data = DATA.OFF;

						utils.writeTool(tool);
						utils.messageAllTabs(tool.panel, {action: 'broadcastUpdate', tool: {name: NAME, label: LABEL, data: DATA.OFF, icon: ICONS.OFF}, isToolDisabled: false});
					})
					.catch(utils.errorHandler);
			});
	}

	function checkIsRunning(domain) {
		return new Promise(resolve => {
			utils.loadTool(NAME)
				.then(tool => {
					resolve(tool.attackingDomain === domain);
				});
		});
	}

	function getTool(tabId, context, port) {
		utils.loadTool(NAME)
			.then(tool => {
				if (context.domain === tool.attackingDomain) {
					port.postMessage({label: LABEL, data: DATA.ON, icon: ICONS.ON});
				} else if (tool.isRunning) {
					port.postMessage({label: LABEL, data: DATA.OFF, icon: ICONS.OFF, isDisabled: true});
				} else {
					port.postMessage({label: LABEL, data: DATA.OFF, icon: ICONS.OFF});
				}
			})
			.catch(utils.errorHandler);
	}

	function showOptions(tabId) {
		const config = {};

		config.tool = NAME;
		config.toolLabel = LABEL;
		config.options = {remove: I18n.t('common_remove')};

		utils.messageFrame(tabId, 'display', {action: 'showButtonOptions', config})
			.then(response => {
				// Handle button choice
				if (response.id === 'remove') {
					utils.removeToolFromPanel(tabId, NAME);
				} else {
					// Cancel
				}
			})
			.catch(utils.errorHandler);
	}

	self.addEventListener('activate', event => {
		initializeStorage();
	});

	self.addEventListener('message', event => {
		const message = event.data;

		// Broadcasts
		switch (message.action) {
			case 'initializeTools':
				initializeStorage();
				break;

			default:
				break;
		}

		// Directed
		if (message.tool === NAME) {
			switch (message.action) {
				case 'buttonClicked':
					showDialog(message.tabId, message.domain);
					break;

				case 'buttonMenuClicked':
					showOptions(message.tabId);
					break;

				case 'getTool':
					getTool(message.tabId, message.context, event.ports[0]);
					break;

				default:
					break;
			}
		}
	});

	return {
		name: NAME,
		initialize: initializeStorage
	};
})();

self.tools[Attack.name] = Attack;
