Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- //GreaseMonkey: Count selection's characters and lines
- //2019-01-03, https://greasyfork.org/en/users/85671-jcunews
- (function(displayTime, disp, timer) {
- function dispClose(ev) {
- if (ev) ev.preventDefault();
- if (!disp) return;
- clearTimeout(timer);
- disp.remove();
- disp = null;
- }
- displayTime = 2000; //in milliseconds. 1000ms = 1 second
- addEventListener("keydown", function(ev, txt, msg) {
- //shortcut: CTRL+; without SHIFT or ALT
- if (!(ev.ctrlKey && !ev.shiftKey && !ev.altKey && (ev.key.toLowerCase() === ";"))) {
- //when the message is displayed, keys except CTRL, SHIFT, or ALT, or the CTRL+L shortcut, will close the message
- if (disp && !["control", "shift", "alt"].includes(ev.key.toLowerCase())) dispClose(ev);
- return;
- }
- ev.preventDefault();
- if (disp) return;
- txt = getSelection();
- if ((txt.anchorNode === txt.focusNode) && (txt.anchorOffset === txt.focusOffset)) {
- if (!(txt = document.activeElement) || !("value" in txt)) return;
- txt = txt.value.substring(txt.selectionStart, txt.selectionEnd);
- } else txt = txt.toString();
- if (txt) {
- msg = txt.length + " characters.";
- } else {
- msg = "No selection.";
- }
- if (disp) {
- clearTimeout(timer);
- disp.remove();
- }
- disp = document.createElement("DIV");
- disp.innerHTML = '<div style="margin:13% auto;border:2px solid blue;width:15ex;text-align:center;padding:1ex 1.5ex;background-color:white;color:black;white-space:pre;font-size:16pt;line-height:normal">' + msg + '</div>';
- disp.style.cssText = "position:fixed;z-index:999999;left:0;top:0;right:0;bottom:0;background-color:rgb(1,1,1,0.3);cursor:pointer";
- disp.onclick = dispClose;
- document.body.appendChild(disp);
- disp.contentEditable = false;
- setTimeout(dispClose, displayTime);
- });
- })();
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement