Greasy Fork

Greasy Fork is available in English.

Syosetu Chapter Copy

Copy the chapter content from a Syosetu chapter page.

目前为 2020-09-10 提交的版本。查看 最新版本

// ==UserScript==
// @name         Syosetu Chapter Copy
// @namespace    ultrabenosaurus.Syosetu
// @version      0.6
// @description  Copy the chapter content from a Syosetu chapter page.
// @author       Ultrabenosaurus
// @source       http://greasyfork.icu/en/users/437117-ultrabenosaurus?sort=name
// @match        https://ncode.syosetu.com/*/*/
// @grant        GM.setClipboard
// @grant        GM_setClipboard
// ==/UserScript==

(function() {
    'use strict';

    var maxChars = 5000; // 5000 = DeepL free user max characters. Also set at the start of the UBsyosetuChapterCopyPart() function.
    var mobile = 0;

    let txtBox = document.createElement("textarea");
    txtBox.id = "UBsyosetuChapterContent";
    txtBox.name = "UBsyosetuChapterContent";
    txtBox.style = "width: 0; height: 0; border: none;";
    document.body.appendChild(txtBox);

    var subTitle = document.querySelectorAll('div#novel_color p.novel_subtitle');
    if(subTitle.length == 0) {
        mobile = 1;
        subTitle = document.querySelectorAll('div#novel_color div.novel_subtitle');
    }

    txtBox.value = (mobile ? subTitle[0].innerText.split("\n")[1] : subTitle[0].textContent) + "\n\n" + document.querySelectorAll('div#novel_honbun')[0].textContent;
    var charCount = txtBox.value.length;

    var btnElem = "<br /><input type='button' id='UBsyosetuChapterCopyWhole' value='Copy Whole Chapter' />";
    if( charCount > maxChars ) {
        btnElem += "&nbsp;<input type='button' id='UBsyosetuChapterCopyPart' value='Copy Chapter Block: 1 of " + Math.ceil( charCount / maxChars ) + "' />";
    }

    subTitle[0].insertAdjacentHTML("beforeend", btnElem);

    var sccwBtn = document.getElementById('UBsyosetuChapterCopyWhole');
    if(sccwBtn){
        sccwBtn.addEventListener("click", UBsyosetuChapterCopyWhole, false);
    }

    var sccpBtn = document.getElementById('UBsyosetuChapterCopyPart');
    if(sccpBtn){
        sccpBtn.addEventListener("click", UBsyosetuChapterCopyPart, false);
    }

    maxChars = mobile = sccwBtn = sccpBtn = btnElem = txtBox = subTitle = charCount = null;
})();

function UBsyosetuChapterCopyWhole() {
    let txtBox = document.getElementById('UBsyosetuChapterContent');
    txtBox.select();
    txtBox.setSelectionRange(0, 999999);
    document.execCommand("copy");
    txtBox = null;
}

function UBsyosetuChapterCopyPart() {
    var maxChars = 5000;

    var copyBtn = document.getElementById('UBsyosetuChapterCopyPart');
    var chpPart = parseInt( copyBtn.value.split(": ")[1].split(" of ")[0] );
    var lastPart = parseInt( copyBtn.value.split(": ")[1].split(" of ")[1] );
    //console.log( chpPart, typeof chpPart );
    let txtBox = document.getElementById('UBsyosetuChapterContent');
    var maxBlocks = Math.ceil( txtBox.value.length / maxChars );

    if( maxBlocks >= chpPart ) {
        var selRngStart = ( chpPart - 1 ) * maxChars;
        var selRngEnd = chpPart * maxChars;

        txtBox.select();
        txtBox.setSelectionRange( selRngStart, selRngEnd );
        document.execCommand("copy");

        copyBtn.value = copyBtn.value.split(": ")[0] + ": " + ( chpPart + 1 ) + " of " + lastPart;
    } else {
        copyBtn.value = copyBtn.value.split(": ")[0] + ": 1 of " + lastPart;
        alert("You have already finished this chapter. Click again to start copying from the first block.");
    }

    maxChars = maxBlocks = copyBtn = chpPart = lastPart = selRngStart = selRngEnd = txtBox = null;
}




//