Download Wattpad Chapter

Download the content of the Wattpad chapter as a text file by clicking a button. The button is designed to integrate with the appearance of the Wattpad page.

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

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

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

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

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

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

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

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

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

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

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

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

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

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

// ==UserScript==
// @name         Download Wattpad Chapter
// @namespace    http://wattpad.com/*
// @version      0.1
// @description  Download the content of the Wattpad chapter as a text file by clicking a button. The button is designed to integrate with the appearance of the Wattpad page.
// @author       Dj Dragkan
// @match        *://www.wattpad.com/*
// @grant        none
// @license      MIT
// ==/UserScript==

(function() {
    'use strict';

    // Function to copy the text of an element to the clipboard
    function copyText(text) {
        // Copy text to clipboard
        navigator.clipboard.writeText(text)
            .catch(function() {
                alert('Error copying text.');
            });
    }

    // Function to download text as a text file
    function downloadFile(text) {
        // Create a Blob object with the text
        var blob = new Blob([text], { type: 'text/plain' });

        // Create a URL object for the Blob
        var url = window.URL.createObjectURL(blob);

        // Create a download link
        var downloadLink = document.createElement('a');
        downloadLink.href = url;
        downloadLink.download = 'downloaded_chapter.txt';

        // Click the link to initiate the download
        downloadLink.click();

        // Revoke the URL object
        window.URL.revokeObjectURL(url);
    }

    // Create a download chapter button
    function createDownloadChapterButton() {
        var downloadChapterButton = document.createElement('button');
        downloadChapterButton.innerText = 'DOWNLOAD CHAPTER';
        downloadChapterButton.style.position = 'fixed';
        downloadChapterButton.style.top = '10px';
        downloadChapterButton.style.left = '50%';
        downloadChapterButton.style.transform = 'translateX(-50%)';
        downloadChapterButton.style.zIndex = '9999';
        downloadChapterButton.style.backgroundColor = '#FFA500';
        downloadChapterButton.style.color = 'white';
        downloadChapterButton.style.fontWeight = 'bold';
        downloadChapterButton.style.border = 'none';
        downloadChapterButton.style.borderRadius = '5px';
        downloadChapterButton.style.padding = '10px 20px';
        downloadChapterButton.style.cursor = 'pointer';

        // Add the Wattpad icon to the button
        var wattpadIcon = document.createElement('img');
        wattpadIcon.src = 'https://www.wattpad.com/apple-touch-icon-114x114-precomposed.png';
        wattpadIcon.style.width = '20px'; // Adjust size as needed
        wattpadIcon.style.verticalAlign = 'middle'; // Align vertically with text
        downloadChapterButton.appendChild(wattpadIcon);

        // Add the button to the document body
        document.body.appendChild(downloadChapterButton);

        // Add click event to the button
        downloadChapterButton.addEventListener('click', function() {
            // Get all <p> elements with the data-p-id attribute
            var elements = document.querySelectorAll('p[data-p-id]');
            if (elements.length > 0) {
                var totalText = '';
                elements.forEach(function(element) {
                    totalText += element.innerText + '\n';
                });
                copyText(totalText);
                downloadFile(totalText);
            } else {
                alert('No <p> elements with the "data-p-id" attribute were found.');
            }
        });
    }

    // Call the function to create the download chapter button when the page loads
    window.addEventListener('load', createDownloadChapterButton);
})();