Selection Context

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

Από την 05/03/2025. Δείτε την τελευταία έκδοση.

Αυτός ο κώδικας δεν πρέπει να εγκατασταθεί άμεσα. Είναι μια βιβλιοθήκη για άλλους κώδικες που περιλαμβάνεται μέσω της οδηγίας meta // @require https://update.greatest.deepsurf.us/scripts/528822/1547501/Selection%20Context.js

θα χρειαστεί να εγκαταστήσετε μια επέκταση όπως το Tampermonkey, το Greasemonkey ή το Violentmonkey για να εγκαταστήσετε αυτόν τον κώδικα.

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

θα χρειαστεί να εγκαταστήσετε μια επέκταση όπως το Tampermonkey ή το Violentmonkey για να εγκαταστήσετε αυτόν τον κώδικα.

θα χρειαστεί να εγκαταστήσετε μια επέκταση όπως το Tampermonkey ή το Userscripts για να εγκαταστήσετε αυτόν τον κώδικα.

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

Θα χρειαστεί να εγκαταστήσετε μια επέκταση διαχείρισης κώδικα χρήστη για να εγκαταστήσετε αυτόν τον κώδικα.

(Έχω ήδη έναν διαχειριστή κώδικα χρήστη, επιτρέψτε μου να τον εγκαταστήσω!)

You will need to install an extension such as Stylus to install this style.

You will need to install an extension such as Stylus to install this style.

You will need to install an extension such as Stylus to install this style.

You will need to install a user style manager extension to install this style.

You will need to install a user style manager extension to install this style.

You will need to install a user style manager extension to install this style.

(Έχω ήδη έναν διαχειριστή στυλ χρήστη, επιτρέψτε μου να τον εγκαταστήσω!)

// ==UserScript==
// @name         Selection Context
// @namespace    http://tampermonkey.net/
// @version      0.1.0
// @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
 * @returns {Object} Object containing selectedText, textBefore, and textAfter
 */
function GetSelectionContext() {
  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();
  
  // Get the range of the current selection
  const range = selection.getRangeAt(0);
  
  // Get the paragraph containing the selection
  let paragraphElement = range.commonAncestorContainer;

  // Navigate up to find a paragraph or meaningful content container
  while (paragraphElement &&
    (paragraphElement.nodeType !== Node.ELEMENT_NODE ||
      !['P', 'DIV', 'ARTICLE', 'SECTION', 'LI'].includes(paragraphElement.tagName))) {
    paragraphElement = paragraphElement.parentNode;
  }

  // Get the paragraph text
  let paragraphText = '';
  if (paragraphElement) {
    paragraphText = paragraphElement.textContent.trim();
    if (paragraphText.length > 500) {
      paragraphText = paragraphText.substring(0, 497) + '...';
    }
  }

  // Create ranges for text before and after selection
  let textBefore = '';
  let textAfter = '';

  try {
    // Get text before selection
    const beforeRange = range.cloneRange();
    beforeRange.setStart(paragraphElement || document.body, 0);
    beforeRange.setEnd(range.startContainer, range.startOffset);
    textBefore = beforeRange.toString().trim();
    
    // Get text after selection
    const afterRange = range.cloneRange();
    afterRange.setStart(range.endContainer, range.endOffset);
    afterRange.setEnd(paragraphElement || document.body, paragraphElement ? paragraphElement.childNodes.length : document.body.childNodes.length);
    textAfter = afterRange.toString().trim();
    
    // Limit the length of before/after text to be reasonable
    if (textBefore.length > 250) {
      textBefore = '...' + textBefore.substring(textBefore.length - 250);
    }
    
    if (textAfter.length > 250) {
      textAfter = textAfter.substring(0, 250) + '...';
    }
  } catch (e) {
    console.error('Error getting text before/after selection:', e);
  }

  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;
}