TuckerMar10 / NessusMassExporter

// ==UserScript==
// @name        NessusMassExporter
// @namespace   https://openuserjs.org/users/TuckerMar10
// @description Adds a "Download Selected" button to the scans overview page, allowing a user to easily download one or multiple nessus scans in either CSV or .Nessus format. Important note: The button will only appear after you've selected at least one scan. (Tags, ignore) Nessus .nessus export download csv nessus vulnerability scanner tennable 
// @include     https://localhost:8834/nessus6.html#
// @include     https://localhost:8834/nessus6.html#/*
// @include     https://localhost:8834/nessus6.html#/scans
// @include     https://localhost:8834/nessus6.html#/scans/*
// @include     https://127.0.0.1:8834/nessus6.html#/scans
// @include     https://127.0.0.1:8834/nessus6.html#/scans/*
// @version     2
// @grant       none
// ==/UserScript==
// 

/*
 * Author: William Martin
 * Email: Martin.william.t@gmail.com
 * Created 2/18/2015
 * 
 * Description: Quickly and easily export single or MULTIPLE nessus scans as a Nessus or CSV file at the same time - without a hassle
 * 
 * 
 * Honorable Mentions: A big thanks to McGladrey LLP (http://McGladrey.com) for the flexibility and encouragement for innovating
 * 
 * 
 * Note: If your nessus server is remote, or on different ports then you will have to modify the above @includes
 */




function startDownloadC()
{
	$(".checked").each(function()
	{
		var scanId = $(this).parent().parent().attr("data-id");
		var i = document.cookie.indexOf("nessus-tk=");
		token = document.cookie.substring(i+"nessus-tk=".length, document.cookie.indexOf(";", i));
		var goto = "https://" + window.location.host + "/scans/"+scanId+"/export";
		var fileId = "";
		//Request the start
		$.ajax({
			headers: {"X-Cookie": "token="+token+";"},
			url: goto, 
			type: "POST",
			dataType: 'json',
			data: {"format": 'csv'}
		}).done(function(data)
		{
			fileId = data.file;
			//Check the status
			goto = "https://" + window.location.host + "/scans/"+scanId+"/export/"+fileId+"/status";
			$.ajax({
				headers: {"X-Cookie": "token="+token+";"},
				url: goto, 
				type: "GET",
				dataType: 'json',
			}).done(function(data)
			{
				window.open("https://"+window.location.host + "/scans/"+scanId+"/export/"+fileId+"/download?token="+token, '_blank');
			});
		});
	});
}
function startDownloadN()
{
	$(".checked").each(function()
	{
		var scanId = $(this).parent().parent().attr("data-id");
		var i = document.cookie.indexOf("nessus-tk=");
		token = document.cookie.substring(i+"nessus-tk=".length, document.cookie.indexOf(";", i));
		var goto = "https://" + window.location.host + "/scans/"+scanId+"/export";
		var fileId = "";
		//Request the start
		$.ajax({
			headers: {"X-Cookie": "token="+token+";"},
			url: goto, 
			type: "POST",
			dataType: 'json',
			data: {"format": 'nessus'}
		}).done(function(data)
		{
			fileId = data.file;
			//Check the status
			goto = "https://" + window.location.host + "/scans/"+scanId+"/export/"+fileId+"/status";
			$.ajax({
				headers: {"X-Cookie": "token="+token+";"},
				url: goto, 
				type: "GET",
				dataType: 'json',
			}).done(function(data)
			{
				window.open("https://"+window.location.host + "/scans/"+scanId+"/export/"+fileId+"/download?token="+token, '_blank');
			});
		});
	});
}
function showButton()
{
	console.log("NME: Showing button");
	var newGuy = $('#scans-overview-menu').clone(true);
	$(newGuy).css("width", "150px");
	$(newGuy).attr("id", "DownloadAll");
	$(newGuy).find(">:first-child").text("Download Selected");
	$(newGuy).find("> ul > li").remove();
	var nDown = '<li id="nessusDown" style="display: block;"><div>.Nessus</li>';
	var cDown = '<li id="csvDown" style="display: block;">.CSV</li>';
	$(newGuy).find("> ul").append(nDown);
	$(newGuy).find("> ul").append(cDown);
	$("#scans-overview-menu").after(newGuy);
	$("#nessusDown").click(function(){startDownloadN();});
	$("#csvDown").click(function(){startDownloadC();});
	$(newGuy).show();
	return;
}
function hideButton()
{
	console.log("NME: Killing button");
	$("#DownloadAll").remove();
	return;
}


	console.log("NME: Main function loaded");
	$(document).on('click', '.checkbox', function() 
	{
		setTimeout(function(){
			if ($(".checked")[0] && $("#DownloadAll")[0]) {return;}
			if ($(".checked")[0]) {showButton(); return;}
			hideButton(); 
			return;
		}, 50);
	});
	$(document).on('click', '.select-all', function() 
	{
		setTimeout(function() {
			if ($(".checked")[0] && $("#DownloadAll")[0]) {return;}
			if ($(".checked")[0]) {showButton(); return;}
			hideButton(); 
			return;
		}, 50);
	});
	console.log("NME: Button hook set");