31st version 2 Dec mynew word tracker

by Julian

HTML

<!DOCTYPE html>
<html lang="en">
<head>
  <meta charset="UTF-8">
  <meta name="viewport" content="width=device-width, initial-scale=1.0">
  <title>Word Index Tracker</title>
  <style>
    #source, #target {
      width: 45%;
      height: 200px;
      margin: 10px;
      border: 1px solid #ccc;
      padding: 10px;
      font-family: Arial, sans-serif;
      font-size: 14px;
    }
    #log {
      margin: 10px;
      padding: 10px;
      border: 1px solid #ccc;
      font-family: Arial, sans-serif;
      font-size: 14px;
      height: 150px;
      overflow-y: auto;
      background-color: #f9f9f9;
    }
    #saveButton {
      margin: 10px;
      padding: 10px 20px;
      background-color: #4CAF50;
      color: white;
      border: none;
      border-radius: 4px;
      font-size: 16px;
      cursor: pointer;
    }
    #saveButton:hover {
      background-color: #45a049;
    }
  </style>
</head>
<body>

<h2>Word Index Tracker</h2>
<div>
  <textarea id="source" placeholder="Paste or type your document here..."></textarea>
  <textarea id="target" placeholder="Paste text here..."></textarea>
</div>
<div>
  <h3>Activity Log:</h3>
  <div id="log"></div>
</div>
<div>
  <button id="saveButton">Save Pasted Words to JSON</button>
</div>


</body>
</html>

JavaScript

const source = document.getElementById('source');
  const target = document.getElementById('target');
  const log = document.getElementById('log');
  const saveButton = document.getElementById('saveButton');

  let wordIndex = []; // To store indexed words and their unique IDs
  let pastedWords = []; // To accumulate pasted words and their matched indexes

  // Function to index every word uniquely by its occurrence in the text
  function indexWords(text) {
    wordIndex = []; // Reset index
    const words = text.match(/\b\w+\b/g) || []; // Match all words, ignoring punctuation
    let currentPosition = 0;

    words.forEach((word, i) => {
      const cleanWord = word.toLowerCase(); // Normalize to lowercase

      // Find the next occurrence of the word in the text
      const position = text.indexOf(word, currentPosition);

      // Add the word's details to the index
      wordIndex.push({
        word: cleanWord,
        index: i + 1, // Unique sequential index
        position: position // Accurate position in the text
      });

      currentPosition = position + word.length; // Move past the current word
    });

    logMessage(`Document indexed with ${wordIndex.length} word occurrences.`);
  }

  // Event listener to track copy-paste activity
  target.addEventListener('paste', (event) => {
    const pastedText = event.clipboardData.getData('text'); // Get pasted text
    const words = pastedText.match(/\b\w+\b/g) || []; // Extract words, ignoring punctuation
    const trackedInfo = [];
    const seenOccurrences = {}; // Track used occurrences for each word

    words.forEach((word) => {
      const cleanWord = word.toLowerCase(); // Normalize pasted word

      // Track occurrences for each word
      if (!seenOccurrences[cleanWord]) {
        seenOccurrences[cleanWord] = 0; // Initialize if not set
      }

      // Find the specific occurrence of the word in the source text
      const matchingEntry = wordIndex.find(
        (entry) =>
         ...