Selection Context

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

目前為 2025-03-05 提交的版本,檢視 最新版本

此腳本不應該直接安裝,它是一個供其他腳本使用的函式庫。欲使用本函式庫,請在腳本 metadata 寫上: // @require https://update.greatest.deepsurf.us/scripts/528822/1547501/Selection%20Context.js

您需要先安裝使用者腳本管理器擴展,如 TampermonkeyGreasemonkeyViolentmonkey 之後才能安裝該腳本。

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

您需要先安裝使用者腳本管理器擴充功能,如 TampermonkeyViolentmonkey 後才能安裝該腳本。

您需要先安裝使用者腳本管理器擴充功能,如 TampermonkeyUserscripts 後才能安裝該腳本。

你需要先安裝一款使用者腳本管理器擴展,比如 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;
}