Selection Context

Get the selected text along with text before and after the selection

Versión del día 5/3/2025. Echa un vistazo a la versión más reciente.

Este script no debería instalarse directamente. Es una biblioteca que utilizan otros scripts mediante la meta-directiva de inclusión // @require https://update.greasyfork.org/scripts/528822/1547803/Selection%20Context.js

Tendrás que instalar una extensión para tu navegador como Tampermonkey, Greasemonkey o Violentmonkey si quieres utilizar este script.

You will need to install an extension such as Tampermonkey to install this script.

Tendrás que instalar una extensión como Tampermonkey o Violentmonkey para instalar este script.

Necesitarás instalar una extensión como Tampermonkey o Userscripts para instalar este script.

Tendrás que instalar una extensión como Tampermonkey antes de poder instalar este script.

Necesitarás instalar una extensión para administrar scripts de usuario si quieres instalar este script.

(Ya tengo un administrador de scripts de usuario, déjame instalarlo)

Tendrás que instalar una extensión como Stylus antes de poder instalar este script.

Tendrás que instalar una extensión como Stylus antes de poder instalar este script.

Tendrás que instalar una extensión como Stylus antes de poder instalar este script.

Para poder instalar esto tendrás que instalar primero una extensión de estilos de usuario.

Para poder instalar esto tendrás que instalar primero una extensión de estilos de usuario.

Para poder instalar esto tendrás que instalar primero una extensión de estilos de usuario.

(Ya tengo un administrador de estilos de usuario, déjame instalarlo)

// ==UserScript==
// @name         Selection Context
// @namespace    http://tampermonkey.net/
// @version      0.1.1
// @description  Get the selected text along with text before and after the selection
// @author       RoCry
// @license MIT
// ==/UserScript==

/**
 * Gets the selected text along with text before and after the selection
 * @param {number} tryContextLength - Desired length of context to try to collect (before + after selection)
 * @returns {Object} Object containing selectedText, textBefore, textAfter, and paragraphText
 */
function GetSelectionContext(tryContextLength = 500) {
  const MAX_CONTEXT_LENGTH = 8192; // 8K characters max (reduced from 16K)
  const actualContextLength = Math.min(tryContextLength, MAX_CONTEXT_LENGTH);
  const halfContextLength = Math.floor(actualContextLength / 2);

  const selection = window.getSelection();
  if (!selection || selection.rangeCount === 0 || selection.toString().trim() === '') {
    return { selectedText: null, textBefore: null, textAfter: null, paragraphText: null };
  }

  const selectedText = selection.toString().trim();
  const range = selection.getRangeAt(0);

  // Helper function to get text nodes in document order
  function getTextNodesIn(node) {
    const textNodes = [];
    const walk = document.createTreeWalker(node, NodeFilter.SHOW_TEXT, null, false);
    let currentNode;
    while (currentNode = walk.nextNode()) {
      textNodes.push(currentNode);
    }
    return textNodes;
  }

  // Get all text nodes in document
  const allTextNodes = getTextNodesIn(document.body);

  // Find start and end text nodes of the selection
  const startNode = range.startContainer;
  const endNode = range.endContainer;

  let textBefore = '';
  let textAfter = '';

  // Collect text before the selection
  let beforeIndex = allTextNodes.findIndex(node => node === startNode) - 1;
  let currentLength = 0;

  // First add partial text from the start node itself
  if (startNode.nodeType === Node.TEXT_NODE) {
    textBefore = startNode.textContent.substring(0, range.startOffset) + textBefore;
    currentLength = textBefore.length;
  }

  // Then add text from previous nodes
  while (beforeIndex >= 0 && currentLength < halfContextLength) {
    const node = allTextNodes[beforeIndex];
    const nodeText = node.textContent;

    // Add the entire node's text
    textBefore = nodeText + '\n' + textBefore;
    currentLength += nodeText.length;

    beforeIndex--;
  }

  // If we didn't get enough context, add ellipsis
  if (beforeIndex >= 0) {
    textBefore = '...\n' + textBefore;
  }

  // Collect text after the selection
  let afterIndex = allTextNodes.findIndex(node => node === endNode) + 1;
  currentLength = 0;

  // First add partial text from the end node itself
  if (endNode.nodeType === Node.TEXT_NODE) {
    textAfter += endNode.textContent.substring(range.endOffset);
    currentLength = textAfter.length;
  }

  // Then add text from subsequent nodes
  while (afterIndex < allTextNodes.length && currentLength < halfContextLength) {
    const node = allTextNodes[afterIndex];
    const nodeText = node.textContent;

    // Add the entire node's text
    textAfter += nodeText + '\n';
    currentLength += nodeText.length;

    afterIndex++;
  }

  // If we didn't get all the text, add ellipsis
  if (afterIndex < allTextNodes.length) {
    textAfter += '\n...';
  }

  // Clean up and trim the text
  textBefore = textBefore.trim();
  textAfter = textAfter.trim();

  // Combine everything for paragraph text
  const paragraphText = (textBefore + ' ' + selectedText + ' ' + textAfter).trim();

  return { selectedText, textBefore, textAfter, paragraphText };
}

// Export the function for use in other scripts
if (typeof module !== 'undefined' && module.exports) {
  module.exports = { GetSelectionContext };
} else {
  // For direct browser use
  window.SelectionUtils = window.SelectionUtils || {};
  window.SelectionUtils.GetSelectionContext = GetSelectionContext;
}