Skip to content

Instantly share code, notes, and snippets.

@MarZab
Last active July 28, 2020 13:25
Show Gist options
  • Save MarZab/9d62df31991cb72aec721f7fae08ba9e to your computer and use it in GitHub Desktop.
Save MarZab/9d62df31991cb72aec721f7fae08ba9e to your computer and use it in GitHub Desktop.
eDavki eKartica - Malenkost boljši izvoz
// ==UserScript==
// @name eDavki eKartica - Malenkost boljši izvoz
// @version 1.4
// @description Na eKartici portala eDavki doda nov gumb, "Alt. CSV", ki vrne UTF-8 CSV datoteko v standardni obliki datuma. Uporaba na lastno odgovornost.
// @author Marko Zabreznik
// @match https://edavki.durs.si/EdavkiPortal/[*]/PersonalPortal/CommonPages/Documents/eKartica.aspx?RepresentedTaxPayerProfileId=*&form=eKartica
// @grant none
// ==/UserScript==
(function() {
'use strict';
let form = jQuery("#ctl01");
let re_datum = /;([0-9]{1,2})\.([0-9]{1,2}).(20[0-9]{2});/g;
let davcna = /Davčna številka:[^0-9]+([0-9]+)/m.exec(jQuery("#tblEditControl").text());
davcna = (davcna) ? davcna[1] : '000000000';
function fetchCSV(cb) {
jQuery.ajax({
type: 'POST',
action: form.attr('action'),
data: form.serialize() + '&ctl00$ctl00$OpenMainContent$MainContent$btnToCsv=CSV',
success: function(data) {
cb(
// BOM for Excel
"\ufeff"+
// fix date
data.replace(re_datum, function(_, dan, mesec, leto) {
if (dan.length < 2) dan = '0'+dan;
if (mesec.length < 2) mesec = '0'+mesec;
return `;${leto}-${mesec}-${dan};`;
}));
},
beforeSend: function( xhr ) {
// fix encoding
xhr.overrideMimeType( "text/plain; charset=windows-1250" );
}
});
}
function download(filename, text) {
var element = document.createElement('a');
element.setAttribute('href', 'data:text/plain;charset=utf-8,' + encodeURIComponent(text));
element.setAttribute('download', filename);
element.style.display = 'none';
document.body.appendChild(element);
element.click();
document.body.removeChild(element);
}
jQuery('<span style="font-size:11px; cursor: pointer; margin-left:10px;color:#2171a0">Alt. CSV</span>').click(function() {
fetchCSV(function(data) {
// make file name from davcna and date
download(`eKartica-${davcna}-${jQuery.datepicker.formatDate('yy-mm-dd', new Date())}.csv`, data);
});
}).insertAfter('#OpenMainContent_MainContent_btnToCsv');
})();
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment