选中文本并标红

选中文本并标红,英语学习

目前為 2023-09-11 提交的版本,檢視 最新版本

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

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

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

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

你需要先安裝一款使用者腳本管理器擴展,比如 Tampermonkey,才能安裝此腳本

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

(我已經安裝了使用者腳本管理器,讓我安裝!)

你需要先安裝一款使用者樣式管理器擴展,比如 Stylus,才能安裝此樣式

你需要先安裝一款使用者樣式管理器擴展,比如 Stylus,才能安裝此樣式

你需要先安裝一款使用者樣式管理器擴展,比如 Stylus,才能安裝此樣式

你需要先安裝一款使用者樣式管理器擴展後才能安裝此樣式

你需要先安裝一款使用者樣式管理器擴展後才能安裝此樣式

你需要先安裝一款使用者樣式管理器擴展後才能安裝此樣式

(我已經安裝了使用者樣式管理器,讓我安裝!)

// ==UserScript==
// @name         选中文本并标红
// @namespace    http://tampermonkey.net/
// @version      1.4
// @description  选中文本并标红,英语学习 
// @author       TCH
// @match        *.economist.com/*
// @require      https://cdn.bootcss.com/jquery/3.5.1/jquery.min.js
// @grant        本脚本仅供学习只用,如若他用,后果自行承担。
// @namespace 
// ==/UserScript==
 
(function()
{
  document.addEventListener("selectionchange", () => {
console.log(document.getSelection().toString());
  selectedText = document.getSelection().toString();
  const range = document.getSelection().getRangeAt(0);
  const docObj = range.extractContents(); //移动了Range 中的内容从文档树到DocumentFragment(文档片段对象)。
  let dom = document.createElement('span');
  dom.style.color = 'red';
  dom.appendChild(docObj);
  range.insertNode(dom);

  });

})();