Selection Context

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

05.03.2025 itibariyledir. En son verisyonu görün.

Bu script direkt olarak kurulamaz. Başka scriptler için bir kütüphanedir ve meta yönergeleri içerir // @require https://update.greatest.deepsurf.us/scripts/528822/1547501/Selection%20Context.js

Bu betiği kurabilmeniz için Tampermonkey, Greasemonkey ya da Violentmonkey gibi bir kullanıcı betiği eklentisini kurmanız gerekmektedir.

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

Bu betiği kurabilmeniz için Tampermonkey ya da Violentmonkey gibi bir kullanıcı betiği eklentisini kurmanız gerekmektedir.

Bu betiği kurabilmeniz için Tampermonkey ya da Userscripts gibi bir kullanıcı betiği eklentisini kurmanız gerekmektedir.

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

Bu komut dosyasını yüklemek için bir kullanıcı komut dosyası yöneticisi uzantısı yüklemeniz gerekecek.

(Zaten bir kullanıcı komut dosyası yöneticim var, kurmama izin verin!)

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.

(Zateb bir user-style yöneticim var, yükleyeyim!)

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