// ==UserScript== // @name News parser // @namespace http://zakonvremeni.ru // @version 0.2 // @description Parse news // @author AlexeiBv+mirocod@narod.ru // @match https://tass.ru/* // @match https://ria.ru/* // @match https://zakonvremeni.ru/* // @icon https://icons.duckduckgo.com/ip2/zakonvremeni.ru.ico // @grant none // ==/UserScript== // Общественное достояние, 2023, Алексей Безбородов (Alexei Bezborodov) (function() { 'use strict'; // Поиск элементов по регулярному выражению function GetElementClassName(a_Element) { return a_Element.className; } function FindElementsByRegExp(a_GenElementNameFunc, a_RegExpPattern, a_ElementParent) { a_ElementParent || (a_ElementParent=document); var descendants = a_ElementParent.getElementsByTagName('*'), i=-1, e, result=[]; var re = new RegExp("(?:^|\\s)" + a_RegExpPattern + "(?!\\S)"); while (e=descendants[++i]) { if (re.test(a_GenElementNameFunc(e))){ result.push(e); } } return result; } // Работа со строками function TrimString(s) { return ( s || '' ).replace( /^\s+|\s+$/g, '' ); } function RemoveBeforeSplitter(a_String, a_Splitter) { var index = a_String.indexOf(a_Splitter) if (index != -1) { return a_String.substring(index + a_Splitter.length); } return a_String; } function RemoveAfterSplitter(a_String, a_Splitter, a_SaveSplitter) { var index = a_String.indexOf(a_Splitter) if (index != -1) { var spl_len = a_Splitter.length if (!a_SaveSplitter) { spl_len = 0; } return a_String.substring(0, index + spl_len); } return a_String; } function ClearUrl(a_Url) { var separator = '?'; return RemoveAfterSplitter(a_Url, separator, false); } function ClearTextFuncTemplate(a_RemoveBeforeList) { function ClearTextFunc(a_Content) { var content = a_Content; for (let i = 0; i < a_RemoveBeforeList.length; i++) { var r = a_RemoveBeforeList[i]; content = RemoveBeforeSplitter(content, r); } return content; } return ClearTextFunc } // Работа с контейнерами function GetImageInContainers(a_Elements, a_TextAlign) { var i; var img_src = ''; var re = new RegExp("(https?:\/\/.*\.(?:png|jpg))"); for (i in a_Elements) { var e = a_Elements[i]; var children = e.querySelectorAll("*"); for(let i = 0; i < children.length; i++){ var c = children[i]; if (c.nodeName == 'IMG' && re.test(c.src)) { img_src = c.src; } } } if (img_src.length > 0) { return '

