RoyalRoad AI Rewriter Enhanced

Advanced RoyalRoad chapter rewriter with Gemini AI - improved reliability and features

作者
hubcap-hug-liking
日安装量
0
总安装量
0
评分
0 0 0
版本
2.0
创建于
2025-07-26
更新于
2025-07-26
大小
43.5 KB
许可证
MIT
适用于

RoyalRoad AI Rewriter Enhanced (Version 2.0)

This Tampermonkey user script dramatically improves the reading experience on RoyalRoad by utilizing Google's Gemini AI to rewrite chapter text. It focuses on enhancing reader retention and engagement while preserving the original plot and dialogue.


Table of Contents

  1. Features
  2. Installation
  3. Usage
  4. Settings
  5. How It Works
  6. Troubleshooting
  7. Disclaimer

1. Features

The RoyalRoad AI Rewriter Enhanced script provides the following key features:

  • AI-Powered Rewriting: Leverages Google's Gemini AI (specifically gemini-2.0-flash-exp by default, with options for gemini-1.5-pro and gemini-1.5-flash) to enhance chapter text for better engagement and flow.
  • Plot and Dialogue Preservation: Ensures that the core plot events, character actions, and dialogue content remain unchanged, focusing only on stylistic improvements.
  • Enhanced Readability: Applies various stylistic enhancements such as descriptive pacing, direct language, dialogue-driven exposition, rich sensory details, and varied sentence structures.
  • Consistent Chapter Length: Aims to maintain the original chapter length without significant expansion or compression.
  • Intelligent Chunking: Breaks down long chapters into smaller, manageable chunks for efficient API processing, handling large chapters seamlessly.
  • Progress and Status Updates: Provides real-time feedback on the rewriting process, including progress bars and status messages.
  • Original Content Restoration: Allows users to revert to the original chapter text at any time.
  • Draggable UI Panel: A floating, draggable user interface that integrates cleanly into the RoyalRoad page.
  • API Key Management: Prompts for and securely stores your Gemini API key using Tampermonkey's GM_setValue.
  • Customizable Settings: Offers a settings panel to change the Gemini model, adjust chunk size, and manage your API key.
  • Preview Mode: Provides an analysis preview of the chapter, including character count, word count, number of processing chunks, and estimated processing time.
  • Error Handling & Retries: Includes robust error handling, rate limiting, and retry mechanisms for API requests to ensure reliability.
  • Notifications: Uses Tampermonkey's GM_notification for system-level alerts on process completion or errors.

2. Installation

