0

переработка скрипта

This commit is contained in:
2021-10-31 23:10:12 +03:00
parent 9168f84a17
commit 7b93ac2abf

View File

@@ -1,156 +1,279 @@
// ==UserScript== // ==UserScript==
// @name JRH: JoyReactor Helper // @name JRH: JoyReactor Helper
// @description Улучшаем JoyReactor // @description Вспомогательные инструменты для JoyReactor
// @version 2.3.1 // @version 2.4
// @author DmitriyMX // @author DmitriyMX
// @namespace https://gitlab.com/DmitriyMX/joyreactor-helper // @namespace https://gitlab.com/DmitriyMX/joyreactor-helper
// @downloadURL https://gitlab.com/DmitriyMX/joyreactor-helper/raw/master/jrh.user.js // @downloadURL https://gitlab.com/DmitriyMX/joyreactor-helper/raw/master/jrh.user.js
// @updateURL https://gitlab.com/DmitriyMX/joyreactor-helper/raw/master/jrh.user.js // @updateURL https://gitlab.com/DmitriyMX/joyreactor-helper/raw/master/jrh.user.js
// @match http://joy.reactor.cc/* // @match http://joy.reactor.cc/*
// @match http://pornreactor.cc/*
// @grant none // @grant none
// @run-at document-end // @run-at document-end
// ==/UserScript== // ==/UserScript==
const domainAliaces = { class Post {
"joy.reactor.cc": ['reactor.cc', 'joyreactor.cc'],
"pornreactor.cc": [],
};
/** /**
* Исправляет внутренние ссылки "реактора" * @param {HTMLElement} elmPost
*/ */
function fixReactorLinks() { constructor(elmPost) {
domainAliaces[document.domain].forEach(domain => { const pc_len = 'postContainer'.length;
document.querySelectorAll('a[href^="http://' + domain + '/"]').forEach(elm => {
elm.href = elm.href.replace('http://' + domain + '/', 'http://' + document.domain + '/');
});
document.querySelectorAll('a[href^="//' + domain + '/"]').forEach(elm => { /** @public {Number} */
elm.href = elm.href.replace('//' + domain + '/', '//' + document.domain + '/'); this.id = parseInt(elmPost.id.substring(pc_len));
});
}); /** @public {String} */
this.author = elmPost.getElementsByClassName('uhead_nick')[0].getElementsByTagName('a')[0].text;
/** @public {String[]} */
this.tags = [];
for (const elmChild of elmPost.getElementsByClassName('taglist')[0].children) {
this.tags.push(elmChild.textContent.trim());
}
}
} }
function createTreeButton(listId) { class Comment {
var aTag = document.createElement('a'); /**
* @param {HTMLElement} elmComment
*/
constructor(elmComment) {
const cm_len = 'comment'.length;
/** @public {HTMLElement} */
this.element = elmComment;
/** @public {Number} */
this.id = parseInt(elmComment.id.substring(cm_len));
const elmsTxt = elmComment.getElementsByClassName('txt');
/** @public {String} */
this.author = null;
/** @public {String} */
this.text = null;
if (elmsTxt.length > 0) {
let elms = elmsTxt[0].getElementsByClassName('comment_username');
if (elms.length > 0) {
this.author = elmsTxt[0].getElementsByClassName('comment_username')[0].textContent;
this.text = elmsTxt[0].children[0].textContent;
} else if (elmComment.getElementsByClassName('comment_show').length > 0) {
this.author = elmComment.getElementsByClassName('comments_bottom')[0].children[1].children[0].textContent;
this.text = '[HIDDEN COMMENT]';
}
}
if (this.author === null) {
throw new Error('author is NULL');
}
/** @public {String} */
this.image = null;
if (elmComment.getElementsByClassName('image').length > 0) {
this.image = elmComment.getElementsByClassName('image')[0].children[0].href;
}
/** @public {Comment[]} */
this.children = [];
for (const childComment of document.getElementById('comment_list_comment_' + this.id).children) {
if (childComment.id.startsWith('comment_list_comment_')) {
continue;
}
try {
this.children.push(new Comment(childComment));
} catch (e) {
console.error("--- ERROR ---");
console.error(childComment);
console.error(e);
console.error("--- END ---");
}
}
}
}
class JRApi {
constructor() {
/** @private {RegExp} */
this._urlPostRegexp = new RegExp("https?:\/\/.+/post/([0-9]+)");
/** @private {Comment[]} */
this._comments = null;
}
/**
* @return {Boolean}
*/
isPostPage() {
return (document.URL.match(this._urlPostRegexp) !== null);
}
/**
* @return {Post}
*/
post() {
const postId = document.URL.match(this._urlPostRegexp)[1];
const elmPostContainer = document.getElementById('postContainer' + postId);
return new Post(elmPostContainer);
}
/**
* @return {Post[]}
*/
posts() {
const elmPostList = document.getElementById('post_list');
const postList = [];
for (const elmPostContainer of elmPostList.children) {
postList.push(new Post(elmPostContainer));
}
return postList;
}
/**
* @return {Comment[]}
*/
comments() {
if (this._comments == null) {
const postId = document.URL.match(this._urlPostRegexp)[1];
this._comments = [];
for (const elmComment of document.getElementById('comment_list_post_' + postId).children) {
if (elmComment.id.startsWith('comment_list_comment_')) {
continue;
}
this._comments.push(new Comment(elmComment));
}
}
return this._comments;
}
}
class JRMod {
/**
* @param {JRApi} jrApi
*/
constructor(jrApi) {
this.jrApi = jrApi;
}
/**
* Добавляем кнопку сворачивания ветки комментариев
*/
addCollapseTreeCommentsButton() {
/**
* @param {Comment} comment
* @return {HTMLElement}
*/
const createButton = function (comment) {
const aTag = document.createElement('a');
aTag.href = "#"; aTag.href = "#";
aTag.classList.toggle('collapse-comments'); aTag.innerHTML = '➖';
aTag.text = '[-]';
aTag.dataset.listid = listId;
aTag.style.fontWeight = 'bold'; aTag.style.fontWeight = 'bold';
aTag.style.color = '#656464'; aTag.style.color = '#656464';
aTag.style.lineHeight = '31px'; aTag.style.lineHeight = '31px';
aTag.style.textDecoration = 'none'; aTag.style.textDecoration = 'none';
aTag.style.margin = '0 10px'; aTag.style.margin = '0 10px';
aTag.style.padding = '0 10px'; aTag.style.padding = '0 10px'
aTag.style.borderRadius = '15px'; aTag.style.borderRadius = '15px';
aTag.style.background = '#dfdfdf'; aTag.style.background = '#dfdfdf';
const elmSubCommentList = document.getElementById('comment_list_comment_' + comment.id);
aTag.onclick = function () { aTag.onclick = function () {
var listId = this.dataset.listid; if (elmSubCommentList.style.display === 'none') {
var commentListElement = document.getElementById(listId); elmSubCommentList.style.display = '';
aTag.innerHTML = '➖'; // , -
if (commentListElement.style.display === 'none') {
commentListElement.style.display = '';
this.text = '[-]';
} else { } else {
commentListElement.style.display = 'none'; elmSubCommentList.style.display = 'none';
this.text = '[+]'; aTag.innerHTML = '➕'; // , +
} }
return false; return false;
}; }
return aTag; return aTag;
} }
/** /** @param {Comment} comment */
* Добавляем кнопку сворачивания комментариев const func = function (comment) {
*/ if (comment.children.length === 0) return;
function collapseTreeComments() { comment.element.getElementsByClassName('reply-link')[0].appendChild(createButton(comment));
var blocks = document.getElementsByClassName('post_comment_list'); comment.children.forEach(comment => func(comment));
var commentBlock = blocks.length === 2 ? blocks[1] : blocks[0];
Array.from(commentBlock.querySelectorAll('div.comment'))
.filter(elm => { return elm.nextElementSibling !== null; })
.filter(elm => { return elm.nextElementSibling.children.length > 0; })
.forEach(elm => {
var listId = elm.nextElementSibling.id;
elm.querySelector('span.reply-link').appendChild(createTreeButton(listId));
});
} }
function isPostPage() { this.jrApi.comments().forEach(comment => func(comment));
for (var domain of Object.keys(domainAliaces)) {
let regexp = new RegExp("http?:\/\/" + domain.replace(/\./g, '\\.') + "\/post");
if (document.URL.match(regexp) !== null) {
return true;
}
}
return false;
} }
/** /**
* Удаляем лишние элементы с сайта * Удалить пункты меню "Люди" и "О проекте"
*/ */
function removeExcessElements() { removeTopMenuItems() {
// Удаляем пункты меню "Люди" и "О проекте" //@formatter:off
Array.from(document.querySelectorAll('#navlist li')) Array.from(document.querySelectorAll('#navlist li'))
.filter(elm => { return elm.querySelector('a').text === "Люди" .filter(elm => { return elm.querySelector('a').text === "Люди"
|| elm.querySelector('a').text === "О проекте" }) || elm.querySelector('a').text === "О проекте" })
.forEach(elm => { elm.remove() }); .forEach(elm => { elm.remove() });
//@formatter:on
}
// Удаляем правую колонку /**
* Удалить правую колонку
*/
removeRightColumn() {
document.getElementById('sidebar').remove(); document.getElementById('sidebar').remove();
// Косметические исправления const width = '930px';
var width = '930px';
document.getElementById('header').style.width = width; document.getElementById('header').style.width = width;
document.getElementById('page').style.width = width; document.getElementById('page').style.width = width;
document.getElementById('topbar').children[0].style.width = width; document.getElementById('topbar').children[0].style.width = width;
document.getElementById('container').style.minWidth = '0'; document.getElementById('container').style.minWidth = '0';
}
// Переключатель языков /**
* Удалить переключатель языков
*/
removeLangSwitch() {
Array.from(document.getElementsByClassName('lang_select')).forEach(elm => { elm.remove() }); Array.from(document.getElementsByClassName('lang_select')).forEach(elm => { elm.remove() });
} }
/** /**
* Удаляем лишние элементы из поста * Удалить из поста блоки "Тренды" и "Фэндомы"
*/ */
function removeExcessElementsInPost() { removeTrandsAndFandomsBlock() {
// Удаляем "Тренды" и "Фэндомы" const elements = document.getElementsByClassName('additional_info');
var elements = document.getElementsByClassName('additional_info'); if (elements.length > 0) {
if (elements.length === 1) {
elements[0].remove(); elements[0].remove();
} }
}
// Удаляем "Похожие посты" /**
elements = Array.from(document.getElementsByClassName('mainheader')) * Удалить из поста блок "Похожие посты"
*/
removeSimilarPostsBlock() {
const elements = Array.from(document.getElementsByClassName('mainheader'))
.filter(elm => { return elm.innerText === "Похожие посты" }); .filter(elm => { return elm.innerText === "Похожие посты" });
if (elements.length === 1) { if (elements.length === 1) {
elements[0].nextElementSibling.remove(); elements[0].nextElementSibling.remove();
elements[0].remove(); elements[0].remove();
} }
} }
/**
* Все "новые комментарии" перестают подсвечиваться
*/
function unNewComments() {
document.querySelectorAll('.new').forEach(elm => { elm.classList.toggle('new') });
} }
(function() { (function() {
'use strict'; 'use strict';
const api = new JRApi();
const mod = new JRMod(api);
removeExcessElements(); mod.removeLangSwitch();
fixReactorLinks(); mod.removeTopMenuItems();
if (isPostPage()) { mod.removeRightColumn();
removeExcessElementsInPost();
collapseTreeComments(); if (api.isPostPage()) {
unNewComments(); mod.addCollapseTreeCommentsButton();
mod.removeTrandsAndFandomsBlock()
mod.removeSimilarPostsBlock()
} else {
console.log(api.posts());
} }
})(); })();