Samlib Reader

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

当前为 2021-01-11 提交的版本,查看 最新版本

  1. // ==UserScript==
  2. // @name Samlib Reader
  3. // @description Делает самиздатовские текста более читабельными: смена фона на тёмный, смена шрифта на verdana, смена цвета текста на светлый, текст выровнен по ширине строки, добавлен автоматический перенос слов
  4. // @copyright 2019, Angens (https://openuserjs.org/users/angens)
  5. // @license MIT
  6. // @version 2.0.4
  7. // @match http://samlib.ru/*/*/*.shtml
  8. // @exclude http://samlib.ru/*/*/index*.shtml
  9. // @exclude http://samlib.ru/*/*/stat.shtml
  10. // @grant none
  11. // @namespace https://greasyfork.org/users/386214
  12. // ==/UserScript==
  13. //
  14. // ==OpenUserJS==
  15. // @author angens
  16. // ==/OpenUserJS==
  17.  
  18.  
  19.  
  20. function changer(){
  21. // Прописываем в тело возможность переноса слов
  22. document.querySelector("body").setAttribute("lang", "ru");
  23. document.querySelector("body").setAttribute("style", "-moz-hyphens: auto; -webkit-hyphens: auto; -ms-hyphens: auto; white-space: unset;");
  24. /*
  25. * Блок работы с описанием
  26. */
  27. // Ищем комментарии на первом уровне тела
  28. let numOfComments = 0;
  29. let lastComment;
  30. let bodyChildren = document.body.childNodes;
  31. for (let i = 0; i < bodyChildren.length; i++){
  32. let node = bodyChildren[i];
  33. if (node.nodeType === 8){
  34. numOfComments++;
  35. lastComment = node;
  36. }
  37. }
  38. // Создаём блок описания
  39. let description = document.createElement('div');
  40. description.id = "SamLibReaderDescription";
  41. // Если комментарии начала и конца произведения дети body — сразу записываем всё под нижним комментарием в блок описания
  42. if(numOfComments === 2){
  43. while(lastComment.nextSibling){
  44. description.append(lastComment.nextSibling);
  45. }
  46. }
  47. // Ищем комментарий описания и находить главный блок. Все последующие отправляем в блок описания
  48. if(numOfComments === 1){
  49. let treeWalker = document.createTreeWalker(document.body, NodeFilter.SHOW_ALL, null, false);
  50. while (treeWalker.nextNode()){
  51. if (treeWalker.currentNode.nodeType === 8){
  52. if (treeWalker.currentNode.nodeValue == "-- Блок описания произведения (слева внизу) ---------------------"){
  53. let node = treeWalker.currentNode.parentElement.parentElement.parentElement;
  54. while(node.nextSibling){
  55. description.append(node.nextSibling);
  56. }
  57. description.insertBefore(node, description.firstChild);
  58. }
  59. }
  60. }
  61. }
  62. /*
  63. * Блок работы с текстом
  64. */
  65. // Блок работы с произведением
  66. let nodes = document.querySelectorAll("dd, p");
  67.  
  68. let new_element = document.createElement('div');
  69.  
  70. new_element.id = "SamLibReader";
  71. new_element.style.color = "wheat";
  72. new_element.style.fontSize = "18px";
  73. new_element.style.fontFamily = "verdana";
  74. new_element.style.backgroundColor = "#212127";
  75. new_element.style.paddingTop = "5%";
  76. new_element.style.paddingLeft = "5%";
  77. new_element.style.paddingRight = "5%";
  78. new_element.style.marginLeft = "25%";
  79. new_element.style.marginRight = "25%";
  80. new_element.style.textAlign = "justify";
  81. if(nodes[0])
  82. nodes[0].parentElement.insertBefore(new_element, nodes[0]);
  83.  
  84. for (let i = 0; i < nodes.length; i++){
  85. new_element.append(nodes[i]);
  86. }
  87. document.body.append(description);
  88. }
  89.  
  90.  
  91. document.addEventListener("DOMContentLoaded", changer());