Samlib Reader

Делает самиздатовские текста более читабельными: смена фона на тёмный, смена шрифта на verdana, смена цвета текста на светлый, текст выровнен по ширине строки, добавлен автоматический перенос слов

目前為 2021-01-14 提交的版本,檢視 最新版本

您需要先安裝使用者腳本管理器擴展,如 TampermonkeyGreasemonkeyViolentmonkey 之後才能安裝該腳本。

You will need to install an extension such as Tampermonkey to install this script.

您需要先安裝使用者腳本管理器擴充功能,如 TampermonkeyViolentmonkey 後才能安裝該腳本。

您需要先安裝使用者腳本管理器擴充功能,如 TampermonkeyUserscripts 後才能安裝該腳本。

你需要先安裝一款使用者腳本管理器擴展,比如 Tampermonkey,才能安裝此腳本

您需要先安裝使用者腳本管理器擴充功能後才能安裝該腳本。

(我已經安裝了使用者腳本管理器,讓我安裝!)

你需要先安裝一款使用者樣式管理器擴展,比如 Stylus,才能安裝此樣式

你需要先安裝一款使用者樣式管理器擴展,比如 Stylus,才能安裝此樣式

你需要先安裝一款使用者樣式管理器擴展,比如 Stylus,才能安裝此樣式

你需要先安裝一款使用者樣式管理器擴展後才能安裝此樣式

你需要先安裝一款使用者樣式管理器擴展後才能安裝此樣式

你需要先安裝一款使用者樣式管理器擴展後才能安裝此樣式

(我已經安裝了使用者樣式管理器,讓我安裝!)

// ==UserScript==
// @name Samlib Reader
// @description Делает самиздатовские текста более читабельными: смена фона на тёмный, смена шрифта на verdana, смена цвета текста на светлый, текст выровнен по ширине строки, добавлен автоматический перенос слов
// @copyright 2019, Angens (https://openuserjs.org/users/angens)
// @license MIT
// @version 2.0.5
// @match http://samlib.ru/*/*/*.shtml
// @exclude http://samlib.ru/*/*/index*.shtml
// @exclude http://samlib.ru/*/*/stat.shtml
// @grant none
// @namespace https://greasyfork.org/users/386214
// ==/UserScript==
//
// ==OpenUserJS==
// @author angens
// ==/OpenUserJS==



function changer(){
  
  // Прописываем в тело возможность переноса слов
  document.querySelector("body").setAttribute("lang", "ru");
  document.querySelector("body").setAttribute("style", "-moz-hyphens: auto; -webkit-hyphens: auto; -ms-hyphens: auto; white-space: unset;");
  
  /*
   * Блок работы с описанием
   */
  
  // Ищем комментарии на первом уровне тела
  let numOfComments = 0;
  let lastComment;
  let bodyChildren = document.body.childNodes;
  for (let i = 0; i < bodyChildren.length; i++){
    let node = bodyChildren[i];
    if (node.nodeType === 8){
      numOfComments++;
      lastComment = node;
    }
  }
  
  // Создаём блок описания
  let description = document.createElement('div');
  description.id = "SamLibReaderDescription"; 
  
  // Если комментарии начала и конца произведения дети body — сразу записываем всё под нижним комментарием в блок описания
  if(numOfComments === 2){
    while(lastComment.nextSibling){
      description.append(lastComment.nextSibling);
    }
  }
  
  // Ищем комментарий описания и находить главный блок. Все последующие отправляем в блок описания
  if(numOfComments === 1){
    let treeWalker = document.createTreeWalker(document.body, NodeFilter.SHOW_ALL, null, false);
    
    while (treeWalker.nextNode()){
      if (treeWalker.currentNode.nodeType === 8){
        if (treeWalker.currentNode.nodeValue == "-- Блок описания произведения (слева внизу) ---------------------"){
          let node = treeWalker.currentNode.parentElement.parentElement.parentElement;
          while(node.nextSibling){
            description.append(node.nextSibling);
          }
          description.insertBefore(node, description.firstChild);
        }
      }
    }
  }
    
  
  /*
   *  Блок работы с текстом
   */
  // Блок работы с произведением
  let nodes = document.querySelectorAll("dd, p");
 
  let new_element = document.createElement('div');
 
  new_element.id = "SamLibReader";
  new_element.style.color = "wheat";
  new_element.style.fontSize = "18px";
  new_element.style.fontFamily = "verdana";
  new_element.style.backgroundColor = "#212127";
  new_element.style.paddingTop = "5%";
  new_element.style.padding = "5%";
  new_element.style.marginLeft = "25%";
  new_element.style.marginRight = "25%";
  new_element.style.textAlign = "justify";
  
  if(nodes[0])
    nodes[0].parentElement.insertBefore(new_element, nodes[0]);
 
  for (let i = 0; i < nodes.length; i++){
    new_element.append(nodes[i]);
  }
  
  document.body.append(description);
  
  /*
   *  Блок работы с тегом <pre>
   */
  let pre = document.querySelectorAll("pre");
  for (let i = 0; i < pre.length; i++){
    pre[i].style.marginLeft = 'auto';
    pre[i].style.marginRight = 'auto';
    pre[i].style.width = 'min-content';
    pre[i].style.color = "wheat";
    pre[i].style.fontSize = "18px";
    pre[i].style.fontFamily = "roboto condensed, verdana";
    pre[i].style.backgroundColor = "#212127";
    pre[i].style.padding = '5%';
  }
}
 
 
document.addEventListener("DOMContentLoaded", changer());