'; } return ''; } function GetContentInContainers(a_Elements, a_GrubTextFunc, a_FinishWorkFunc) { var result = ''; for (var i in a_Elements) { var e = a_Elements[i]; var content = ''; if (e.querySelectorAll) { var children = e.querySelectorAll("*"); if (children.length == 0 || e.innerText) { content += a_GrubTextFunc(e); } else { for (let i = 0; i < children.length; i++) { var c = children[i]; content += a_GrubTextFunc(c); } } } if (a_FinishWorkFunc) { result += a_FinishWorkFunc(content, e); } else { result += content; } } return result; } // Фильтрация элементов function FIlterElements(a_Elements, a_ElementChecker) { var result = []; for (let i = 0; i < a_Elements.length; i++) { var e = a_Elements[i]; if (a_ElementChecker(e)) { result.push(e); } } return result; } function ElementCheckerTrue(a_Element) { return true; } function ElementCheckerRia(a_Element) { if (a_Element.dataset.type == 'article' || a_Element.dataset.type == 'banner') { return false; } return true; } function ElementCheckerZV(a_Element) { if (a_Element.itemprop == 'articleBody') { return true; } return false; } // Обработка элементов function GrubTextFuncTemplate() { function GrubTextFunc(a_Element) { var content = ''; if (a_Element.innerText) { content = TrimString(a_Element.textContent); } return content; } return GrubTextFunc } function FinishWorkFuncTemplate(a_OutTag, a_TextAlign, a_ClearTextFunc) { function FinishWorkFunc(a_Content, a_Element) { if (a_ClearTextFunc) { a_Content = a_ClearTextFunc(a_Content); } if (a_Element && a_Element.dataset && a_Element.dataset.type == 'list') { var childrens = FindElementsByRegExp(GetElementClassName, 'article__list-item', a_Element); let content = ''; for (let i = 0; i < childrens.length; i++) { var c = childrens[i]; content += '
  • ' + GrubTextFuncTemplate()(c) + '
  • '; } a_Content = ''; } if (a_OutTag && a_TextAlign) { a_Content = '<' + a_OutTag + ' style = "text-align:' + a_TextAlign + ';">' + a_Content + ''; } if (a_Element && a_Element.dataset && a_Element.dataset.type == 'quote') { a_Content = '
    ' + a_Content + '
    '; } return a_Content; } return FinishWorkFunc } // Создание контента для стандартных новостей function MakeContentByNews(a_BaseElementTitle, a_BaseElementImage, a_BaseElementText, a_TitleRegExpElementPattern, a_ImageRegExpElementPattern, a_TextRegExpElementPattern, a_ElementChecker, a_ClearTextPatterns) { var title_tag = 'h2'; var p_tag = 'p'; var title_finish_text_func = FinishWorkFuncTemplate(title_tag, 'center') var grub_func = GrubTextFuncTemplate(); var content = ''; var paragraph_finish_text_func = FinishWorkFuncTemplate(p_tag, 'justify', ClearTextFuncTemplate(a_ClearTextPatterns)); content += GetContentInContainers(FindElementsByRegExp(GetElementClassName, a_TitleRegExpElementPattern, a_BaseElementTitle), grub_func, title_finish_text_func); content += GetImageInContainers(FindElementsByRegExp(GetElementClassName, a_ImageRegExpElementPattern, a_BaseElementImage), 'center'); content += GetContentInContainers(FIlterElements(FindElementsByRegExp(GetElementClassName, a_TextRegExpElementPattern, a_BaseElementText), a_ElementChecker), grub_func, paragraph_finish_text_func); return content; } // Создание контента для сайта function MakeContent() { var content = ''; var source_add = true; var zero_tag_func = FinishWorkFuncTemplate() var grub_text_func = GrubTextFuncTemplate() if (location.hostname == 'tass.ru') { let base_element = document.getElementById('content_box'); content = MakeContentByNews( base_element, base_element, base_element, 'tass_pkg_title--variant_h1_default.*', 'Image_wrapper_.*', 'Paragraph_paragraph.*', ElementCheckerTrue, ['/ТАСС/. '] ); } else if (location.hostname == 'ria.ru') { let base_element = document.getElementsByClassName('article__header')[0]; var base_element_text = document.getElementsByClassName('article__body')[0]; var tire = ['-', '–', '—', '‒', '―', '⸺', '⸻']; var clear_text = []; for (var i in tire) { var t = tire[i]; clear_text.push(t + ' РИА Новости. '); } content = MakeContentByNews( base_element, base_element, base_element_text, 'article__title', 'photoview__open', 'article__block', ElementCheckerRia, clear_text ); } else if (location.hostname == 'zakonvremeni.ru') { let base_element = document.getElementsByClassName('item-page')[0]; var title = GetContentInContainers(FindElementsByRegExp(GetElementClassName, 'page-header', base_element), grub_text_func); var parent_category = GetContentInContainers(FindElementsByRegExp(GetElementClassName, 'parent-category-name', base_element), grub_text_func); var category = GetContentInContainers(FindElementsByRegExp(GetElementClassName, 'category-name', base_element), grub_text_func); var page = RemoveAfterSplitter(TrimString(document.getElementsByClassName('item-page')[0].querySelector('[itemprop=articleBody]').textContent), '.', true); content = title + '\n' + parent_category + ' ' + category + '\n\n' + page + '\n' + document.URL; source_add = false; } var result = ''; if (content.length > 0) { result = ''; } return result; } var content = MakeContent(); var logo = document.createElement("div"); logo.innerHTML = '
    ' + content + '
    '; document.body.insertBefore(logo, document.body.firstChild); })();