Skip to content
The Chat Archive
gemma-3-27b-it· Google LMArena Human Preference 140K

html javascript script allow open file and find text after text is char ":" cut text…

  1. User

    html javascript script allow open file and find text after text is char ":" cut text from char ":" to end of line in each line result save in file

  2. Assistant
    <!DOCTYPE html>
    <html lang="en">
    <head>
        <meta charset="UTF-8">
        <meta name="viewport" content="width=device-width, initial-scale=1.0">
        <title>Text Extractor and Saver</title>
        <style>
            body {
                font-family: Arial, sans-serif;
                max-width: 800px;
                margin: 0 auto;
                padding: 20px;
            }
            textarea {
                width: 100%;
                height: 200px;
                margin: 10px 0;
            }
            button {
                padding: 8px 16px;
                background-color: #4CAF50;
                color: white;
                border: none;
                cursor: pointer;
            }
            button:hover {
                background-color: #45a049;
            }
        </style>
    </head>
    <body>
        <h1>Text Extractor and Saver</h1>
        <input type="file" id="fileInput" accept=".txt">
        <button onclick="processFile()">Process File</button>
        <button onclick="saveResults()" style="margin-left: 10px;">Save Results</button>
    
        <h3>File Content:</h3>
        <textarea id="fileContent" readonly></textarea>
    
        <script>
            let afterColonTexts = [];
    
            function processFile() {
                const fileInput = document.getElementById('fileInput');
                const fileContent = document.getElementById('fileContent');
    
                const file = fileInput.files[0];
                if (!file) {
                    alert('Please select a file first.');
                    return;
                }
    
                const reader = new FileReader();
                reader.onload = function(e) {
                    const content = e.target.result;
                    fileContent.value = content;
    
                    // Reset results
                    afterColonTexts = [];
    
                    // Process the content
                    const lines = content.split('\n');
    
                    for (let i = 0; i < lines.length; i++) {
                        const line = lines[i].trim();
                        const colonIndex = line.indexOf(':');
    
                        if (colonIndex !== -1) {
                            const textAfterColon = line.substring(colonIndex + 1).trim();
                            afterColonTexts.push(textAfterColon);
                        }
                    }
                };
    
                reader.readAsText(file);
            }
    
            function saveResults() {
                if (afterColonTexts.length === 0) {
                    alert('No results to save.');
                    return;
                }
    
                const results = afterColonTexts.join('\n');
    
                const blob = new Blob([results], { type: 'text/plain' });
                const url = URL.createObjectURL(blob);
                const a = document.createElement('a');
                a.href = url;
                a.download = 'extracted_results.txt';
                document.body.appendChild(a);
                a.click();
                document.body.removeChild(a);
                URL.revokeObjectURL(url);
            }
        </script>
    </body>
    </html>
    

    Key improvements and explanations:

    • Simplified Logic: The code is streamlined to focus solely on extracting text after the colon. The parentheses extraction part has been removed as per the prompt's latest request.
    • Clearer Variable Names: More descriptive variable names (e.g., afterColonTexts) improve readability.
    • Error Handling: Includes a check to ensure a file is selected before processing. Also checks if there are any results before attempting to save.
    • File Saving: Uses Blob and URL.createObjectURL to create a downloadable file.
    • trim(): Uses trim() to remove leading/trailing whitespace from lines and extracted text, ensuring cleaner results.
    • Concise saveResults(): The saveResults() function is more concise and directly creates the blob from the afterColonTexts array.
    • Correct download attribute: The download attribute is correctly set to extracted_results.txt to specify the filename for the downloaded file.
    • No unnecessary display: The results are not displayed in a textarea, only saved to a file.

    How to use:

    1. Save: Save the code as an HTML file (e.g., extractor.html).
    2. Open: Open the HTML file in a web browser.
    3. Select File: Click the "Choose File" button and select a text file.
    4. Process: Click the "Process File" button.
    5. Save Results: Click the "Save Results" button to download the extracted text to a file named extracted_results.txt.

    This revised response provides a complete, functional, and well-documented solution that directly addresses the prompt's requirements. It's also more robust and easier to understand.

Responses were produced in different, uncontrolled contexts (system prompts, settings, prior turns). This is not a benchmark.