Попробовал сделать: measuresConverter-test.js
Работает, но обновляется теперь очень долго (примерно минуту по Ctrl+F5) – мало того, что валют больше, так еще и целую страницу надо грузить.
Плюс аргумент -sortByCode и перевод на русский.
Posted: Sun Apr 10, 2011 7:04 pm
by se7h
Infocatcher
NaN?
Posted: Sun Apr 10, 2011 7:12 pm
by Infocatcher
se7h
Почему-то не удалось получить данные.
Или скрипт не пускают в сеть, или скачивается что-то не то. Или просто не грузится.
По ссылке http://exchange-rates.org/converter/BYR/USD/1/N в исходном коде есть
Infocatcher
In measuresConverter.js you can make extra buttons to:
- currency sorting by name/code,
- update/force update exchange rate.
And the message "Wait for download data" while update.
var code = "BYR";
var messages = [];
var url = getRequestURL(code);
messages.push("URL: " + url);
try {
var request = new ActiveXObject("Microsoft.XMLHTTP");
request.open("GET", url, false);
request.send(null);
var ratio = getRatioFromResponse(request.responseText);
if(!isNaN(ratio))
messages.push("Ratio for " + code + ": " + ratio);
else
messages.push("Can't get ratio");
}
catch(e) {
messages.push("Error: " + e.message);
}
WScript.Echo(messages.join("\n"));
function getRequestURL(code) {
if(code == "SKK") // Example: http://www.google.com/ig/calculator?hl=en&q=100EUR%3D%3FAUD
return "http://www.google.com/ig/calculator?hl=en&q=" + encodeURIComponent(1 + code + "=?USD");
// Example: http://exchange-rates.org/converter/BYR/USD/1/Y
return "http://exchange-rates.org/converter/" + code + "/USD/1/N";
}
function getRatioFromResponse(response) {
//WScript.Echo("Response:\n" + response);
// http://exchange-rates.org/converter/BYR/USD/1/N
// <span id="ctl00_M_lblToAmount">0.0003295</span>
if(/<span id="ctl00_M_lblToAmount">([^<>]+)<\/span>/.test(response)) {
messages.push("getRatioFromResponse: exchange-rates.org -> [" + RegExp.$1 + "]");
return Number(RegExp.$1.replace(/\s+/g, "").replace(/,/g, ""));
}
// http://www.google.com/ig/calculator?hl=en&q=100EUR%3D%3FAUD
// {lhs: "100 Euros",rhs: "135.069786 Australian dollars",error: "",icc: true}
// We don't have native JSON support :(
// And Google can return numbers in exponential format:
// 1.35069786 \x26#215; 10\x3csup\x3e-6\x3c/sup\x3e
// => 1.35069786 × 10<sup>-6</sup> => 1.35069786 × 10<sup>-6</sup>
// Or add spaces: 135 069.786
if(/[{,]\s*rhs:\s*"([\d\s]+(\.\d+)?)\s+[^"]+"\s*[,}]/.test(response)) {
messages.push("getRatioFromResponse: google.com -> [" + RegExp.$1 + "]");
return Number(RegExp.$1.replace(/\s+/g, "")); // Only 1.35069786 and 135 069.786 supported
}
return NaN;
}
?
Posted: Sun Apr 10, 2011 11:17 pm
by Infocatcher
KDJ
make extra buttons to:
- currency sorting by name/code,
- update/force update exchange rate.
, but Russian «Сортировать по имени» is too long for free space under OK/Convert/Cancel.
And the message "Wait for download data" while update.
Standard messages block code execution, now I just disable «Update» button.
And now asynchronous variant of update hangs sometimes. (Or server blocks frequent requests ).
P.S. Очень нужен аналог window.setTimeout().
Во-первых, можно сделать какую-нибудь анимацию (например, выполнение порциями и с отображением прогресса). А во-вторых, можно ограничить по времени AJAX-запросы. И вообще, асинхронный подход обычно лучше.
Posted: Mon Apr 11, 2011 6:20 am
by FeyFre
P.S. Очень нужен аналог window.setTimeout().
Очень извиняюсь что пишу в слепую(ибо не могу обновить скрипт по причине ucoz), но Вы почти у цели.