Skip to content

Instantly share code, notes, and snippets.

@ulysses4ever
Last active December 23, 2016 17:50
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save ulysses4ever/c87f90b6a29034430dac to your computer and use it in GitHub Desktop.
Save ulysses4ever/c87f90b6a29034430dac to your computer and use it in GitHub Desktop.
Перенос данных из CVS-файла в СБРС мехмата ЮФУ
// ==UserScript==
// @name cvs2brs
// @description Перенос данных из CVS-файла в СБРС мехмата ЮФУ
// @author ulysses
// @license MIT
// @version 1.0
// @grant none
// @include https://grade.sfedu.ru/*
// ==/UserScript==
// Add CSS for drag/choose file zone
$("<style type='text/css'>#container { width: 480px;}\n #drop_zone { border: 1px dashed grey; width: 98%; height: 60px; text-align: center;}\n#drop_text { line-height: 60px; }\n#selector { text-align:center; height: 50px; vertical-align: top;}</style>").appendTo("head");
// Create drag/choose file zone
$('<div id="container"><div id="result">Здесь будут отображаться результаты добавления оценок</div><div id="drop_zone"><span id="drop_text">Перетащите CSV-файл с оценками сюда</span></div><div id="selector">Или выберите его здесь: <input type="file" id="files" name="files[]" multiple /></div></div>').prependTo(".main_layer");
// Check for the various File API support.
if (window.File && window.FileReader && window.FileList && window.Blob)
{
// Great success! All the File APIs are supported.
}
else
{
alert('The File APIs are not fully supported in this browser.');
}
// Transfer marks to table cells in a student-row
function addInfo(studentRow, studentMarks,log) {
//log.innerHTML = log.innerHTML + "студент: ";
studentRow.children().each(function (index) {
if (index === 0 || index + 1 >= studentMarks.length)
return;
$( this ).focusin();
var inp = $( this ).children().first();
if (inp)
$( inp ).attr("value", studentMarks[1 + index].toString());
$( this ).focusout();
//log.innerHTML = log.innerHTML + " оценка " + $( inp ).attr("value");
});
//log.innerHTML = log.innerHTML + ";<br />";
}
// Handlers for file add
function handleFileSelect(evt) {
evt.stopPropagation();
evt.preventDefault();
var files = evt.target.files || evt.dataTransfer.files; // FileList object.
var file = files[0];
// this creates the FileReader and reads stuff as text
var fr = new FileReader();
fr.onload = parse;
fr.readAsText(file);
// this is the function that actually parses the file
// and populates the table
function parse()
{
var res = document.getElementById('result');
res.innerHTML = "";
var students = fr.result.split('\n');
var c = 0;
var n = students.length - 1;
for (var i = 1; i < n; i++)
{
var st = students[i].split(',');
var stCell = $(".name:contains('" + st[1] + " " + st[0] + "')");
if (!stCell.attr("id")) // poor man's check for find success
continue;
addInfo(stCell.parent(), st,res);
c++;
}
res.innerHTML = res.innerHTML + '<span>Успешно добавлены оценки для ' + c + ' студентов из ' + (n-1) + ', что были в исходном файле</span>';
}
}
function handleDragOver(evt) {
evt.stopPropagation();
evt.preventDefault();
evt.dataTransfer.dropEffect = 'copy'; // Explicitly show this is a copy.
}
// Add handlers for file add
var dropZone = document.getElementById('drop_zone');
dropZone.addEventListener('dragover', handleDragOver, false);
dropZone.addEventListener('drop', handleFileSelect, false);
document.getElementById('files').addEventListener('change', handleFileSelect, false);
@ulysses4ever
Copy link
Author

После обновления СБРС осенью 2016 скрипт больше не работает :(

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment