Selection Context

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

2025-03-05 या दिनांकाला. सर्वात नवीन आवृत्ती पाहा.

This script should not be not be installed directly. It is a library for other scripts to include with the meta directive // @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 यासारखे एक्स्टेंशन इंस्टॉल करावे लागेल..

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

ही स्क्रिप्ट इंस्टॉल करण्यासाठी तुम्हाला Tampermonkey यासारखे एक्स्टेंशन इंस्टॉल करावे लागेल..

ही स्क्रिप्ट इंस्टॉल करण्यासाठी तुम्हाला एक युझर स्क्रिप्ट व्यवस्थापक एक्स्टेंशन इंस्टॉल करावे लागेल.

(माझ्याकडे आधीच युझर स्क्रिप्ट व्यवस्थापक आहे, मला इंस्टॉल करू द्या!)

ही स्टाईल इंस्टॉल करण्यासाठी तुम्हाला Stylus सारखे एक्स्टेंशन इंस्टॉल करावे लागेल.

ही स्टाईल इंस्टॉल करण्यासाठी तुम्हाला Stylus सारखे एक्स्टेंशन इंस्टॉल करावे लागेल.

ही स्टाईल इंस्टॉल करण्यासाठी तुम्हाला Stylus सारखे एक्स्टेंशन इंस्टॉल करावे लागेल.

ही स्टाईल इंस्टॉल करण्यासाठी तुम्हाला एक युझर स्टाईल व्यवस्थापक इंस्टॉल करावे लागेल.

ही स्टाईल इंस्टॉल करण्यासाठी तुम्हाला एक युझर स्टाईल व्यवस्थापक इंस्टॉल करावे लागेल.

ही स्टाईल इंस्टॉल करण्यासाठी तुम्हाला एक युझर स्टाईल व्यवस्थापक इंस्टॉल करावे लागेल.

(माझ्याकडे आधीच युझर स्टाईल व्यवस्थापक आहे, मला इंस्टॉल करू द्या!)

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