CSDN Copyman

Press ctrl and click to copy without login

  1. // ==UserScript==
  2. // @name CSDN Copyman
  3. // @namespace http://tampermonkey.net/
  4. // @version 0.1.6
  5. // @description Press ctrl and click to copy without login
  6. // @author blvlight
  7. // @match https://*.csdn.net/*
  8. // @icon data:image/gif;base64,R0lGODlhAQABAAAAACH5BAEKAAEALAAAAAABAAEAAAICTAEAOw==
  9. // @grant none
  10. // @license MIT
  11. // ==/UserScript==
  12.  
  13. (function () {
  14. 'use strict';
  15.  
  16. function useDomHighlight(root) {
  17. const highlightBlock = document.createElement('div')
  18. const s = highlightBlock.style;
  19. s.position = 'fixed';
  20. s.pointerEvents = 'none';
  21. s.background = 'rgba(0,0,200,.3)';
  22. document.body.appendChild(highlightBlock);
  23. function handleMouseOver(e) {
  24. e.stopPropagation();
  25. if (e.ctrlKey) {
  26. const cur = e.target;
  27. s.width = cur.offsetWidth + 'px';
  28. s.height = cur.offsetHeight + 'px';
  29. s.top = cur.offsetTop + 'px';
  30. s.left = cur.offsetLeft + 'px';
  31. }
  32. else {
  33. s.width = 0;
  34. s.height = 0;
  35. }
  36. }
  37. function addEventListenerAll(root) {
  38. if (!root) return;
  39. root.addEventListener('mouseover', handleMouseOver);
  40. for (let el of [...root.querySelectorAll('*')]) {
  41. addEventListenerAll(el);
  42. }
  43. }
  44. addEventListenerAll(root);
  45. function removeEventListenerAll(root) {
  46. if (!root) return;
  47. root.removeEventListener('mouseover', handleMouseOver);
  48. for (let el of [...root.querySelectorAll('*')]) {
  49. removeEventListenerAll(el);
  50. }
  51. }
  52. return {
  53. handleMouseOver,
  54. addEventListenerAll,
  55. removeEventListenerAll
  56. }
  57. }
  58. function handleClick(e) {
  59. if (e.ctrlKey) {
  60. const text = e.target?.innerHTML
  61. .replace(/<.*?>/, '')
  62. .replace('&nbsp;', ' ')
  63. .replace('&lt;', '<')
  64. .replace('&rt;', '>');
  65. if (text) {
  66. console.log(`from CSDNcopyman - You can check your copy text in '[]':\n[\n${text}\n]\n----${new Date().toLocaleString()}`);
  67. navigator.clipboard.writeText(text).then(() => alert(`复制内容:\n${text}`));
  68. } else {
  69. console.log(`from CSDNcopyman - It seems like click on void.----${new Date().toLocaleString()}`)
  70. }
  71. }
  72. }
  73.  
  74. window.addEventListener('mousemove', function () {
  75. const sb = document.querySelector('.imgViewDom');
  76. if (sb) {
  77. sb.innerHTML = '';
  78. sb.classList.remove('imgViewDom');
  79. }
  80. })
  81.  
  82.  
  83. useDomHighlight(document.querySelector('article'));
  84. window.addEventListener('click', handleClick);
  85. })();