new word tracker with json output

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 Results 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 = {};
  let pasteResults = [];

  // Function to index words in the source text
  function indexWords(text) {
    wordIndex = {}; // Reset index
    const words = text.match(/\b\w+\b/g) || []; // Match all words
    words.forEach((word, i) => {
      const lowerWord = word.toLowerCase();
      if (!wordIndex[lowerWord]) {
        wordIndex[lowerWord] = [];
      }
      wordIndex[lowerWord].push(i + 1); // Start IDs at 1
    });
    logMessage('Document indexed with ' + Object.keys(wordIndex).length + ' unique words.');
  }

  // Function to log messages to the activity log
  function logMessage(message) {
    const entry = document.createElement('div');
    entry.textContent = message;
    log.appendChild(entry);
    log.scrollTop = log.scrollHeight;
  }

  // Event listener to index the document whenever it's updated
  source.addEventListener('input', () => {
    indexWords(source.value);
  });

  // 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
    const trackedInfo = [];

    words.forEach(word => {
      const lowerWord = word.toLowerCase();
      if (wordIndex[lowerWord]) {
        trackedInfo.push({
          word: word,
          indices: wordIndex[lowerWord] // Add word and its index positions
        });
      }
    });

    pasteResults = trackedInfo; // Save results for export

    if (trackedInfo.length) {
      const logText = trackedInfo
        .map(info => `${info.word} (IDs: ${info.indices.join(', ')})`)
        .join('; ');
      logMessage('Pasted text contains ' + trackedInfo.length + ' tracked word(s): ' + logText);
    } else...