Syosetu Chapter Copy

Copy the chapter content from a Syosetu chapter page.

当前为 2020-09-13 提交的版本,查看 最新版本

  1. // ==UserScript==
  2. // @name Syosetu Chapter Copy
  3. // @namespace ultrabenosaurus.Syosetu
  4. // @version 0.8
  5. // @description Copy the chapter content from a Syosetu chapter page.
  6. // @author Ultrabenosaurus
  7. // @source https://greasyfork.org/en/users/437117-ultrabenosaurus?sort=name
  8. // @match https://ncode.syosetu.com/*/*/
  9. // @grant GM.setClipboard
  10. // @grant GM_setClipboard
  11. // ==/UserScript==
  12.  
  13. (function() {
  14. 'use strict';
  15.  
  16. /*
  17. *
  18. * maxChars: The maximum number of characters to copy at once. If the whole
  19. * chapter is less than maxChars there will only be one button which
  20. * will copy the whole chapter. If it is longer then there will be 2
  21. * buttons, with the second copying "blocks" of maxChars characters.
  22. *
  23. * safetyNet: How many characters to overlap from the previous block. This is
  24. * so that there is some context for your translation tool to work
  25. * with in the case that a sentence would otherwise be cut in half.
  26. *
  27. * e.g. the first block will be 5000 - 100 = 4900 characters, subsequent blocks
  28. * will be 5000 chars including the last 100 chars from the previous block and
  29. * the 4900 of the current block.
  30. *
  31. * Both are also set at the start of the UBsyosetuChapterCopyPart() function.
  32. *
  33. */
  34. var maxChars = 5000;
  35. var safetyNet = 100;
  36.  
  37. var mobile = 0;
  38.  
  39. let txtBox = document.createElement("textarea");
  40. txtBox.id = "UBsyosetuChapterContent";
  41. txtBox.name = "UBsyosetuChapterContent";
  42. txtBox.style = "width: 0; height: 0; border: none;";
  43. document.body.appendChild(txtBox);
  44.  
  45. var subTitle = document.querySelectorAll('div#novel_color p.novel_subtitle');
  46. if(subTitle.length == 0) {
  47. mobile = 1;
  48. subTitle = document.querySelectorAll('div#novel_color div.novel_subtitle');
  49. }
  50.  
  51. txtBox.value = (mobile ? subTitle[0].innerText.split("\n")[1] : subTitle[0].textContent) + "\n\n" + document.querySelectorAll('div#novel_honbun')[0].textContent;
  52. var charCount = txtBox.value.length;
  53.  
  54. var btnElem = "<br /><input type='button' id='UBsyosetuChapterCopyWhole' value='Copy Whole Chapter' />";
  55. if( charCount > maxChars ) {
  56. btnElem += "&nbsp;<input type='button' id='UBsyosetuChapterCopyPart' value='Copy Chapter Block: 1 of " + Math.ceil( charCount / ( maxChars - safetyNet ) ) + "' />";
  57. }
  58.  
  59. subTitle[0].insertAdjacentHTML("beforeend", btnElem);
  60.  
  61. var sccwBtn = document.getElementById('UBsyosetuChapterCopyWhole');
  62. if(sccwBtn){
  63. sccwBtn.addEventListener("click", UBsyosetuChapterCopyWhole, false);
  64. }
  65.  
  66. var sccpBtn = document.getElementById('UBsyosetuChapterCopyPart');
  67. if(sccpBtn){
  68. sccpBtn.addEventListener("click", UBsyosetuChapterCopyPart, false);
  69. }
  70.  
  71. maxChars = safetyNet = mobile = sccwBtn = sccpBtn = btnElem = txtBox = subTitle = charCount = null;
  72. })();
  73.  
  74. function UBsyosetuChapterCopyWhole() {
  75. let txtBox = document.getElementById('UBsyosetuChapterContent');
  76. txtBox.select();
  77. txtBox.setSelectionRange(0, 999999);
  78. document.execCommand("copy");
  79. txtBox = null;
  80. }
  81.  
  82. function UBsyosetuChapterCopyPart() {
  83. var maxChars = 5000;
  84. var safetyNet = 100;
  85.  
  86. var copyBtn = document.getElementById('UBsyosetuChapterCopyPart');
  87. var chpPart = parseInt( copyBtn.value.split(": ")[1].split(" of ")[0] );
  88. var lastPart = parseInt( copyBtn.value.split(": ")[1].split(" of ")[1] );
  89. let txtBox = document.getElementById('UBsyosetuChapterContent');
  90. var maxBlocks = Math.ceil( txtBox.value.length / ( maxChars - safetyNet ) );
  91.  
  92. if( maxBlocks >= chpPart ) {
  93. var selRngStart = ( chpPart - 1 ) * ( maxChars - safetyNet );
  94. selRngStart = chpPart > 1 ? selRngStart - safetyNet : selRngStart;
  95. var selRngEnd = chpPart * ( maxChars - safetyNet );
  96.  
  97. txtBox.select();
  98. txtBox.setSelectionRange( selRngStart, selRngEnd );
  99. document.execCommand("copy");
  100.  
  101. copyBtn.value = copyBtn.value.split(": ")[0] + ": " + ( chpPart + 1 ) + " of " + lastPart;
  102. } else {
  103. copyBtn.value = copyBtn.value.split(": ")[0] + ": 1 of " + lastPart;
  104. alert("You have already finished this chapter. Click again to start copying from the first block.");
  105. }
  106.  
  107. maxChars = safetyNet = maxBlocks = copyBtn = chpPart = lastPart = selRngStart = selRngEnd = txtBox = null;
  108. }
  109.  
  110.  
  111.  
  112.  
  113. //