Showing posts with label jscript. Show all posts
Showing posts with label jscript. Show all posts

Saturday, 29 September 2012

How to concatenate multiple lines in console (bash, cmd)

If for some reasons you need to have concatenated lines try one of the recipes below.

Sunday, 25 March 2012

Universal way to identify your external IP and more

There are many such kind of resources in Internet but this is the best. It allows the numerous structured data and it has nice name. Meet http://ifconfig.me/.
Command line interface
Using curl
curl ifconfig.me
or wget
wget -qO - ifconfig.me/ip
JScript
JS
var xmlhttp = new ActiveXObject('Microsoft.XMLHTTP');
xmlhttp.open('GET', 'http://ifconfig.me/ip', false);
xmlhttp.send();
 
var ip = xmlhttp.responseText;
JS+XML
var xmlhttp = new ActiveXObject('Microsoft.XMLHTTP');
xmlhttp.open('GET', 'http://ifconfig.me/all.xml', false);
xmlhttp.send();
 
var ip = xmlhttp.responseXml.getElementsByTagName('ip_addr')[0].text;
Having JSON library you can read and parse JSON data:
var xmlhttp = new ActiveXObject('Microsoft.XMLHTTP');
xmlhttp.open('GET', 'http://ifconfig.me/all.json', false);
xmlhttp.send();
 
var data = JSON.parse(xmlhttp.responseText);
var ip = data.ip_addr;
VBScript
Dim xmlhttp, ip
 
Set xmlhttp = CreateObject("Microsoft.XMLHTTP")
xmlhttp.open "GET", "http://ifconfig.me/ip", False
xmlhttp.send
 
ip = xmlhttp.responseText

Thursday, 5 January 2012

Artist/Album Info in Foobar2000

foobar2000 is perfect mediaplayer for Windows platforms. It is kind of those applications whose functionality is extendable simply adding plugins. One of these finest plugins is Run services that allows to run external applications and scrpts from within foobar2000.

I use Run services to view/edit information about albums or artists stored in plain text files. Let's consider how it is possible.

Monday, 10 October 2011

Версии Internet Explorer

Нечасто, но иногда на странице требуется программно определить какой же браузер и какой версии используется. Для этого существует несколько способов определения.

Sunday, 1 May 2011

Обмен данными и объектами между скриптами

Однажды на Сером форуме шло обсуждение как можно осуществить обмен данными между скриптами. Идея основана на создании нового экземпляра, наследуемого от интерфейса IWebBrowser2 (ссылка: IWebBrowser2 Interface). Объект хранит в себе данные даже после завершения всех скриптов, практически как буфер обмена, и может служить как средство обмена данными и объектами между скриптами.

Monday, 29 March 2010

Windows Scripting Command Interpreter

Консольный интерпретатор JS/VBS-скриптов в автоматическом и интерактивном режимах.

Monday, 14 December 2009

MsgBox / InputBox in JScript

Unfortunately JScript does not have several useful features available in VBScript. The most famous things are MsgBox and InputBox. The following code makes them available in JScript too.

Thursday, 9 July 2009

JS2BAT converter 2

Earlier to embed javascript codes into batch files the JS2BAT tool has ben implemented with batch features only. In this topic the same tool has been implemented in pure javascript. As opposed to the previous version the present one is the more functional and has optional parmeters. Previously the js2bat.js script has been implemented in javascript. After that it has been converted to the js2bat.bat batch script by itself with the following command:
cscript js2bat.js js2bat.js

Tuesday, 7 July 2009

JS2BAT converter

This script allows you to embed Javascript codes into the batch-script immediately. So you can stop to worry about creating of an additional batch-wrapper or lost of some specific parameters. Perl distribution has the same functional script pl2bat but for wrapping of perl-scripts with batch-scripts. I am not author of this idea, i just implemented this feature as the standalone tool. The main feature is to write the prolog code that is valid both in a batch and javascript. When launching the batch it runs as batch and calls the javascript interpreter with the same file as javascript file. But the second one skips this prolog as valid javascript comments.

Friday, 27 March 2009

JavaScript / JScript Benchmark (en)

Beginning

