CSDN Copyman

Press S and click to copy without login

As of 2023-08-28. See the latest version.

  1. // ==UserScript==
  2. // @name CSDN Copyman
  3. // @namespace http://tampermonkey.net/
  4. // @version 0.1.6
  5. // @description Press S 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.clientWidth + 'px';
  28. s.height = cur.clientHeight + '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.replace(/<.*?>/, '');
  61. if (text) {
  62. console.log(`from CSDNcopyman - You can check your copy text in '[]':\n[\n${text}\n]\n----${new Date().toLocaleString()}`);
  63. navigator.clipboard.writeText(text).then(() => alert(`复制内容:\n${text}`));
  64. } else {
  65. console.log(`from CSDNcopyman - It seems like click on void.----${new Date().toLocaleString()}`)
  66. }
  67. }
  68. }
  69.  
  70. window.addEventListener('mousemove', function () {
  71. const sb = document.querySelector('.imgViewDom');
  72. if (sb) {
  73. sb.innerHTML = '';
  74. sb.classList.remove('imgViewDom');
  75. }
  76. })
  77.  
  78. useDomHighlight(document.querySelector('article'));
  79. window.addEventListener('click', handleClick);
  80. })();