当前位置: 首页 > news >正文

油猴脚本 chrome 浏览器 插件 显示鼠标选中的文字总数

前提:

  安装有chrome

 

image

 

// ==UserScript==
// @name         选中文本字数统计
// @namespace    http://tampermonkey.net/
// @version      1.5
// @description  鼠标选择一段文本,在右下角显示统计的字数
// @author       You
// @match        *://*/*
// @grant        none
// @run-at       document-end
// ==/UserScript==(function() {'use strict';// 等待 DOM 完全加载function init() {// 检查 body 是否存在if (!document.body) {return;}// 检查是否已经存在if (document.getElementById('word-count-box')) {return;}// 创建样式const style = document.createElement('style');style.id = 'word-count-style';style.textContent = `#word-count-box {position: fixed;bottom: 20px;right: 20px;background-color: rgba(0, 0, 0, 0.85);color: #fff;padding: 12px 16px;border-radius: 6px;font-size: 14px;font-family: Arial, sans-serif;z-index: 2147483647;display: none;box-shadow: 0 4px 12px rgba(0, 0, 0, 0.4);min-width: 150px;line-height: 1.6;pointer-events: none;user-select: none;}`;document.head.appendChild(style);// 创建显示框const wordCountBox = document.createElement('div');wordCountBox.id = 'word-count-box';wordCountBox.setAttribute('role', 'status');document.body.appendChild(wordCountBox);// 统计字数的函数function countWords(text) {if (!text || text.trim() === '') {return { total: 0, chinese: 0, english: 0, numbers: 0, other: 0 };}text = text.trim();const chineseChars = (text.match(/[\u4e00-\u9fa5]/g) || []).length;const englishChars = (text.match(/[a-zA-Z]/g) || []).length;const numberChars = (text.match(/\d/g) || []).length;const otherChars = (text.match(/[^\u4e00-\u9fa5a-zA-Z0-9\s]/g) || []).length;const total = chineseChars + englishChars + numberChars + otherChars;return {total: total,chinese: chineseChars,english: englishChars,numbers: numberChars,other: otherChars};}// 显示字数统计function showWordCount() {const selection = window.getSelection();const selectedText = selection.toString().trim();if (selectedText.length > 0) {const count = countWords(selectedText);wordCountBox.innerHTML = `<div style="font-weight:bold;margin-bottom:6px;border-bottom:1px solid #555;padding-bottom:4px;">📊 字数统计</div><div>📝 总字符:${count.total}</div><div>🇨🇳 中文:${count.chinese}</div><div>🔤 英文:${count.english}</div><div>🔢 数字:${count.numbers}</div><div>📌 其他:${count.other}</div>`;wordCountBox.style.display = 'block';// 调整位置try {const selectionRange = selection.getRangeAt(0);const rect = selectionRange.getBoundingClientRect();if (rect.right > window.innerWidth - 200) {wordCountBox.style.right = (window.innerWidth - rect.left + 10) + 'px';} else {wordCountBox.style.right = '20px';}if (rect.bottom > window.innerHeight - 150) {wordCountBox.style.bottom = (window.innerHeight - rect.top + 10) + 'px';} else {wordCountBox.style.bottom = '20px';}} catch (e) {wordCountBox.style.right = '20px';wordCountBox.style.bottom = '20px';}} else {hideWordCount();}}// 隐藏字数统计function hideWordCount() {wordCountBox.style.display = 'none';}// 监听鼠标松开事件document.addEventListener('mouseup', function() {setTimeout(function() {const selection = window.getSelection();const selectedText = selection.toString().trim();if (selectedText.length > 0) {showWordCount();} else {hideWordCount();}}, 100);});// 键盘选择文本时更新统计document.addEventListener('keyup', function(e) {if (e.shiftKey || e.key === 'ArrowLeft' || e.key === 'ArrowRight' || e.key === 'ArrowUp' || e.key === 'ArrowDown') {setTimeout(function() {const selection = window.getSelection();const selectedText = selection.toString().trim();if (selectedText.length > 0) {showWordCount();} else {hideWordCount();}}, 100);}});// 滚动和窗口大小改变时隐藏统计框document.addEventListener('scroll', function() {hideWordCount();});window.addEventListener('resize', function() {hideWordCount();});}// 根据当前状态决定如何执行if (document.readyState === 'complete' || document.readyState === 'interactive') {init();} else {document.addEventListener('DOMContentLoaded', init);}// 备用方案:如果 3 秒后还没初始化,强制执行setTimeout(function() {if (!document.getElementById('word-count-box')) {init();}}, 3000);
})();

  

http://www.rkmt.cn/news/1421933.html

相关文章:

  • 长期观察使用Taotoken聚合路由对服务可用性的提升感受
  • 基于Arduino与水流传感器的电子吹奏乐器制作全解析
  • 2026年香港大学、香港中文大学、香港科技大学本科怎么申请?专业香港申请中介机构推荐 - 品牌2025
  • 课堂随笔13
  • 2026新疆目的地婚礼权威测评发布 三大直营品牌引领西域婚旅新风尚 - 江湖评测
  • 性价比高的网络推广代运营厂家排名
  • 2026年国产柔性夹爪品牌推荐:助力药企实现高效无损搬运 - 品牌2025
  • 从机器学习到网络安全:算法工程师的转型之路与技能迁移实战
  • Lumerical FDTD自动化脚本入门:从零编写你的第一个Python控制脚本(基于v231 API)
  • 从5G到微波:当EVM遇到1024/4096QAM,你的测试仪器还扛得住吗?
  • Lindy理赔自动化实施全周期拆解(从需求冻结到SLA提升47%的真相)
  • 当车主还在因为补漆犹豫“是否靠谱的时候”,北京的这家店已经把标准藏在看不见的地方 - 新闻快传
  • 零编程基础入门:KH Coder 13种语言文本挖掘完整指南
  • 082A-基于51单片机智能晾衣架【Proteus仿真+Keil程序+报告+原理图】
  • AI客服系统进入业务执行阶段,售后服务开始重视“处理能力”
  • 机器学习调参时,Jensen不等式能帮你省多少计算量?(附Python代码验证)
  • 保姆级避坑指南:在CentOS 8.5上用JDK 17搞定Hadoop 3.3.5 + Spark 3.3.2集群(附虚拟机克隆技巧)
  • 三步解锁手机音频无线传输:sndcpy让电脑成为你的手机音响
  • Go语言WASM:WebAssembly支持
  • 2026年6月亲历深度评测现场记录|百达翡丽官方售后网点2026年实地验证报告(含迁址与新开) - 百达翡丽服务中心
  • 绵阳游仙区一环路东段149号附近,宠物生病去哪看?本地人常去的3家口碑医院 - 品牌日记
  • 2026年国内五大辣椒油品牌推荐!2026最新排名出炉,椒上飞实力领先 - 十大品牌榜
  • 告别Cloud Sync?试试用Rclone在群晖上挂载阿里云盘,实现更灵活的同步与备份
  • 智造未来:四大品牌如何赋能制造业数字化转型?
  • 如何快速掌握Raw Accel鼠标加速:面向游戏玩家的7种曲线终极指南
  • pom-xml-flattened 这是什么文件?可以删除吗?
  • AI统一分析:打破数据孤岛,从暗数据到智能决策的实战指南
  • 深度解析:AI智能体的“记忆”(Memory)与“知识库”(RAG)如何协同进化?
  • 别再手动敲字了!用Python的EasyOCR库,5分钟搞定图片文字批量提取(附中文识别实战代码)
  • 谷歌投资回报周期解析:从业务拆解到实战策略