PageSourceSearch

https://a-egoist.github.io/libs/codeBlock/codeCopy.js

js a-egoist.github.io collected 2026-10-03 08:32:02 UTC 2,193 bytes, 57 lines download raw bytes

1// 代码块一键复制
2
3$(function () {
4    var $copyIcon = $('<i class="fas fa-copy code_copy" title="复制代码" aria-hidden="true"></i>')
5    var $notice = $('<div class="codecopy_notice"></div>')
6    $('.code-area').prepend($copyIcon)
7    $('.code-area').prepend($notice)
8    // “复制成功”字出现
9    function copy(text, ctx) {
10        if (document.queryCommandSupported && document.queryCommandSupported('copy')) {
11            try {
12                document.execCommand('copy') // Security exception may be thrown by some browsers.
13                $(ctx).prev('.codecopy_notice')
14                    .text("复制成功")
15                    .animate({
16                        opacity: 1,
17                        top: 30
18                    }, 450, function () {
19                        setTimeout(function () {
20                            $(ctx).prev('.codecopy_notice').animate({
21                                opacity: 0,
22                                top: 0
23                            }, 650)
24                        }, 400)
25                    })
26            } catch (ex) {
27                $(ctx).prev('.codecopy_notice')
28                    .text("复制失败")
29                    .animate({
30                        opacity: 1,
31                        top: 30
32                    }, 650, function () {
33                        setTimeout(function () {
34                            $(ctx).prev('.codecopy_notice').animate({
35                                opacity: 0,
36                                top: 0
37                            }, 650)
38                        }, 400)
39                    })
40                return false
41            }
42        } else {
43            $(ctx).prev('.codecopy_notice').text("浏览器不支持复制")
44        }
45    }
46    // 复制
47    $('.code-area .fa-copy').on('click', function () {
48        var selection = window.getSelection()
49        var range = document.createRange()
50        range.selectNodeContents($(this).siblings('pre').find('code')[0])
51        selection.removeAllRanges()
52        selection.addRange(range)
53        var text = selection.toString()
54        copy(text, this)
55        selection.removeAllRanges()
56    })
57});

Line numbers count LF bytes from the start of the resource, as the search results do. Vendor segments are library code the classifier recognised; they are stored but not indexed. Bytes are shown as Latin1 characters, one per byte.