Untitled
BJ2unknown
plain_text
5 months ago
6.4 kB
2
Indexable
(function() { 'use strict'; // Enhanced normalizeText function using regex function normalizeText(text) { return text .replace(/\[.*?\]|\d|[^\w\s-]|[*]+(\w+)[*]+/g, (match, p1) => { if (p1) return p1; // If it's a word surrounded by asterisks, return the word without the asterisks return ''; // Otherwise, remove the match (brackets, numbers, non-alphanumeric) }) .trim() // Trim leading and trailing whitespace .toLowerCase(); // Convert to lowercase } // 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 let words = [...normalizedMarkText.matchAll(/\b\w[\w-]*\b/g)] // Match words with letters, hyphens using regex .map(match => match[0]) // Extract the matched words .filter(word => word.length > 3 && !stopWords.has(word)); // Filter out short words and stop words 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) { // Get the original inner HTML of the target element let originalHTML = targetElement.innerHTML; // Normalize the text content of the target element for matching let normalizedHTML = normalizeText(originalHTML); // Function to highlight each marked word in the original HTML function highlightTest(htmlContent, segments) { segments.forEach(segment => { if (!segment || segment === "") { return; // Skip undefined or empty segments } let regex = new RegExp(`\\b(${segment})\\b(?![^<]*>)`, 'gi'); // Ensure the word is standalone with word boundaries let matches = normalizedHTML.match(regex); // Check if the word is found if (matches) { // Safely wrap the matched word in the <span> without disrupting the HTML structure htmlContent = htmlContent.replace(new RegExp(`\\b(${segment})\\b(?![^<]*>)`, 'gi'), '<span class="marked" style="background-color: pink;">$1</span>'); } }); return htmlContent; } // Apply the highlight function using the individual words let highlightedHTML = highlightTest(originalHTML, Array.from(segments)); // Apply the modified HTML back to the element if any matches were found 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; // Get the ID of the editable-57 element (e.g., 'editable-57_sentence_html1') let targetId = sourceId.replace('editable-57', 'editable-2077'); // Replace 'editable-57' with 'editable-2077' for the corresponding target ID let targetElement = document.getElementById(targetId); // Find the corresponding editable-2077 element if (targetElement) { // Extract marked words from the editable-57 element let markedSegments = extractMarkedWords(sourceElement); // Highlight the matching words in the corresponding editable-2077 element highlightMatchingWords(targetElement, markedSegments); } }); // Check all checkboxes and radio buttons let checkboxesAndRadioButtons = document.querySelectorAll( '[id^="editable-2724_sentence_html"][type="checkbox"], ' + // Checkboxes '[id^="editable-2458_sentence_html"][type="checkbox"], ' + // Checkboxes '[id^="editable-2566_sentence_html"][type="radio"], ' + // Radio buttons '[id^="editable-2747_sentence_html"][type="radio"]' // radio buttons ); checkboxesAndRadioButtons.forEach(element => { element.checked = true; element.dispatchEvent(new Event('change')); // Trigger the change event for each checkbox or radio button }); // Find all textareas with name 'comment_attribution_sentence_html1' and unhide them let textareas = document.querySelectorAll('textarea[name="comment_attribution_sentence_html1"]'); textareas.forEach(textarea => { textarea.style.display = 'block'; // Unhide each textarea }); // Periodically check and hide specified divs const divsToHide = ["editable-1", "editable-2244", "editable-2549", "editable-44"]; const hideDivs = () => { let allHidden = true; // Flag to check if all divs are hidden divsToHide.forEach(divId => { const div = document.getElementById(divId); if (div) { div.style.display = "none"; } else { allHidden = false; // If any div is not found, set flag to false } }); // If all divs are hidden, clear the interval if (allHidden) { clearInterval(hideDivsInterval); } }; // Run hideDivs every 500ms to ensure the divs are hidden as soon as they appear const hideDivsInterval = setInterval(hideDivs, 500); })();
Editor is loading...
Leave a Comment