Selection Context

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

目前为 2025-03-05 提交的版本。查看 最新版本

此脚本不应直接安装,它是一个供其他脚本使用的外部库。如果您需要使用该库,请在脚本元属性加入:// @require https://update.gf.qytechs.cn/scripts/528822/1547501/Selection%20Context.js

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

QingJ © 2025

镜像随时可能失效,请加Q群300939539或关注我们的公众号极客氢云获取最新地址