You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
201 lines
7.3 KiB
201 lines
7.3 KiB
1 year ago
|
// ==UserScript==
|
||
|
// @name News parser
|
||
|
// @namespace http://zakonvremeni.ru
|
||
|
// @version 0.1
|
||
|
// @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) <AlexeiBv+mirocod_zv@narod.ru>
|
||
|
|
||
|
(function() {
|
||
|
'use strict';
|
||
|
|
||
|
function getByClass (className, parent) {
|
||
|
parent || (parent=document);
|
||
|
var descendants=parent.getElementsByTagName('*'), i=-1, e, result=[];
|
||
|
var re = new RegExp("(?:^|\\s)" + className + "(?!\\S)");
|
||
|
while (e=descendants[++i]) {
|
||
|
if (re.test(e.className)){
|
||
|
result.push(e);
|
||
|
}
|
||
|
}
|
||
|
return result;
|
||
|
}
|
||
|
|
||
|
function GetImageInContainers(baseClass, parent, textAlign) {
|
||
|
var elems = getByClass(baseClass, parent);
|
||
|
if (!elems) {
|
||
|
return '';
|
||
|
}
|
||
|
|
||
|
var i;
|
||
|
var img_src = '';
|
||
|
var re = new RegExp("(https?:\/\/.*\.(?:png|jpg))");
|
||
|
for (i in elems) {
|
||
|
var e = elems[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 '<p style = "text-align:' + textAlign + ';"><img src = "'+ img_src + '" width = "600px"/></p>';
|
||
|
}
|
||
|
return '';
|
||
|
}
|
||
|
|
||
|
function Trim(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 GetContentInContainers(a_OutTag, baseClass, parent, textAlign, a_ElementFilterFunc, a_ClearTextFunc) {
|
||
|
var elems = getByClass(baseClass, parent);
|
||
|
if (!elems) {
|
||
|
return 'Не удалось найти ' + baseClass;
|
||
|
}
|
||
|
|
||
|
var result = '';
|
||
|
for (var i in elems) {
|
||
|
var e = elems[i];
|
||
|
if (a_ElementFilterFunc && !a_ElementFilterFunc(e)) {
|
||
|
continue;
|
||
|
}
|
||
|
|
||
|
var content = '';
|
||
|
if (e.querySelectorAll) {
|
||
|
var children = e.querySelectorAll("*");
|
||
|
if (children.length == 0 || e.innerText) {
|
||
|
if (e.innerText) {
|
||
|
content += Trim(e.textContent);
|
||
|
}
|
||
|
}
|
||
|
else {
|
||
|
for (let i = 0; i < children.length; i++) {
|
||
|
var c = children[i];
|
||
|
if (c.innerText) {
|
||
|
content += Trim(c.textContent);
|
||
|
}
|
||
|
}
|
||
|
}
|
||
|
}
|
||
|
if (a_ClearTextFunc) {
|
||
|
content = a_ClearTextFunc(content);
|
||
|
}
|
||
|
if (a_OutTag == '') {
|
||
|
result += content;
|
||
|
}
|
||
|
else {
|
||
|
result += '<' + a_OutTag + ' style = "text-align:' + textAlign + ';">' + content + '</' + a_OutTag + '>';
|
||
|
}
|
||
|
}
|
||
|
return result;
|
||
|
}
|
||
|
|
||
|
function ClearUrl(a_Url) {
|
||
|
var separator = '?';
|
||
|
return RemoveAfterSplitter(a_Url, separator, false);
|
||
|
}
|
||
|
|
||
|
function FIlterTrue(element) {
|
||
|
return true;
|
||
|
}
|
||
|
|
||
|
function FIlterRia(element) {
|
||
|
if (element.dataset.type == 'text' || element.dataset.type == 'quote' || element.dataset.type == 'list') {
|
||
|
return true;
|
||
|
}
|
||
|
return false;
|
||
|
}
|
||
|
|
||
|
function FIlterZV(element) {
|
||
|
if (element.itemprop == 'articleBody') {
|
||
|
return true;
|
||
|
}
|
||
|
return 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 MakeContent() {
|
||
|
var content = '';
|
||
|
var title_tag = 'h2';
|
||
|
var p_tag = 'p';
|
||
|
var zero_tag = '';
|
||
|
var source_add = true;
|
||
|
if (location.hostname == 'tass.ru') {
|
||
|
content += GetContentInContainers(title_tag, 'tass_pkg_title--variant_h1_default.*', document.getElementById('content_box'), 'center');
|
||
|
content += GetImageInContainers('Image_wrapper_.*', document.getElementById('content_box'), 'center');
|
||
|
content += GetContentInContainers(p_tag, 'Paragraph_paragraph.*', document.getElementById('content_box'), 'justify', FIlterTrue, ClearTextFuncTemplate(['/ТАСС/. ']));
|
||
|
}
|
||
|
else if (location.hostname == 'ria.ru') {
|
||
|
content += GetContentInContainers(title_tag, 'article__title', document.getElementsByClassName('article__header')[0], 'center');
|
||
|
content += GetImageInContainers('photoview__open', document.getElementsByClassName('article__header')[0], 'center');
|
||
|
content += GetContentInContainers(p_tag, 'article__block', document.getElementsByClassName('article__body')[0], 'justify', FIlterRia, ClearTextFuncTemplate(['– РИА Новости. ', '— РИА Новости. ']));
|
||
|
}
|
||
|
else if (location.hostname == 'zakonvremeni.ru') {
|
||
|
var title = GetContentInContainers(zero_tag, 'page-header', document.getElementsByClassName('item-page')[0]);
|
||
|
var parent_category = GetContentInContainers(zero_tag, 'parent-category-name', document.getElementsByClassName('item-page')[0]);
|
||
|
var category = GetContentInContainers(zero_tag, 'category-name', document.getElementsByClassName('item-page')[0]);
|
||
|
var page = RemoveAfterSplitter(Trim(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 = '<textarea id = "news_content" rows="10" cols="100">' + content;
|
||
|
if (source_add) {
|
||
|
result += '<p style="text-align: justify;">Источник: <a href = "' + ClearUrl(document.URL) + '">' + location.hostname + '</a></p>';
|
||
|
}
|
||
|
result += '</textarea>';
|
||
|
}
|
||
|
return result;
|
||
|
}
|
||
|
|
||
|
var content = MakeContent();
|
||
|
var logo = document.createElement("div");
|
||
|
logo.innerHTML = '<div style="margin: 0pt auto; width: 800px; text-align: center;">' + content + '</div>';
|
||
|
|
||
|
document.body.insertBefore(logo, document.body.firstChild);
|
||
|
|
||
|
})();
|