PageSourceSearch

https://bii.by/js/htw/part2Word.js

js bii.by collected 2026-10-02 04:03:37 UTC 16,410 bytes, 534 lines download raw bytes

1let stack_txt = [];
2const
3    getRoot = document.querySelector('#source-html'),
4    getTool = document.querySelector('#getdoc-exp_id');
5
6class Toast {
7    constructor(params) {
8
9        this._title = params['title'] === false ? false : params['title'] || 'Title';
10        this._text = params['text'] || 'Message...';
11        this._theme = params['theme'] || 'default';
12        this._autohide = params['autohide'] && true;
13        this._interval = +params['interval'] || 5000;
14        this._create();
15        this._el.addEventListener('click', (e) => {
16            if (e.target.classList.contains('toast__close')) {
17                this._hide();
18            }
19        });
20        this._show();
21    }
22
23    _show() {
24        this._el.classList.add('toast_showing');
25        this._el.classList.add('toast_show');
26        window.setTimeout(() => {
27            this._el.classList.remove('toast_showing');
28        });
29        if (this._autohide) {
30            setTimeout(() => {
31                this._hide();
32            }, this._interval);
33        }
34    }
35
36    _hide() {
37        this._el.classList.add('toast_showing');
38        this._el.addEventListener('transitionend', () => {
39            this._el.classList.remove('toast_showing');
40            this._el.classList.remove('toast_show');
41            this._el.remove();
42        }, {
43            once: true
44        });
45        const event = new CustomEvent('hide.toast', {
46            detail: {
47                target: this._el
48            }
49        });
50        document.dispatchEvent(event);
51    }
52
53    _create() {
54        const el = document.createElement('div');
55        el.classList.add('toast');
56        el.classList.add(`toast_${this._theme}`);
57        let html = `{header}<div class="toast__body"></div><button class="toast__close" type="button"></button>`;
58        const htmlHeader = this._title === false ? '' : '<div class="toast__header"></div>';
59        html = html.replace('{header}', htmlHeader);
60        el.innerHTML = html;
61        if (this._title) {
62            el.querySelector('.toast__header').textContent = this._title;
63        } else {
64            el.classList.add('toast_message');
65        }
66        el.querySelector('.toast__body').textContent = this._text;
67        this._el = el;
68        if (!document.querySelector('.toast-container')) {
69            const container = document.createElement('div');
70            container.classList.add('toast-container');
71            document.body.append(container);
72        }
73        document.querySelector('.toast-container').append(this._el);
74    }
75}
76
77document.addEventListener('mouseup', e => {
78    window.getSelection().toString().length ?
79        (
80            getTool.classList.add('active')
81        ) : null;
82});
83
84document.addEventListener('mousedown', () => {
85
86    if (window.getSelection().toString().length < 1) {
87        if (stack_txt.length < 1) {
88            getTool.classList.remove('active');
89
90        }
91
92    }
93});
94
95var close_id = document.getElementById('close_id');
96close_id.onclick = function () {
97    stack_txt = [];
98    clearSelection();
99
100}
101
102function copy2Word() {
103    console.log('copy2Word');
104    var style = document.getElementsByTagName("STYLE")[0];
105    if (!style) {
106        style = "";
107    } else {
108        style = style.innerHTML;
109    }
110
111    var result = "";
112    if (stack_txt.length < 1) {
113        getTool.classList.remove('active');
114        result = getHTMLOfSelection();
115        $(result).find('a').removeAttr('href');
116        $(result).find('.an').hide();
117        export2Word(style, result);
118        clearSelection();
119    } else {
120        export2WordArray(style, stack_txt);
121    }
122    stack_txt = [];
123    clearSelection();
124
125}
126
127function copy2Print() {
128    var result = "";
129    if (stack_txt.length < 1) {
130        getTool.classList.remove('active');
131        result = getHTMLOfSelection();
132        $(result).find('a').removeAttr('href');
133        $(result).find('.an').hide();
134        printElement(result);
135        clearSelection();
136
137    } else {
138        printArray(stack_txt);
139    }
140    stack_txt = [];
141    clearSelection();
142
143}
144
145function copy2Stack() {
146    stack_txt.push(getHTMLOfSelection());
147    new Toast({
148        title: false,
149        text: 'добавлено:' + stack_txt.length,
150        theme: 'default',
151        autohide: true,
152        interval: 2000
153    });
154}
155
156function getHTMLOfSelection() {
157    var range;
158    if (document.selection && document.selection.createRange) {
159        range = document.selection.createRange();
160        $(range.htmlText).find('a').removeAttr('href');
161        $(range.htmlText).find('.an').hide();
162        return range.htmlText;
163    } else if (window.getSelection) {
164        var selection = window.getSelection();
165        if (selection.rangeCount > 0) {
166            range = selection.getRangeAt(0);
167            var clonedSelection = range.cloneContents();
168            var div = document.createElement('div');
169            div.appendChild(clonedSelection);
170            $(div).find('a').removeAttr('href');
171            $(div).find('.an').hide();
172            return div;
173        }
174    }
175    return '';
176}
177
178function clearSelection() {
179    getTool.classList.remove('active');
180    if (window.getSelection) {
181        window.getSelection().removeAllRanges();
182    } else {
183        document.selection.empty();
184    }
185}
186
187//----------------------------------------------------------------
188function getCSS() {
189    var css = [];
190    for (var sheeti = 0; sheeti < document.styleSheets.length; sheeti++) {
191        var sheet = document.styleSheets[sheeti];
192        let pash = sheet.href;
193        if(pash !== null)
194        {
195            if(pash.indexOf("jivo") ==-1)
196            {
197                var rules = ('cssRules' in sheet) ? sheet.cssRules : sheet.rules;
198                for (var rulei = 0; rulei < rules.length; rulei++) {
199                    var rule = rules[rulei];
200                    if ('cssText' in rule)
201                        css.push(rule.cssText);
202                    else
203                        css.push(rule.selectorText + ' {\n' + rule.style.cssText + '\n}\n');
204                }
205            }
206        }
207    }
208    return css.join('\n');
209}
210//----------------------------------------------------------------------------------------------------------------------
211function cutTegs(str) {
212    var regex = /( |<img([^>]+)>)/ig,
213        result = str.replace(regex, "");
214    return result;
215}
216
217//----------------------------------------------------------------------------------------------------------------------
218function export2Word(style, element) {
219    var css = "<style>"+getCSS()+"</style>";
220    var text = '<div style="font-size: 12px;">' + document.title + '</div><br>';
221    text += cutTegs(element.innerHTML);
222    html = '\
223  		 	<html xmlns:o="urn:schemas-microsoft-com:office:office" xmlns:w="urn:schemas-microsoft-com:office:word" xmlns="http://www.w3.org/TR/REC-html40">\
224  		 	<head><meta charset=\'utf-8\'>\
225  		 	' + css + '</head>\
226  		 	<body lang=RU-ru style="tab-interval:.5in">' + text + '\
227  		 	</body>\
228  		 	</html>';
229
230    if (style.indexOf('landscape', 0) != -1) {
231        var orientation = "landscape";
232    } else {
233        var orientation = "portrait";
234    }
235    var converted = htmlDocx.asBlob(html, {
236        orientation: orientation,
237        margins: {top: 720, right: 720, bottom: 720, left: 720}
238    });
239    var filename = getFilename();
240    stack_txt = [];
241    saveAs(converted, filename + '.docx');
242
243}
244
245//----------------------------------------------------------------------------------------------------------------------
246function export2WordArray(style, elements) {
247    var css = "<style>" + getCSS() + "</style>";
248    var text = '<div style="font-size: 12px;">' + document.title + '</div><br>';
249    elements.forEach(function (element) {
250        text += cutTegs(element.innerHTML) + '<br> <br>';
251    });
252    html = '\
253  		 	<html xmlns:o="urn:schemas-microsoft-com:office:office" xmlns:w="urn:schemas-microsoft-com:office:word" xmlns="http://www.w3.org/TR/REC-html40">\
254  		 	<head><meta charset=\'utf-8\'>\
255  		 	' + css + '</head>\
256  		 	<body lang=RU-ru style="tab-interval:.5in">' + text + '\
257  		 	</body>\
258  		 	</html>';
259
260    if (style.indexOf('landscape', 0) != -1) {
261        var orientation = "landscape";
262    } else {
263        var orientation = "portrait";
264    }
265    var converted = htmlDocx.asBlob(html, {
266        orientation: orientation,
267        margins: {top: 720, right: 720, bottom: 720, left: 720}
268    });
269    var filename = getFilename();
270    stack_txt = [];
271    saveAs(converted, filename + '.docx');
272
273}
274
275//----------------------------------------------------------------------------------------------------------------------
276function printElement(element) {
277    printTXT(element.innerHTML);
278}
279
280//----------------------------------------------------------------------------------------------------------------------
281function printArray(elements) {
282    var text = "";
283    elements.forEach(function (element) {
284        text += element.innerHTML + "</br></br>";
285    });
286    printTXT(text);
287}
288
289//----------------------------------------------------------------------------------------------------------------------
290function printTXT(text) {
291    var style = "<style>table {table-layout: fixed; border-collapse: collapse; border: 1px; width: 100%;}" +
292        "td, th {border: 1px solid #636b6f; padding: 5px;}" +
293        ".title {font-size: 25px;}" +
294        ".m-b-md {margin-bottom: 30px;margin-top: 40px;}" +
295        "ins {text-decoration: none;}</style>";
296
297    var newWin = window.open('', 'Print-Window');
298    newWin.document.open();
299    newWin.document.write('<html><head>' + style + '<title>' + document.title + '</title></head><body onload="window.print()"><div class="title m-b-md"></div>' + text + '</body></html>');
300    newWin.document.close();
301    setTimeout(function () {
302        newWin.close();
303    }, 10);
304    stack_txt = [];
305}
306
307//----------------------------------------------------------------------------------------------------------------------
308function getFilename() {
309    var tmp = document.title;
310    let words = tmp.split(' ');
311    var isAnalitic = -1;
312    var isComment = -1;
313    var isDoc = 1;
314    var ot_index = -1;
315
316    words.forEach(function (elem, index) {
317
318        if (elem == "Автор" || elem == "Источник:" || tmp.indexOf("Комментарий к") != -1 || tmp.indexOf("Комментарии к") != -1) {
319            if (tmp.indexOf("Комментарии к") != -1) {
320                isComment = 1;
321                isDoc = -1;
322                isAnalitic = -1;
323            } else {
324                isAnalitic = 1;
325                isDoc = -1;
326                isComment = -1;
327            }
328        }
329
330        if (elem == "от") {
331            ot_index = index;
332        }
333    })
334
335    if (isAnalitic == 1) {
336
337        var character = '';
338        var isOver = 0;
339        if (tmp.indexOf("Автор") != -1) {
340            let array = tmp.match(/.*(?=Автор)/);
341            tmp = array[0];
342            words = tmp.split(' ');
343        }
344        if (tmp.indexOf("Источник:") != -1) {
345            let array = tmp.match(/.*(?=Источник:)/);
346            tmp = array[0];
347            words = tmp.split(' ');
348        }
349        tmp = "";
350        words.forEach(function (elem, index) {
351            if (isOver == 0) {
352                if (index > 0) {
353                    character = elem.charAt(0);
354                    if (character === character.toUpperCase() && character !== character.toLowerCase()) {
355                        tmp += words[index];
356
357                        if (words[index + 1] != undefined) {
358                            tmp += " " + words[index + 1];
359                        }
360                        if (words[index + 2] != undefined) {
361                            tmp += " " + words[index + 2];
362                        }
363                        isOver = 1;
364                    }
365                }
366            }
367
368        })
369    } else if (isComment == 1) {
370        tmp = words[0];
371        if (words[1] != undefined) {
372            tmp += " " + words[1]
373        }
374        if (words[2] != undefined) {
375            tmp += " " + words[2];
376        }
377
378    } else if (isDoc == 1) {
379        if (tmp.indexOf(" от ") != -1) // есть "от" судебка или ненормативка
380        {
381            if (words[0] == "Другие") {
382                tmp = "";
383                var isOver = 0;
384                var character = '';
385                words.forEach(function (elem, index) {
386
387                    if (index > ot_index) {
388                        if (isOver == 0) {
389                            character = elem.charAt(0);
390
391                            if (character === character.toUpperCase() && character !== character.toLowerCase()) {
392
393                                tmp += words[index];
394                                if (words[index + 1] != undefined) {
395                                    tmp += " " + words[index + 1];
396                                }
397                                if (words[index + 2] != undefined) {
398                                    tmp += " " + words[index + 2];
399                                }
400                                isOver = 1;
401                            }
402                        }
403                    }
404                })
405
406
407            } else {
408                tmp = "";
409                words.forEach(function (elem, index) {
410                    if (index == 0) {
411                        tmp = elem + " ";
412                    } else {
413                        if (validate_date(elem))//дата
414                        {
415                            tmp += elem + " ";
416                        } else if (elem == "№") {
417                            tmp += words[index + 1];
418                        }
419                    }
420                })
421            }
422        } else // нет "от"
423        {
424            tmp = words[0];
425            if (words[1] != undefined) {
426                tmp += " " + words[1]
427            }
428            if (words[2] != undefined) {
429                tmp += " " + words[2];
430            }
431        }
432    }
433    var filename = transliterate(tmp.trim());
434    return filename;
435}
436
437//----------------------------------------------------------------------------------------------------------------------
438function transliterate(word) {
439    var answer = ""
440        , a = {};
441    a["Ё"] = "YO";
442    a["Й"] = "I";
443    a["Ц"] = "TS";
444    a["У"] = "U";
445    a["К"] = "K";
446    a["Е"] = "E";
447    a["Н"] = "N";
448    a["Г"] = "G";
449    a["Ш"] = "SH";
450    a["Щ"] = "SCH";
451    a["З"] = "Z";
452    a["Ð¥"] = "H";
453    a["Ъ"] = "'";
454    a["ё"] = "yo";
455    a["й"] = "i";
456    a["ц"] = "ts";
457    a["у"] = "u";
458    a["к"] = "k";
459    a["е"] = "e";
460    a["н"] = "n";
461    a["г"] = "g";
462    a["ш"] = "sh";
463    a["щ"] = "sch";
464    a["з"] = "z";
465    a["х"] = "h";
466    a["ъ"] = "'";
467    a["Ф"] = "F";
468    a["Ы"] = "I";
469    a["В"] = "V";
470    a["А"] = "A";
471    a["П"] = "P";
472    a["Р"] = "R";
473    a["О"] = "O";
474    a["Л"] = "L";
475    a["Д"] = "D";
476    a["Ж"] = "ZH";
477    a["Э"] = "E";
478    a["ф"] = "f";
479    a["ы"] = "i";
480    a["в"] = "v";
481    a["а"] = "a";
482    a["п"] = "p";
483    a["р"] = "r";
484    a["о"] = "o";
485    a["л"] = "l";
486    a["д"] = "d";
487    a["ж"] = "zh";
488    a["э"] = "e";
489    a["Я"] = "Ya";
490    a["Ч"] = "CH";
491    a["С"] = "S";
492    a["М"] = "M";
493    a["И"] = "I";
494    a["Т"] = "T";
495    a["Ь"] = "'";
496    a["Б"] = "B";
497    a["Ю"] = "YU";
498    a["я"] = "ya";
499    a["ч"] = "ch";
500    a["с"] = "s";
501    a["м"] = "m";
502    a["и"] = "i";
503    a["т"] = "t";
504    a["ь"] = "'";
505    a["б"] = "b";
506    a["ю"] = "yu";
507    a[" "] = "_";
508
509    for (i in word) {
510        if (word.hasOwnProperty(i)) {
511            if (a[word[i]] === undefined) {
512                answer += word[i];
513            } else {
514                answer += a[word[i]];
515            }
516        }
517    }
518    return answer;
519}
520
521//----------------------------------------------------------------------------------------------------------------------
522function validate_date(value) {
523    var arrD = value.split(".");
524    arrD[1] -= 1;
525    var d = new Date(arrD[2], arrD[1], arrD[0]);
526    if ((d.getFullYear() == arrD[2]) && (d.getMonth() == arrD[1]) && (d.getDate() == arrD[0])) {
527        return true;
528    } else {
529
530        return false;
531    }
532}
533
534//----------------------------------------------------------------------------------------------------------------------

Line numbers count LF bytes from the start of the resource, as the search results do. Vendor segments are library code the classifier recognised; they are stored but not indexed. Bytes are shown as Latin1 characters, one per byte.