To use this script, you need a browser extension that supports UserScripts, such as Tampermonkey (recommended for Chrome, Edge, Firefox, and Opera).

  1. Install Tampermonkey:
  * **For Chrome/Edge:** Visit the [Chrome Web Store](https://chrome.google.com/webstore/detail/tampermonkey/dhdgffkkebhmkfjojejmpbldmpobfkfo?hl=en).
  * **For Firefox:** Visit [Firefox Add-ons](https://addons.mozilla.org/en-US/firefox/addon/tampermonkey/).
  * Follow the installation instructions for your browser.
  1. Install the User Script:
  * Click on the **"Raw"** or **"Install"** button on the script's source page (e.g., if you copied this code from a GitHub Gist, there would be a "Raw" button).
  * Tampermonkey will open a new tab with the script's source code.
  * Review the code (optional) and click the **"Install"** button in the Tampermonkey interface.
  1. Obtain a Gemini API Key:
  * Go to [https://ai.google.dev/](https://ai.google.dev/).
  * Sign in with your Google account.
  * Navigate to the "Get API key" section and generate a new API key. This key is necessary for the script to access the Gemini AI.

3. Usage

Once installed and configured, the script will automatically activate when you visit any RoyalRoad chapter page (URLs matching https://www.royalroad.com/fiction/*/chapter/* or https://royalroad.com/fiction/*/chapter/*).

  1. Locate the AI Rewriter Panel: A floating panel titled "🎯 AI Chapter Rewriter" will appear on the top right side of the RoyalRoad chapter page.

  2. Rewrite a Chapter:

  * Click the **"✨ Rewrite Chapter"** button.
  * The first time you use it, a prompt will appear asking for your **Gemini API Key**. Paste the API key you obtained from `ai.google.dev` and click "OK". This key will be saved for future use.
  * The script will begin processing the chapter. A progress bar and status messages will update within the panel.
  * Once complete, the chapter content will be replaced with the AI-rewritten version.
  1. Reset to Original:
  * If you want to revert the chapter to its original text, click the **"↩️ Reset Original"** button.

4. Settings

To customize the script's behavior:

  1. Click the "⚙️ Settings" button on the AI Rewriter panel.
  2. A pop-up modal will appear with the following options:
    • Gemini API Key: Your currently saved API key will be displayed (as asterisks for security). You can update it here.
    • Model Settings: Choose the Gemini model you want to use for rewriting:
      • Gemini 2.0 Flash (Recommended): (gemini-2.0-flash-exp) - Default, balanced for speed and quality.
      • Gemini 1.5 Pro: (gemini-1.5-pro) - Higher quality, potentially slower.
      • Gemini 1.5 Flash: (gemini-1.5-flash) - Faster, potentially lower quality.
    • Chunk Size (characters): Adjust the maximum number of characters per text chunk sent to the AI. Smaller chunks lead to more API calls but can improve quality for very long chapters. The default is 35000.
  3. Click "Save" to apply your changes or "Cancel" to close without saving.

Preview Mode

Clicking the "👁️ Preview" button provides a quick analysis of the current chapter:

  • Total characters and word count.
  • Number of chunks the chapter will be split into for processing.
  • Estimated number of API calls and processing time.
  • A preview of the first chunk of text that will be sent to the AI.

5. How It Works

The script operates through several key components:

  1. UI Manager: Creates and manages the floating control panel, handling button clicks, status updates, and progress display. It also makes the panel draggable for user convenience.
  2. Content Manager:
    • Identifies the main chapter content on the RoyalRoad page using various selectors.
    • Extracts and sanitizes the text, removing unwanted elements like ads or author notes.
    • Implements an intelligent chunking mechanism that splits the chapter into smaller pieces (paragraphs are preserved within chunks) to fit within the AI model's token limits.
    • Handles replacing the original chapter HTML with the rewritten text and can restore the original content.
  3. API Manager:
    • Manages the Gemini API key, prompting the user if it's not found.
    • Constructs the AI prompt, including the specific REWRITE_PROMPT that guides the Gemini AI on how to rewrite the text.
    • Makes GM_xmlhttpRequest calls to the Gemini API endpoint.
    • Includes rate limiting and retry logic to handle API request failures (e.g., rate limits, network errors, timeouts) gracefully.
    • Configures generationConfig (like temperature, topK, topP) and safetySettings to ensure appropriate and creative output.
  4. Rewrite Process:
    • When "Rewrite Chapter" is clicked, the script first finds and extracts the original chapter text.
    • The text is then divided into chunks.
    • Each chunk is sent sequentially to the Gemini API with the specified prompt.
    • The rewritten chunks are collected and then rejoined to form the complete rewritten chapter.
    • Finally, the original chapter content on the page is replaced with the newly generated text.

6. Troubleshooting

  • "No API key provided" / API Key Prompt Loops: Ensure you've obtained a valid API key from ai.google.dev and paste it correctly when prompted. Double-check for extra spaces.
  • "Could not locate chapter content": The script might not be able to find the main chapter text on the page. Try refreshing the page. If the issue persists, RoyalRoad might have updated its page structure.
  • "Chapter too short": The script has a minimum character length (200 characters by default) to prevent processing very short or irrelevant content.
  • "Failed to process section X" / API Errors:
    • This could be due to network issues, rate limits from Google, or an invalid API key. The script includes retry logic, but persistent errors might require checking your internet connection or your API key status on ai.google.dev.
    • Sometimes, the AI might generate content that violates safety policies, leading to a blocked response. The script has BLOCK_NONE for safety settings, but this can still occur.
  • Page Freezing/Slowness: Rewriting can be resource-intensive, especially for very long chapters or slow internet connections. The script tries to manage this with rate limiting and chunking. If it consistently causes issues, consider reducing the "Chunk Size" in settings.
  • Script not appearing/working:
    • Ensure Tampermonkey is enabled in your browser extensions.
    • Verify that the script is enabled within the Tampermonkey dashboard.
    • Check if the current URL matches the @match rules in the script header.
    • Try reloading the RoyalRoad chapter page.
  • Content doesn't look rewritten: Check the similarity percentage in the status message. If it's very high (e.g., 99-100%), the AI might not have found much to change, or there was an issue with the response.

7. Disclaimer

This script relies on external AI services (Google Gemini API). Usage of the Gemini API may incur costs if you exceed free tier limits, and is subject to Google's terms of service. Always review the terms and conditions of any API you use. The quality and style of the rewritten content are dependent on the AI model's capabilities and the prompt engineering within the script. While efforts are made to preserve plot and dialogue, minor deviations or interpretations by the AI are possible.

QingJ © 2025

镜像随时可能失效,请加Q群300939539或关注我们的公众号极客氢云获取最新地址