您需要先安装一个扩展,例如 篡改猴、Greasemonkey 或 暴力猴,之后才能安装此脚本。
您需要先安装一个扩展,例如 篡改猴 或 暴力猴,之后才能安装此脚本。
您需要先安装一个扩展,例如 篡改猴 或 暴力猴,之后才能安装此脚本。
您需要先安装一个扩展,例如 篡改猴 或 Userscripts ,之后才能安装此脚本。
您需要先安装一款用户脚本管理器扩展,例如 Tampermonkey,才能安装此脚本。
您需要先安装用户脚本管理器扩展后才能安装此脚本。
按F2扩展当前选中的文字范围
- // ==UserScript==
- // @name Extend Selection
- // @namespace http://tampermonkey.net/
- // @version 0.1
- // @description 按F2扩展当前选中的文字范围
- // @match *://*/*
- // @grant none
- // @license MIT
- // ==/UserScript==
- (function() {
- 'use strict';
- // 处理键盘按下事件
- document.addEventListener('keydown', function(e) {
- if (e.key === "F2") {
- const selection = window.getSelection();
- if (selection.rangeCount > 0) {
- const range = selection.getRangeAt(0);
- const startContainer = range.startContainer;
- const endContainer = range.endContainer;
- // 向上扩展选区
- if (startContainer.parentNode) {
- range.setStartBefore(startContainer.parentNode);
- }
- // 向下扩展选区
- if (endContainer.parentNode) {
- range.setEndAfter(endContainer.parentNode);
- }
- // 更新选区
- selection.removeAllRanges();
- selection.addRange(range);
- }
- }
- });
- })();