-
Notifications
You must be signed in to change notification settings - Fork 0
/
visa_export
91 lines (87 loc) · 3.5 KB
/
visa_export
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
// ==UserScript==
// @name Export VISA Transactions
// @namespace https://www.op.fi/
// @version 0.1
// @description Export VISA transactions from Osuuspankki to an http server (Google Spreadsheets for instance).
// @author Mikko Mattila
// @require http://ajax.googleapis.com/ajax/libs/jquery/1.8.3/jquery.min.js
// @match http://stackoverflow.com/questions/11275378/cross-domain-post-with-greasemonkey
// @grant GM_xmlhttpRequest
// ==/UserScript==
var delay = 0;
var outcome = "POST output:";
var usedRows = [];
function main() {
$(".Lomake tr").sort().each(function( index ) {
delay = delay + 1000; // Must delay the subsequent requests to make sure the data has been written to Sheets before next is sent.
var date = $(this).find(".Ensimmainen").text().replace(/( |\n|\t)/g,'').trim();
var payee = $(this).find(".EiAlleviivattu").text().replace(/( |\n|\t)/g,' ').trim();
var amount = $(this).find(".Nowrap").first().text().replace(/( |\n|\t)/g,'').trim().replace(",", ".");
var transaction = {
memo: "",
inflow: "",
outflow: "",
date: date,
payee: payee,
amount: amount
};
if ( parseFloat(transaction.amount) < 0 ) {
transaction.outflow = Math.abs(parseFloat(transaction.amount));
}
else {
transaction.inflow = Math.abs(parseFloat(transaction.amount));
}
//console.log( index + ": " + date + "/" + payee + "/" + inflow + "/" + outflow );
if ( parseFloat(transaction.amount) ){
var status = 0;
setTimeout(function() {
sendData(transaction, true);
}, delay);
}
});
}
function sendData(transaction, tryagain) {
//console.log("Sending: ");
//console.log(transaction);
GM_xmlhttpRequest ( {
method: "POST",
url: "https://script.google.com/macros/s/AKfycbwbZI9Bt6WH8EVWnwcvxd4JR3iaohSkhva5PdGKDkY/dev",
data: "Date=" + encodeURIComponent (transaction.date)
+ "&" + "Payee=" + encodeURIComponent (transaction.payee)
+ "&" + "Memo=" + encodeURIComponent (transaction.memo)
+ "&" + "Outflow=" + encodeURIComponent (transaction.outflow)
+ "&" + "Inflow=" + encodeURIComponent (transaction.inflow)
,
onload: function(req) {
var do_retry = false;
status = req.status;
console.log(req.status + ' ' + req.responseText);
try {
var resp = JSON.parse(req.responseText);
console.log(resp);
} catch(e) {
console.log
do_retry = true;
}
if ( do_retry || usedRows.indexOf(resp.row) > -1 ) {
if (tryagain) {
delay = delay + 1000;
setTimeout(function() {
console.log("Trying again once.");
sendData(transaction, false); // Try again.
}, delay);
}
else {
alert("ERROR: There was a collision! Double check the data.\nExecution aborted.");
}
return;
}
usedRows.push(resp.row);
$("body").prepend("OK / row " + resp.row + " / Total rows uploaded: " + usedRows.length + "<br/>");
},
headers: {
"Content-Type": "application/x-www-form-urlencoded"
}
} );
}
main();