I will not lie - I do not like to estimate a performance of javascript code. This procedure provides for the main project a lot of "unnecessary" and "senseless" code:
var n = 1000; 
var start = (new Date()).getTime();/
for (var i = 0 ; i < n; i++) {
    // bla-bla-bla ...
}
var stop = (new Date()).getTime();
var duration = stop - start;
var average = duration / n; 
document.writeln(average);
I can presume that all the familiar lines such as these. I may be interested in the performance of a function or method, and possibly, some of program. But I do not want to pollute my working project by some kind of garbage, such as this. Also I do not want this for function that I am trying to estimate - what algorithm is better.
When I finally tired of it - I decided to develop a Benchmark. The objective was to estimate the performance of not only function, but any piece of code, beginning from the line n and ending by line m.

Motivation

Starting this work, I understood - it is possible that this code has been realized already and successfully. Nevertheless I started ab ovo. When finishing I decided to compare my solution with solutions of my predecessors (links are in the bottom of the article). I will not overmodest - despite of some similarities, my solution is better. The argumentation is following - despite of similarity my code is universal essentially.
Then I will show - that is, and how it can be very convenient and simple tool. Partially similarities and differences of the actual solution and it’s predecessors will be consider.

Features of OO-approach in JavaScript

Any piece of code can be a body of nameless function (this is truth for JavaScript especially taking in account it’s special OO realization). E.g.:
var arr = [100, 200, 300, 400]; 
    var result = 0; 
    for (var i = 0; i < arr.length; i++) {
        result += arr[i];
    }
