-
Notifications
You must be signed in to change notification settings - Fork 10
/
Copy pathys.js
211 lines (164 loc) · 4.31 KB
/
ys.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
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
//
// http://slovari.yandex.ru
// Command line version
//
// Copyright (c) 2010, 2011, Ildar Shaimordanov
//
/*!
*/
///////////////////////////////////////////////////////////////////////////
//[requires[ js/Ajax.js ]]
///////////////////////////////////////////////////////////////////////////
var YandexSlovary = {
// Yandex Slovary Shell
name: 'Yandex.Slovari Shell',
version: '0.1.6 Beta',
userAgent: function()
{
return this.name + '/' + this.version + '; (compatible; Windows Script Host, Version ' + WScript.Version + ')';
},
// Looking for text within these tags
re_content: /<div class="b-holster.*?">((?:[\r\n]|.)*)<\/div>\s*<div class="b-foot">/m,
re_notags: [
/<(form|select|script)(?:[\r\n]|.)+?\/\1>/img,
/<div class="b-tabs-line">.*?<\/div>/img
],
re_noents: [
/&(#\d+|#x[0-9a-f]+|[a-z]+);/ig,
/\u2022/g,
],
// Use mobile version of Ynadex.Slovari
url: 'http://m.slovari.yandex.ru/search.xml'
};
YandexSlovary.help = function()
{
var msg = this.name + '/' + this.version + '\n'
+ 'Copyright (C) 2010, 2011, Ildar Shaimordanov\n'
+ '\n'
+ 'Usage: ' + WScript.ScriptName + ' "PHRASE" [ /LANG:lang-abbr-list ]'
;
this.alert(msg);
};
YandexSlovary.alert = function()
{
if ( arguments.length == 0 ) {
return;
}
WScript.Echo([].slice.call(arguments));
};
YandexSlovary.quit = function(exitCode)
{
WScript.Quit(exitCode);
};
YandexSlovary.parse = function(xml)
{
var m = xml.match(this.re_content);
if ( ! m ) {
return '';
}
var result = m[0];
// Removes all unused tags and their contents
for (var i = 0; i < this.re_notags.length; i++) {
result = result.replace(this.re_notags[i], '');
}
// Removes all unprintable entities
for (var i = 0; i < this.re_noents.length; i++) {
result = result.replace(this.re_noents[i], '');
}
return result
// Converts block tags to line breaks
.replace(/<(div|h[\d])[^>]*>(.*?)<\/\1>/img, '\n$2\n')
.replace(/<p[^>]*>(.*?)<\/p>/img, '$1\n')
// Replaces tagged line breaks to text-oriented line breaks
.replace(/<br[^>]*>/ig, '\n')
// Replaces <img alt="..."> with the text from the alt attribute
// .replace(/<img.+?alt="([^"]+)"[^>]*>/g, '$1')
// Removes the rest of tags
.replace(/<[^<>]+>/g, '')
// Leaves the most important entities
.replace(/ /ig, ' ')
.replace(/</ig, '<')
.replace(/>/ig, '>')
// removes all heading and trailing white spaces
.replace(/^\s+|\s+$/g, '')
;
};
YandexSlovary.query = function(url)
{
return Ajax.queryFile(url, {
headers: {
'User-Agent': this.userAgent()
}
});
/*
var IDs = [
'Msxml2.XMLHTTP',
'Microsoft.XMLHTTP'];
var xmlhttp;
for (var i = 0; i < IDs.length; i++) {
var e;
try {
xmlhttp = new ActiveXObject(IDs[i]);
break;
} catch (e) {
}
}
if ( ! xmlhttp ) {
throw new ReferenceError();
}
var result;
xmlhttp.onreadystatechange = function()
{
if ( xmlhttp.readyState != 4 ) {
return;
}
result = xmlhttp.responseText;
};
xmlhttp.open('GET', url, false);
xmlhttp.setRequestHeader('If-Modified-Since', (new Date(0)).toUTCString());
xmlhttp.setRequestHeader('User-Agent', this.userAgent());
xmlhttp.send();
return result;
*/
};
YandexSlovary.get = function(word, lang)
{
if ( ! word ) {
return '';
}
var queryString = 'text=' + encodeURIComponent(word);
if ( lang === undefined ) {
queryString += '&where=2';
} else {
queryString += '&where=3&lang=' + (encodeURIComponent(lang) || 'en-ru-en');
}
return this.query(this.url + '?' + queryString);
};
YandexSlovary.glossary = function(word, lang)
{
return this.parse(this.get(word, lang));
};
///////////////////////////////////////////////////////////////////////////
if ( WScript.Arguments.Unnamed.length == 0 ) {
YandexSlovary.help();
YandexSlovary.quit();
}
var word = (function()
{
var result = [];
for (var i = 0; i < WScript.Arguments.Unnamed.length; i++) {
result.push(WScript.Arguments.Unnamed.item(i));
}
return result.join(' ');
})();
var lang = (function()
{
var e;
try {
return WScript.Arguments.Named.item('LANG');
} catch (e) {
return '';
}
})();
var text = YandexSlovary.glossary(word, lang);
YandexSlovary.alert(text);