lx-scan
Version:
License eXtension to find 5-tuples of all installed packages: name, version, project home page, license (e. g. Apache v2, BSD) and required notice. It includes a GUI to edit information for each package and to enter information if necessary.
252 lines (229 loc) • 9.86 kB
JavaScript
(function (obj) {
obj.browserJS = function (params) {
// Active parts of de-jank-ifying the file browser
$(document).on("change", ".btn-file :file", function () {
var input = $(this),
numFiles = input.get(0).files ? input.get(0).files.length : 1,
label = input.val().replace(/\\/g, '/').replace(/.*\//, '');
input.trigger('fileselect', [numFiles, label]);
});
$(document).ready( function () {
$(".btn-file :file").on("fileselect", function (event,numFiles,label) {
document.getElementById("filetext").value = label;
});
});
obj.showbox = function () {
$("#scanmodal").modal("show")
};
// This function applies the user-defined information on the page to
// send the server a scanning message
obj.showspinny = function () {
document.getElementById("sbutton").href = "javascript: void(0)";
$("#errorbutton").hide();
var path = document.getElementById("path");
var fullpath = path.value;
showbox();
var message = {"path": fullpath,"name":document.getElementById("filename").value,"jfile":params.jref};
if (params.scantype)
message["scantype"] = params.scantype;
if(!document.getElementById("file").files)
document.getElementById("file").files = [];
if($("#diffbox").is(":visible") && document.getElementById("file").files[0]) {
var file = document.getElementById("file").files[0];
var reader = new FileReader();
reader.onload = function (e) {
var text = reader.result;
message["diff"] = text;
socket.emit("scanreq",message);
}
reader.readAsText(file);
} else if ($("#diffbox").is(":visible")) {
message["diff"] = "";
socket.emit("scanreq",message);
}
else {
socket.emit("scanreq",message);
}
};
// This block sets up how the client will react to scan results from the server
socket.on("error", function (error) {
if(obj.test_jsdom_debug)
return obj.debugErrorFunc(error.msg);
else {
$("#alerttext").text(error.msg);
$("#spinny").slideUp();
$("#errorbutton").slideDown();
$("#errorbutton").click(function() {
window.location = document.URL;
});
}
});
socket.on("scandone", function(jfile) {
if(obj.test_jsdom_debug)
return obj.debugScanFunc(jfile.msg);
window.location = "/?jfile=" + jfile.msg;
});
// The page needs to appear different depending on who the caller is;
// this function handles those modifications. It's run after the page loads
// so that all of the elements have loaded. This is different from the
// window.onload event since the window might load while some the elements
// still don't exist
obj.fixpage = function () {
document.getElementById("path").onkeydown = function(event) { save(event,"filename2") } ;
var fullpath = params.pathname;
document.getElementById("path").value = fullpath;
if(!/^.*[\/\\]$/.test(params.pathname) && !(params.filelist === "")) {
// Tests whether or not the path ends with a slash, adds one
// if it doesn't
fullpath += (!/^win.*$/i.test(window.navigator.oscpu) ? "/" : "\\");
}
var sbutton = document.getElementById('sbutton');
var calling = params.calling;
if(typeof calling.split("?")[1] === "undefined" ) {
sbutton.href = calling + "?jfile="+params.jref+"&verbose="+params.verb+"&type="+params.ftype+"&path="+ fullpath;
} else {
sbutton.href = calling + "&jfile="+params.jref+"&verbose="+params.verb+"&type="+params.ftype+"&path="+ fullpath;
}
document.getElementById("filediv").style.display = "";
$("#diffbox").hide();
$("#diffbutton").click(function () {
$("#diffbox").slideToggle();
});
$("#diffholder").click(function () {
window.location = $("#difflink").attr("href");
});
if(calling === "/scan?scan=true") {
sbutton.innerHTML = "Scan";
sbutton.onclick = showspinny;
$("#sholder").click(showspinny);
if(params.scantype==="") {
document.getElementById("filediv").style.display = "none";
document.getElementById("top_label").innerHTML = "Path to a folder to be scanned:";
document.getElementById("top_tip").setAttribute("data-content", "This should be a path to a directory containing a node_modules folder when you hit \"Scan\". If the path indicates the root folder of a node package (it has a package.json file in it) then that package will be included in the scan.");
if(!params.node_modules_check) {
$("#diffcontainer").hide();
$("#sholder").hide();
}
}
if(params.scantype==="python") {
document.getElementById("name_text").innerHTML = "Python package name: ";
document.getElementById("top_label").innerHTML = "Path to a directory containing Python binary: ";
document.getElementById("top_tip").setAttribute("data-content","If there's no Python binary in the folder indicated by this box, your global install base will be scanned instead.");
document.getElementById("bottom_tip").setAttribute("data-content","Leave this blank if you want to scan the entire install base of packages. If you don't know the name of your package, check the file setup.py in its root folder.");
}
return;
}
$("#diffcontainer").hide();
$("#sholder").click(function () {
$("#savemodal").modal("show");
});
sbutton.onclick = function(e) {e.preventDefault();};
$("#sbutton2").click(function () {
window.location = $("#sbutton").attr("href");
});
document.getElementById("filediv").style.display = "none";
$("#").click(function (e) {e.preventDefault();});
if(calling === "/open") {
sbutton.innerHTML = "Open";
document.getElementById("top_tip").setAttribute("data-content","You can type in the path to a folder here and hit enter to navigate to that folder.");
$("#sholder").click(function () {
window.location = sbutton.href;
});
return;
}
if(params.ftype === "html") {
document.getElementById("filediv").style.display = "none";
}
};
// This next block sets up functions that update the status of links on the
// page and other variables as the user inputs new information into the various
// page elements
obj.fillpath = function(input, value) {
var path = document.getElementById("path");
var confirm = document.getElementById("confirm");
var fullpath = params.pathname + value;
if(!/^.*\/$/.test(params.pathname) && !/^.*\\$/.test(params.pathname)) {
// Tests whether or not the path ends with a slash, adds one
// if it doesn't
if(/^win.*$/i.test(window.navigator.oscpu)) {
//Windows check
fullpath = params.pathname + "\\" + value;
} else {
fullpath = params.pathname + "/" + value;
}
}
if(typeof obj.oldselection !== "undefined") {
document.getElementById(obj.oldselection).style.border = "";
}
obj.oldselection = input.id;
input.style.border = "solid";
input.style.borderColor = "blue";
confirm.href = "/browse?jfile="+params.jref+"&verbose="+params.verb+"&type="+params.ftype+"&oldpath="+params.oldpath+"&scantype="+params.scantype+"&path=" + fullpath+"&caller="+params.calling;
var sbutton = document.getElementById('sbutton');
var calling = params.calling;
sbutton.href = calling + (typeof calling.split("?")[1]==="undefined" ? "?" : "&") + "jfile=" + params.jref;
if(params.oldpath)
sbutton.href += "&oldpath=" + params.oldpath;
if(params.ftype) {
sbutton.href += "&type=" + params.ftype;
}
if(params.ftype) {
if(params.ftype === "html")
sbutton.href+=("&verbose="+params.verb);
}
sbutton.href += "&path="+ fullpath
path.value = fullpath;
};
obj.fillpath2 = function (id) {
var sbutton = document.getElementById('sbutton');
var calling = params.calling;
var path = document.getElementById('path');
var fullpath = path.value;
if(!/^.*\/$/.test(fullpath) && !/^.*\\$/.test(fullpath)) {
// Tests whether or not the path ends with a slash, adds one
// if it doesn't
if(/^win.*$/i.test(window.navigator.oscpu)) {
fullpath += "\\";
} else {
fullpath += "/";
}
}
if(!(document.getElementById(id).value==="") && document.getElementById(id).value) {
fullpath+= document.getElementById(id).value;
}
document.getElementById('confirm').href = "/browse?jfile="+params.jref+"&verbose="+params.verb+"&type="+params.ftype+"&oldpath="+params.oldpath+"&scantype="+params.scantype+"&caller="+params.calling+"&path=" + fullpath;
sbutton.href = calling + (typeof calling.split("?")[1]==="undefined" ? "?" : "&") + "jfile=" + params.jref;
if(params.oldpath)
sbutton.href += "&oldpath=" + params.oldpath;
if(params.ftype) {
sbutton.href += "&type=" + params.ftype;
if(params.ftype === "html") {
sbutton.href+=("&verbose="+params.verb);
}
}
sbutton.href += "&path="+ fullpath
};
obj.followlink = function (input, value) {
fillpath(input,value);
window.location = document.getElementById('confirm').href;
};
obj.save = function (event,id) {
fillpath2(id);
var kcode = (event.which) ? event.which : event.keyCode;
if(kcode === 13) {
if(event.preventDefault)
event.preventDefault();
if(/save/.test(params.calling)) {
if(document.activeElement.id === "path") {
window.location = document.getElementById("confirm").href
} else {
window.location = document.getElementById("sbutton").href;
}
return;
}
var test = (document.getElementById("filediv").style.display !== "none" && params.scantype==="") ? document.getElementById("sbutton").href : document.getElementById("confirm").href;
window.location = test;
};
}
};
})(this);