Greasy Fork

Greasy Fork is available in English.

快捷键粘贴当前时间文本

使用键盘快捷键【Ctrl+Shift+T】快速粘贴当前时间文本到当前输入框当前光标位置

当前为 2020-07-18 提交的版本,查看 最新版本

您需要先安装一款用户脚本管理器扩展,例如 Tampermonkey 篡改猴Greasemonkey 油猴子Violentmonkey 暴力猴,才能安装此脚本。

您需要先安装一款用户脚本管理器扩展,例如 Tampermonkey 篡改猴,才能安装此脚本。

您需要先安装一款用户脚本管理器扩展,例如 Tampermonkey 篡改猴Violentmonkey 暴力猴,才能安装此脚本。

您需要先安装一款用户脚本管理器扩展,例如 Tampermonkey 篡改猴Userscripts ,才能安装此脚本。

您需要先安装一款用户脚本管理器扩展,例如 Tampermonkey 篡改猴,才能安装此脚本。

您需要先安装一款用户脚本管理器扩展后才能安装此脚本。

(我已经安装了用户脚本管理器,让我安装!)

您需要先安装一款用户样式管理器扩展,比如 Stylus,才能安装此样式。

您需要先安装一款用户样式管理器扩展,比如 Stylus,才能安装此样式。

您需要先安装一款用户样式管理器扩展,比如 Stylus,才能安装此样式。

您需要先安装一款用户样式管理器扩展后才能安装此样式。

您需要先安装一款用户样式管理器扩展后才能安装此样式。

您需要先安装一款用户样式管理器扩展后才能安装此样式。

(我已经安装了用户样式管理器,让我安装!)

// ==UserScript==
// @name         快捷键粘贴当前时间文本
// @namespace    http://tampermonkey.net/
// @version      0.4
// @description  使用键盘快捷键【Ctrl+Shift+T】快速粘贴当前时间文本到当前输入框当前光标位置
// @author       PY-DNG
// @include      *
// @grant        none
// ==/UserScript==

document.onkeydown = function hotkey(){
  // 获取按键代码
  let keycode = window.event.keyCode
  // 检测快捷键[Ctrl+Shift+T]是否被触发
  if (keycode === 84 && event.ctrlKey && event.shiftKey){
    // 获取日期时间文本
    let d = new Date();
    let timetext = d.getFullYear().toString() + "年" + d.getMonth().toString() + "月" + d.getDate().toString() + "日 " + d.getHours().toString() + ":" + d.getMinutes().toString() + ":" + d.getSeconds().toString();
    let CT = document.activeElement;
    let CPS = getTextselection(false);
    let CPE = getTextselection(true);
    CT.value = CT.value.substring(0, CPS) + timetext + CT.value.substring(CPE, CT.value.length);
    CPE = CPS + timetext.length;
    CT.setSelectionRange(CPE, CPE);
    CT.focus();
  }
}

function getTextselection(End) {
    var oText = document.activeElement;
    var cursurPosition = -1;
    if(End){ // 获取选定区域结尾位置
        cursurPosition = oText.selectionEnd;
    }else{   // 获取选定区域起始位置
        if (oText.selectionStart) { //正常
            cursurPosition = oText.selectionStart;
        } else { //在最左边
            cursurPosition = 0;
        }
    }
    if(cursurPosition == undefined){cursurPosition = 0};
    return cursurPosition;
}