Selection Context

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

2025/03/05のページです。最新版はこちら。

このスクリプトは単体で利用できません。右のようなメタデータを含むスクリプトから、ライブラリとして読み込まれます: // @require https://update.greatest.deepsurf.us/scripts/528822/1547501/Selection%20Context.js

このスクリプトの質問や評価の投稿はこちら通報はこちらへお寄せください。
  1. // ==UserScript==
  2. // @name Selection Context
  3. // @namespace http://tampermonkey.net/
  4. // @version 0.1.0
  5. // @description Get the selected text along with text before and after the selection
  6. // @author RoCry
  7. // @license MIT
  8. // ==/UserScript==
  9.  
  10. /**
  11. * Gets the selected text along with text before and after the selection
  12. * @returns {Object} Object containing selectedText, textBefore, and textAfter
  13. */
  14. function GetSelectionContext() {
  15. const selection = window.getSelection();
  16. if (!selection || selection.rangeCount === 0 || selection.toString().trim() === '') {
  17. return { selectedText: null, textBefore: null, textAfter: null, paragraphText: null };
  18. }
  19.  
  20. const selectedText = selection.toString().trim();
  21. // Get the range of the current selection
  22. const range = selection.getRangeAt(0);
  23. // Get the paragraph containing the selection
  24. let paragraphElement = range.commonAncestorContainer;
  25.  
  26. // Navigate up to find a paragraph or meaningful content container
  27. while (paragraphElement &&
  28. (paragraphElement.nodeType !== Node.ELEMENT_NODE ||
  29. !['P', 'DIV', 'ARTICLE', 'SECTION', 'LI'].includes(paragraphElement.tagName))) {
  30. paragraphElement = paragraphElement.parentNode;
  31. }
  32.  
  33. // Get the paragraph text
  34. let paragraphText = '';
  35. if (paragraphElement) {
  36. paragraphText = paragraphElement.textContent.trim();
  37. if (paragraphText.length > 500) {
  38. paragraphText = paragraphText.substring(0, 497) + '...';
  39. }
  40. }
  41.  
  42. // Create ranges for text before and after selection
  43. let textBefore = '';
  44. let textAfter = '';
  45.  
  46. try {
  47. // Get text before selection
  48. const beforeRange = range.cloneRange();
  49. beforeRange.setStart(paragraphElement || document.body, 0);
  50. beforeRange.setEnd(range.startContainer, range.startOffset);
  51. textBefore = beforeRange.toString().trim();
  52. // Get text after selection
  53. const afterRange = range.cloneRange();
  54. afterRange.setStart(range.endContainer, range.endOffset);
  55. afterRange.setEnd(paragraphElement || document.body, paragraphElement ? paragraphElement.childNodes.length : document.body.childNodes.length);
  56. textAfter = afterRange.toString().trim();
  57. // Limit the length of before/after text to be reasonable
  58. if (textBefore.length > 250) {
  59. textBefore = '...' + textBefore.substring(textBefore.length - 250);
  60. }
  61. if (textAfter.length > 250) {
  62. textAfter = textAfter.substring(0, 250) + '...';
  63. }
  64. } catch (e) {
  65. console.error('Error getting text before/after selection:', e);
  66. }
  67.  
  68. return { selectedText, textBefore, textAfter, paragraphText };
  69. }
  70.  
  71. // Export the function for use in other scripts
  72. if (typeof module !== 'undefined' && module.exports) {
  73. module.exports = { GetSelectionContext };
  74. } else {
  75. // For direct browser use
  76. window.SelectionUtils = window.SelectionUtils || {};
  77. window.SelectionUtils.GetSelectionContext = GetSelectionContext;
  78. }