forked from fschaefer/ComunioPlusPlusDE
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.js
171 lines (144 loc) · 4.7 KB
/
main.js
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
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
/*
* ComunioPlusPlus DE
*
* Copyright (c) 2012, Florian Schäfer <[email protected]>
* All rights reserved
*
* Use of this source code is governed by a BSD-style license that can be
* found in the LICENSE file.
*
*/
function normalizeName(name) {
return name.match(/(?:\w\.\s)?(\w*)/)[1];
}
function get(urls, done, fail) {
var done = done || $.noop,
fail = fail || $.noop,
responses = {},
requests = urls.map(function (url, i) {
return $.get(url, function (response) { responses[i] = response });
});
function makeArray(obj) {
var result = [];
for (var prop in obj) {
if (obj.hasOwnProperty(prop)) {
result[prop] = obj[prop];
}
}
return result;
}
$.when.apply(null, requests)
.done(function () {
done(makeArray(responses));
})
.fail(function () {
fail();
});
}
function addInjury($targetTr, data, name, club, done) {
var done = done || $.noop,
tr = $(data).find('td:contains("' + name + '"):first').closest('tr'),
$tr = $(tr),
injured = !!( $tr.find('td img[title="' + club + '"]').length ),
injury = $tr.find('>td:eq(4)').text(),
until = $tr.find('>td:eq(6)').text(),
$td = $('<td/>', { 'align': 'center', 'css': { 'width': '24px' } }).appendTo($targetTr);
if (injured) {
$('<img/>', {
'src': chrome.extension.getURL('/images/redcross.gif'),
'title': injury + ' - ' + until,
'css': {
'heigth': '16px',
'width': '16px'
}
})
.appendTo($td);
}
done();
}
function addStats($targetTr, name, club) {
get([
'http://stats.comunio.de/search.php?name=' + name
], function (data) {
var $tr = $(data[0]).find('table tr td a:contains("' + name + '")').closest('tr').find('img[title="' + club + '"]').closest('tr'),
$img = $tr.find('td img.trend').clone(),
$td = $('<td/>', { 'align': 'center', 'css': { 'width': '24px' } }).appendTo($targetTr);
$img.css({
'background-image': "url(" + chrome.extension.getURL('/images/trends.png') + ")"
})
.appendTo($td);
});
}
function forEachPlayer(callback) {
$([
'.tablebox tr' /* player table in lineup.html */
, '.tablecontent03 tr' /* player table in exchangemarket.phtml */
].join(','))
.each(function () {
var $tr = $(this),
name = $(this).find('td:eq(0)').text().replace(/[\*\s]/g, ""), /* player name */
club = $(this).find('span.clubimg').attr("title"); /* club name */
callback($tr, name, club);
});
}
function formatNumber(number) {
var number = number + '',
x = number.split('.'),
x1 = x[0],
x2 = x.length > 1 ? '.' + x[1] : '',
regexp = /(\d+)(\d{3})/;
while (regexp.test(x1)) {
x1 = x1.replace(regexp, '$1' + '.' + '$2');
}
return x1 + x2;
}
function normalizeNumber(number) {
return parseInt(number.split('.').join(''));
}
function calculateBids() {
var bids = 0;
$('.tablecontent03 input[onkeyup]').each(function () {
var value = $(this).val();
if (value) {
bids += normalizeNumber(value);
}
});
return bids;
}
function getBudget() {
return normalizeNumber($('#userbudget').text().match(/[?\.\d]+/)[0]);
}
function doCalculation() {
var $calculation = $('#calculation');
if ($calculation.length === 0) {
$calculation = $('#title').clone().attr('id', 'calculation').addClass('contenttext').empty().insertAfter('#title');
}
var budget = getBudget(),
bids = calculateBids(),
newBalance = budget - bids;
$calculation.html([
'<p>' + formatNumber(budget) + '</p>'
, '<p>-' + formatNumber(bids) + '</p>'
, '<p>=' + formatNumber(newBalance) + '</p>'
].join(''));
}
var currentLocation = document.location.href;
if ( currentLocation.match("http://www.comunio.de/lineup.phtml")
|| currentLocation.match("http://www.comunio.de/exchangemarket.phtml") ) {
get([
'http://www.transfermarkt.de/1-bundesliga/verletztespieler/wettbewerb/L1'
], function (injuryData) {
forEachPlayer(function ($tr, name, club) {
addInjury($tr, injuryData[0], normalizeName(name), club, function () {
addStats($tr, name, club);
});
});
});
}
if (currentLocation.match("http://www.comunio.de/exchangemarket.phtml")) {
doCalculation();
$('.tablecontent03 input[onkeyup]')
.on('keyup, click, blur', function () {
doCalculation();
});
}