can be transformed to the next
var arr = [100, 200, 300, 400]; 
((function() 
{
    var result = 0; 
    for (var i = 0; i < arr.length; i++) {
        result += arr[i];
    }
})(); 

Realization

This means that for any function you can apply a method which can perform additional actions:
Function.prototype.eval = function()
This method is defined for internal object Function, it is a certain amount of times, while retaining the length function and prints some statistics about the performance.
Let's to examine an example of usage:
var arr = [1, 2, 3, 4];

function sum(arr)
{
   var result = 0;
   for (var i = 0; i < arr.length; i++) {
      result += arr[i];
   }
   return result;
}

// original code
var s = sum(arr);

// modified code
var s = sum.eval(arr);
When comparing the original and modified codes, it can be seen that a difference is minimal. The code is pure until now and the result allows analyzing of information.

Features

This solution does not use timers and can be performed in different environment such as the browser window and standalone applications. Also it consists of the additional code solving problem of platform independency.
What has been implemented additionally? In the difference of analogues this code works in the most of cases - JavaScript/JScript. To reach this feature some additional useful properties has been defined:
- Function.prototype.evalCount - integer parameter keeps the number of iterations, by default is1000.
- Function.prototype.evalDuration - integer parameter keeps duration of the code execution. It is evaluated during a benchmarking process and does not have default value.
- Function.prototype.evalPrint = function() - auxiliary function for output of statistics of the performance. It takes in account differences of environments and launches appropriate output method. By default, it outputs the number of iterations and duration.
Of course, these parameters can be redefined, e.g., change the number of iterations or change the method of the statistics output. And this can be performed both globally via the function’s prototype and locally for the actual function. E.g.:
sum.evalCount = 10240; // 10K iterations
var result = sum.eval(1, 2, 3, 4);

Benefits

In the contrast of analogues the suggested method:
1. allows a light embedding the performance code with minimal modifications of the main code;
2. has extended features of control;
3. independent of the environment and allows to use this extension without modification both JavaScript and JScript (Windows Scripting Host).

The full solution

The solution is environment independent. It has been tested for IE6, IE7, FF2.0.0.15, FF3.0.7 and Windows Scripting Host. It is distributed under the GPL. The source code is available by this link. Ibid - the download link.

References

1. John Resig's blog
2. webtoolkit site

Saturday, 21 March 2009

JavaScript / JScript Benchmark

Начало

Не буду лгать - я не люблю оценивать производительность javascript кода. Эта процедура содержит для основного проекта массу "ненужного" и "бессмысленного" кода:
var n = 1000; 
var start = (new Date()).getTime();/
for (var i = 0 ; i < n; i++) {
    // bla-bla-bla ...
}
var stop = (new Date()).getTime();
var duration = stop - start;
var average = duration / n; 
document.writeln(average);
Думаю, всем знакомы строчки, подобные этим. Меня может интересовать производительность функции или метода, а возможно и некоторого участка программы. Но я не хочу засорять свой еще сырой код всяким мусором, подобным этому. Я также не хочу это делать для функций, когда пытаюсь оценить - какой же алгоритм лучше.
Когда мне это окончательно надоело - я решил написать свой Benchmark. Задача заключалась в том, чтобы можно было оценить производительность не только функции, но и любого участка кода, начиная со строки n и заканчивая строкой m.

Мотивация

Начиная работу, я понимал - вполне вероятно, что такой код уже кем-то и весьма успешно реализован. Тем не менее, я начал с нуля. После завершения я решил сравнить свое решение с работами своих предшественников (ссылки в конце). Не буду скромничать - не смотря на некоторое сходство, мое решение лучше. Аргументация следующая - при существенном сходстве реализации мой код в значительной мере более универсален.
Далее я покажу - что есть, и как из него можно получить весьма удобный и простой инструмент. Частично будут рассмотрены сходства и различия настоящего решения и его предшественников.

Особенности ОО-подхода в JavaScript

Любой фрагмент кода (особенно это справедливо для JavaScript с его особым ОО-подходом) может быть телом безымянной функции. Например, код
var arr = [100, 200, 300, 400]; 
    var result = 0; 
    for (var i = 0; i < arr.length; i++) {
        result += arr[i];
    }
может быть преобразован в следующий
var arr = [100, 200, 300, 400]; 
((function() 
{
    var result = 0; 
    for (var i = 0; i < arr.length; i++) {
        result += arr[i];
    }
})(); 

Реализация

Это значит, что для любой функции можно применить некий метод, который может выполнить дополнительные действия, а именно:
Function.prototype.eval = function()
Данный метод определен для встроенного объекта Function, вызывает его определенное количество раз, при этом запоминает длительность выполнения функции и печатает некоторую статистическую информацию о ходе выполнения.
Посмотрим на примере как эго можно использовать:
var arr = [1, 2, 3, 4];

function sum(arr)
{
   var result = 0;
   for (var i = 0; i < arr.length; i++) {
      result += arr[i];
   }
   return result;
}

// исходный код
var s = sum(arr);

// модифицированный код
var s = sum.eval(arr);
То есть, сравнивая исходный и модифицированный код, можно увидеть, что отличия минимальны. Код по прежнему чист, а результат позволяет проанализировать полученную информацию.

Особенности

Данное решение не использует таймеры отчета времени и может выполняться в разных средах, как в окне браузера, так и в самостоятельном приложении. Также содержит некоторый дополнительный код, который решает проблему кроссплатформенности.
Что же было применено дополнительно? В отличия от аналогов, данный пример работает для большинства случаев - JavaScript/JScript. Для этого дополнительно определено несколько полезных свойств:
- Function.prototype.evalCount - целочисленный параметр хранит количество итераций, или количество раз исполнения кода, значение по умолчанию 1000.
- Function.prototype.evalDuration - целочисленный параметр хранит продолжительность выполнения кода.
- Function.prototype.evalPrint = function() - вспомогательная функция для вывода статистики о производительности, учитывает различия сред исполнения и вызывает соответствующий метод для вывода количества итераций и продолжительности исполнения кода.
По умолчанию, производится оценка и выводится время исполнения кода для 1000 итераций. Параметр Function.prototype.evalDuration вычисляется в процессе и не имеет значения по умолчанию.
Естественно, эти параметры можно переопределить, например, изменить количество итераций, или переопределить свой метод отображения статистики. При чем, это можно сделать как глобально через прототип, так и конкретно для определенной функции. Например:
sum.evalCount = 10240; // 10K iterations
var result = sum.eval(1, 2, 3, 4);

Преимущества

Предложенная методика в отличие от аналогов
1. позволяет легко внедрить в свой код возможности оценки производительности с минимальными изменениями основного кода;
2. имеет расширенные возможности контроля;
3. она независима от текущей платформы и позволяет использовать данное расширение без изменений как в среде JavaScript, так и в JScript (Windows Scripting Host).

Полное решение

Решение платформно-независимо. Тестировалось для IE6, IE7, FF2.0.0.15, FF3.0.7 и Windows Scripting Host. Распространяется по лицензии GPL. Листинг исходного кода доступен по этой ссылке. Там же - ссылка на скачивание.

Ссылки по теме

1. John Resig's blog
2. webtoolkit site