Untitled

acronym
 avatar
unknown
plain_text
6 months ago
5.0 kB
4
Indexable
(function() {
    'use strict';

    // Enhanced normalizeText function to handle apostrophes, brackets, and punctuation
    function normalizeText(text) {
        const normalized = text
            .replace(/[*]+([\wÀ-ž'-]+)[*]+/g, '$1')        // Remove asterisks around words
            .replace(/[^\wÀ-ž\s'-]+/g, ' ')                // Replace any non-word chars (except apostrophes, hyphens, spaces) with a space
            .replace(/\s+/g, ' ')                          // Ensure multiple spaces are reduced to a single space
            .trim();                                       // Trim leading and trailing spaces
        return normalized;
    }

    // List of stop words that we want to exclude from matching
    const stopWords = new Set(['the', 'which', 'will', 'this', 'an', 'as', 'by', 'or', 'and', 'a', 'in', 'on', 'at', 'of', 'for', 'to', 'is', 'are', 'was', 'were', 'it', 'but']);

    // Function to extract marked words from an editable-57 element
    function extractMarkedWords(sourceElement) {
        let markedSegments = new Set(); // Use a Set to ensure uniqueness
        let markElements = sourceElement.querySelectorAll('mark'); // Get the marked text inside <mark> tags

        markElements.forEach(mark => {
            let normalizedMarkText = normalizeText(mark.textContent); // Normalize the text by removing punctuation and symbols

            let words = [...normalizedMarkText.matchAll(/\b[\wÀ-ž'-]+\b/g)] // Match words with letters, apostrophes, accents, hyphens
                .map(match => match[0]) // Extract the matched words
                // Include words longer than 3 letters OR uppercase 3-letter words
                .filter(word => (word.length > 3 || /^[A-Z]{3}$/.test(word)) && !stopWords.has(word)); 

            words.forEach(word => {
                markedSegments.add(word); // Add each word to the Set
            });
        });

        return markedSegments;
    }

    // Function to highlight words in a corresponding editable-2077 element
    function highlightMatchingWords(targetElement, segments) {
        let originalHTML = targetElement.innerHTML;
        let normalizedHTML = normalizeText(originalHTML);

        function highlightTest(htmlContent, segments) {
            segments.forEach(segment => {
                if (!segment || segment === "") return;

                let regex = new RegExp(`\\b(${segment.replace(/[-]/g, '\\$&')})\\b(?![^<]*>)`, 'gi'); // Escape hyphen
                let matches = normalizedHTML.match(regex);

                if (matches) {
                    htmlContent = htmlContent.replace(new RegExp(`\\b(${segment.replace(/[-]/g, '\\$&')})\\b(?![^<]*>)`, 'gi'), '<span class="marked" style="background-color: pink;">$1</span>');
                }
            });
            return htmlContent;
        }

        let highlightedHTML = highlightTest(originalHTML, Array.from(segments));

        if (highlightedHTML !== originalHTML) {
            targetElement.innerHTML = highlightedHTML;
        }
    }

    // Get all 'editable-57' elements and their corresponding 'editable-2077' elements
    let allTargets = Array.from(document.querySelectorAll('[id^="editable-57_sentence_html"]'));

    // Iterate over each 'editable-57' element, extract marked words, and highlight in the corresponding 'editable-2077'
    allTargets.forEach(sourceElement => {
        let sourceId = sourceElement.id; 
        let targetId = sourceId.replace('editable-57', 'editable-2077');

        let targetElement = document.getElementById(targetId);
        if (targetElement) {
            let markedSegments = extractMarkedWords(sourceElement);
            highlightMatchingWords(targetElement, markedSegments);
        }
    });

    // Check all checkboxes and radio buttons
    let checkboxesAndRadioButtons = document.querySelectorAll(
        '[id^="editable-2724_sentence_html"][type="checkbox"], ' +  
        '[id^="editable-2458_sentence_html"][type="checkbox"], ' +  
        '[id^="editable-2566_sentence_html"][type="radio"], ' +     
        '[id^="editable-2747_sentence_html"][type="radio"]'
    );
    
    checkboxesAndRadioButtons.forEach(element => {
        element.checked = true;
        element.dispatchEvent(new Event('change')); 
    });

    // Unhide textareas
    let textareas = document.querySelectorAll('textarea[name="comment_attribution_sentence_html1"]');
    
    textareas.forEach(textarea => {
        textarea.style.display = 'block'; 
    });

    // Hide specified divs immediately when the script runs
    const divsToHide = ["editable-1", "editable-2244", "editable-2549", "editable-44"];

    divsToHide.forEach(divId => {
        const div = document.getElementById(divId);
        if (div) {
            div.setAttribute("style", "display: none !important"); 
        }
    });

})();
Editor is loading...
Leave a Comment