ao3 tweak formatting

quick tools for text formatting

  1. // ==UserScript==
  2. // @name ao3 tweak formatting
  3. // @namespace https://greatest.deepsurf.us/en/users/36620
  4. // @version 2.3.0
  5. // @description quick tools for text formatting
  6. // @author scriptfairy
  7. // @include /https?://archiveofourown\.org/.*works/\d+/
  8. // @grant none
  9. // ==/UserScript==
  10.  
  11. function doubleBreak(ch) {
  12. ch.innerHTML = ch.innerHTML.replace(/<br>/g,'<br><br>').replace(/<br\/>/g,'<br><br>');
  13. }
  14.  
  15. function deSpace(ch) {
  16. var noBreak = document.createElement("style");
  17. noBreak.innerText = '#chapters br+br {display:none}';
  18. noBreak.type = 'text/css';
  19. document.head.appendChild(noBreak);
  20. ch.innerHTML = ch.innerHTML.replace(/&nbsp;/g, ' ');
  21. }
  22.  
  23. function stripAlign(ch) {
  24. ch.innerHTML = ch.innerHTML.replace(/align="(left|center|align|justify)"/g, '');
  25. }
  26.  
  27. function stripItalics(ch) {
  28. ch.innerHTML = ch.innerHTML.replace(/<em>/g,'').replace(/<\i>/g,'');
  29. }
  30.  
  31. function deAsterisk(ch) {
  32. ch.innerHTML = ch.innerHTML.replace(/\*/g,'<i>').replace(/<i>\s/g,'</i>').replace(/<i>[^A-Za-z0-9]/g,'</i>');
  33. }
  34.  
  35. function noTypewriter(ch) {
  36. ch.innerHTML = ch.innerHTML.replace(/(&nbsp;| ){2}/g,'&nbsp;');
  37. }
  38.  
  39. function noEllipses(ch) {
  40. ch.innerHTML = ch.innerHTML.replace(/\.\.\./g, '.');
  41. }
  42.  
  43. //
  44. var chapter = document.getElementById('chapters');
  45.  
  46. var links = document.createElement('div');
  47. links.innerHTML = '<span id="tweakFormat" class="click">Tweak Format</span>'
  48. + '<ul class="format-options">'
  49. + '<li><a id="deSpace">remove line breaks</a></li>'
  50. + '<li><a id="doubleBreak">insert line breaks</a></li>'
  51. + '<li><a id="stripAlign">align to default</a></li>'
  52. + '<li><a id="stripItalics">strip italics</a></li>'
  53. + '<li><a id="noEllipses">strip ellipses</a></li>'
  54. + '<li><a id="noTypewriter">remove double spaces</a></li>'
  55. + '<li><a id="deAsterisk">*word* to <em>word</em> (exp.)</a></li>'
  56. + '</ul>';
  57. links.classList.add('tweak-format');
  58.  
  59. var linksFormat = document.createElement('style');
  60. linksFormat.innerText = '.tweak-format {text-align:right; font-size:small; cursor:pointer}'
  61. + '.tweak-format .click+.format-options {display:none;}'
  62. + '.tweak-format .click::before {content:"\\25b6 \\0020";}'
  63. + '.tweak-format .clicked+.format-options {display:block;}'
  64. + '.tweak-format .clicked::before {content:"\\25bc \\0020";}';
  65. linksFormat.type = 'text/css';
  66.  
  67. document.head.appendChild(linksFormat);
  68. chapter.parentNode.insertBefore(links, chapter);
  69.  
  70. document.getElementById('deAsterisk').onclick = function() {deAsterisk(chapter);};
  71. document.getElementById('stripItalics').onclick = function() {stripItalics(chapter);};
  72. document.getElementById('stripAlign').onclick = function() {stripAlign(chapter);};
  73. document.getElementById('doubleBreak').onclick = function() {doubleBreak(chapter);};
  74. document.getElementById('deSpace').onclick = function() {deSpace(chapter);};
  75. document.getElementById('noTypewriter').onclick = function() {noTypewriter(chapter);};
  76. document.getElementById('noEllipses').onclick = function() {noEllipses(chapter);};
  77.  
  78. document.getElementById('tweakFormat').onclick = function() {
  79. if (this.classList.contains('click')) {
  80. this.classList.remove('click');
  81. this.classList += ' clicked';
  82. } else {
  83. this.classList.remove('clicked');
  84. this.classList += ' click';
  85. }
  86. };