当前位置: 首页 > 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.jsqmd.com/news/910626/

相关文章:

  • 基于NodeMCU与ThingSpeak的智能温室监控系统:从传感器到云端
  • 基于Arduino与水流传感器的电子吹奏乐器制作全解析
  • 离散曲率:诊断与优化图神经网络过平滑与过挤压的几何方法
  • 2026年香港大学、香港中文大学、香港科技大学本科怎么申请?专业香港申请中介机构推荐 - 品牌2025
  • 长期观察使用Taotoken聚合路由对服务可用性的提升感受
  • 2026 中国气膜建筑实力厂家权威榜单:上海绿荫膜空间结构有限公司稳居榜首,行业高质量发展 - 商业新知
  • 告别“模型孤岛”:DMXAPI 如何用“统一接口”破解企业多模型管理困局
  • 情感计算:从表情识别到多模态融合,AI如何读懂人类情绪
  • 攻克精密制造痛点:2026年小间隙工件柔性夹爪方案提供商推荐 - 品牌2025
  • 基于LM2596与IRF740构建20A大电流可调开关电源全攻略
  • 告别卡顿!Ubuntu 20.04远程桌面终极方案:Xrdp配置避坑与VNC/NoMachine横向评测
  • 性价比高的网络推广代运营厂家排名
  • 课堂随笔13
  • 2026会计人员如何全面提升业务能力:别只盯着账本,数据思维才是你的核心竞争力
  • 航班机组排班列生成求解代码包:Python+Gurobi实现主问题与子问题迭代
  • 2026新疆目的地婚礼权威测评发布 三大直营品牌引领西域婚旅新风尚 - 江湖评测
  • 微信投票系统那个好?书法绘画系列比赛投票活动策划方案及实操细节 - 投票评选活动
  • 用Cheat Engine和OD定位PC微信3.9.2.23收消息函数:一个逆向新手的实战笔记
  • 量子纠缠分布能耗研究:理论框架与优化路径
  • 从显卡驱动到cuDNN:Win10深度学习环境搭建的完整工具链梳理
  • 2026年国产柔性夹爪品牌推荐:助力药企实现高效无损搬运 - 品牌2025
  • 从机器学习到网络安全:算法工程师的转型之路与技能迁移实战
  • Lumerical FDTD自动化脚本入门:从零编写你的第一个Python控制脚本(基于v231 API)
  • 别再为Modelsim 10.4安装报错发愁了!手把手教你从下载到破解的保姆级避坑指南
  • 2026Q2漯河衣柜定制厂家TOP8权威推荐:橱柜、门墙柜高端定制商家甄选 - 品牌智鉴榜
  • Ubuntu登录界面黑屏?手把手教你排查和修复lightdm启动失败(附debug命令详解)
  • Arduino驱动7段数码管:从硬件原理到代码实现的嵌入式入门实践
  • AMD Ryzen终极调试指南:5分钟掌握免费开源工具SMUDebugTool
  • 从5G到微波:当EVM遇到1024/4096QAM,你的测试仪器还扛得住吗?
  • Lindy理赔自动化实施全周期拆解(从需求冻结到SLA提升47%的真相)