sspnote java copy button

add java copy button

  1. // ==UserScript==
  2. // @name sspnote java copy button
  3. // @namespace http://tampermonkey.net/
  4. // @version 2024年8月15日15点43分
  5. // @description add java copy button
  6. // @author onionycs
  7. // @match https://www.sspnote.com/detail/*
  8. // @require http://code.jquery.com/jquery-3.x-git.min.js
  9. // @icon https://www.google.com/s2/favicons?sz=64&domain=sspnote.com
  10. // @grant none
  11. // @license MIT
  12. // ==/UserScript==
  13.  
  14. (function() {
  15. 'use strict';
  16.  
  17. // Your code here...
  18.  
  19. /* globals jQuery, $, waitForKeyElements */
  20. $('.ssp-main-box')[0].style.width='1200px';
  21. var docs= $('.note-content');
  22. $('<button class="increase-width" style="margin-right: 100px">增加200px</button>').insertBefore(docs);
  23. $('<button class="decrease-width">减少200px</button>').insertBefore(docs);
  24. $('.increase-width').click(function() {
  25. var width = parseInt($('.ssp-main-box').first().css('width'), 10) || 0; // 获取当前宽度,如果无法获取则默认为0
  26. $('.ssp-main-box').first().css('width', width + 200 + 'px'); // 增加200px
  27. });
  28.  
  29. // 为减少宽度的按钮添加点击事件
  30. $('.decrease-width').click(function() {
  31. var width = parseInt($('.ssp-main-box').first().css('width'), 10) || 0; // 获取当前宽度,如果无法获取则默认为0
  32. $('.ssp-main-box').first().css('width', Math.max(0, width - 200) + 'px'); // 减少200px,并确保宽度不会小于0
  33. });
  34.  
  35. $('strong').toArray().forEach(s => {
  36. s.style.color = '#65a3f8';
  37. s.style.backgroundColor = 'black';
  38. });
  39.  
  40.  
  41. $(document).ready(function() {
  42. // 找到所有包含具有language-Java类的<code>标签的<pre>标签
  43. $('pre:has(code.language-Java)').each(function() {
  44. // 在每个找到的<pre>标签之前插入一个按钮
  45. $('<button>复制Java代码</button>').insertBefore(this).click(function() {
  46. // 找到与这个按钮相关联的<pre>标签内的<code>标签
  47. var $code = $(this).next('pre').find('code.language-Java');
  48.  
  49. // 获取<code>标签的文本
  50. var javaCodeText = $code.text();
  51.  
  52. // 复制到剪贴板
  53. navigator.clipboard.writeText(javaCodeText).then(function() {
  54. console.log('代码已复制到剪贴板');
  55. // 可以在这里添加一些UI反馈,比如改变按钮的文本或颜色
  56. $(this).text('已复制!');
  57. setTimeout(function() {
  58. $(this).text('复制Java代码'); // 恢复按钮文本
  59. }.bind(this), 2000); // 2秒后恢复
  60. }, function(err) {
  61. console.error('复制失败: ', err);
  62. });
  63.  
  64. // 注意:上面的$(this)在navigator.clipboard.writeText的回调中不指向按钮,
  65. // 所以我们需要使用.bind(this)或者将按钮保存在一个外部变量中
  66. // 这里我使用了一个匿名函数来捕获正确的this上下文
  67. (function(button) {
  68. navigator.clipboard.writeText(javaCodeText).then(function() {
  69. console.log('代码已复制到剪贴板');
  70. button.text('已复制!');
  71. setTimeout(function() {
  72. button.text('复制Java代码');
  73. }, 2000);
  74. }, function(err) {
  75. console.error('复制失败: ', err);
  76. });
  77. })($(this)); // 将$(this)(即按钮)作为参数传递给匿名函数
  78. });
  79. });
  80.  
  81. // 找到所有包含具有language-Java类的<code>标签的<pre>标签
  82. $('pre:has(code.language-java)').each(function() {
  83. // 在每个找到的<pre>标签之前插入一个按钮
  84. $('<button>复制Java代码</button>').insertBefore(this).click(function() {
  85. // 找到与这个按钮相关联的<pre>标签内的<code>标签
  86. var $code = $(this).next('pre').find('code.language-java');
  87.  
  88. // 获取<code>标签的文本
  89. var javaCodeText = $code.text();
  90.  
  91. // 复制到剪贴板
  92. navigator.clipboard.writeText(javaCodeText).then(function() {
  93. console.log('代码已复制到剪贴板');
  94. // 可以在这里添加一些UI反馈,比如改变按钮的文本或颜色
  95. $(this).text('已复制!');
  96. setTimeout(function() {
  97. $(this).text('复制Java代码'); // 恢复按钮文本
  98. }.bind(this), 2000); // 2秒后恢复
  99. }, function(err) {
  100. console.error('复制失败: ', err);
  101. });
  102.  
  103. // 注意:上面的$(this)在navigator.clipboard.writeText的回调中不指向按钮,
  104. // 所以我们需要使用.bind(this)或者将按钮保存在一个外部变量中
  105. // 这里我使用了一个匿名函数来捕获正确的this上下文
  106. (function(button) {
  107. navigator.clipboard.writeText(javaCodeText).then(function() {
  108. console.log('代码已复制到剪贴板');
  109. button.text('已复制!');
  110. setTimeout(function() {
  111. button.text('复制Java代码');
  112. }, 2000);
  113. }, function(err) {
  114. console.error('复制失败: ', err);
  115. });
  116. })($(this)); // 将$(this)(即按钮)作为参数传递给匿名函数
  117. });
  118. });
  119. });
  120.  
  121. })();