sspnote java copy button

add java copy button

As of 14.08.2024. See апошняя версія.

You will need to install an extension such as Tampermonkey, Greasemonkey or Violentmonkey to install this script.

You will need to install an extension such as Tampermonkey to install this script.

You will need to install an extension such as Tampermonkey or Violentmonkey to install this script.

You will need to install an extension such as Tampermonkey or Userscripts to install this script.

You will need to install an extension such as Tampermonkey to install this script.

You will need to install a user script manager extension to install this script.

(I already have a user script manager, let me install it!)

You will need to install an extension such as Stylus to install this style.

You will need to install an extension such as Stylus to install this style.

You will need to install an extension such as Stylus to install this style.

You will need to install a user style manager extension to install this style.

You will need to install a user style manager extension to install this style.

You will need to install a user style manager extension to install this style.

(I already have a user style manager, let me install it!)

// ==UserScript==
// @name         sspnote java copy button
// @namespace    http://tampermonkey.net/
// @version      2024年08月14日19:07:44
// @description  add java copy button
// @author       onionycs
// @match        https://www.sspnote.com/detail/*
// @require      http://code.jquery.com/jquery-3.x-git.min.js
// @icon         https://www.google.com/s2/favicons?sz=64&domain=sspnote.com
// @grant        none
// @license      MIT
// ==/UserScript==

(function() {
    'use strict';

    // Your code here...

    /* globals jQuery, $, waitForKeyElements */
    $(document).ready(function() {
        $('.ssp-main-box')[0].style.width='1500px'
    // 找到所有包含具有language-Java类的<code>标签的<pre>标签
    $('pre:has(code.language-Java)').each(function() {
        // 在每个找到的<pre>标签之前插入一个按钮
        $('<button>复制Java代码</button>').insertBefore(this).click(function() {
            // 找到与这个按钮相关联的<pre>标签内的<code>标签
            var $code = $(this).next('pre').find('code.language-Java');

            // 获取<code>标签的文本
            var javaCodeText = $code.text();

            // 复制到剪贴板
            navigator.clipboard.writeText(javaCodeText).then(function() {
                console.log('代码已复制到剪贴板');
                // 可以在这里添加一些UI反馈,比如改变按钮的文本或颜色
                $(this).text('已复制!');
                setTimeout(function() {
                    $(this).text('复制Java代码'); // 恢复按钮文本
                }.bind(this), 2000); // 2秒后恢复
            }, function(err) {
                console.error('复制失败: ', err);
            });

            // 注意:上面的$(this)在navigator.clipboard.writeText的回调中不指向按钮,
            // 所以我们需要使用.bind(this)或者将按钮保存在一个外部变量中
            // 这里我使用了一个匿名函数来捕获正确的this上下文
            (function(button) {
                navigator.clipboard.writeText(javaCodeText).then(function() {
                    console.log('代码已复制到剪贴板');
                    button.text('已复制!');
                    setTimeout(function() {
                        button.text('复制Java代码');
                    }, 2000);
                }, function(err) {
                    console.error('复制失败: ', err);
                });
            })($(this)); // 将$(this)(即按钮)作为参数传递给匿名函数
        });
    });
});
})();