Copyright dog retreats - imitation of Zhihu copied text comes with a copyright statement

December 26, 2015 · Sharing
Table of Contents Expand

What the hell?

The copyright dog is gone, and my mother no longer has to worry about me being infringed (actually, I only guard against gentlemen, not villains).

Similar to Zhihu, when the website copies text longer than 42 characters, it automatically adds this copyright statement:

著作权归作者所有。
商业转载请联系作者获得授权,非商业转载请注明出处。
作者:DIYgod
链接:https://www.anotherhome.net/
来源:Anotherhome

Principle

  • Listen for copy events

  • Use window.getSelection() to get the selected text

  • Use clipboardData.setData to manipulate the contents of the clipboard

Code

document.body.addEventListener('copy', function (e) {
    if (window.getSelection().toString() && window.getSelection().toString().length > 42) {
        setClipboardText(e);
        alert('商业转载请联系作者获得授权,非商业转载请注明出处,谢谢合作。');
    }
});

function setClipboardText(event) {
    var clipboardData = event.clipboardData || window.clipboardData;
    if (clipboardData) {
        event.preventDefault();

        var htmlData = ''
            + '著作权归作者所有。<br>'
            + '商业转载请联系作者获得授权,非商业转载请注明出处。<br>'
            + '作者:DIYgod<br>'
            + '链接:' + window.location.href + '<br>'
            + '来源:Anotherhome<br><br>'
            + window.getSelection().toString();
        var textData = ''
            + '著作权归作者所有。\n'
            + '商业转载请联系作者获得授权,非商业转载请注明出处。\n'
            + '作者:DIYgod\n'
            + '链接:' + window.location.href + '\n'
            + '来源:Anotherhome\n\n'
            + window.getSelection().toString();

        clipboardData.setData('text/html', htmlData);
        clipboardData.setData('text/plain',textData);
    }
}

Known issues

iOS Safari is not compatible with the clipboardData.setData() method, so it is invalid on iOS Safari

Zhihu original version

Attached is the original key code of Zhihu. The principle is similar. If you are interested, you can also take a look:

var lz = function (a, b, c) {
    function d(a, b) {
        return ["著作权归作者所有。", "商业转载请联系作者获得授权,非商业转载请注明出处。", "作者:" + b, "链接:" + a, "来源:知乎", "", ""]
    }

    function f(a, b, c) {
        return "\x3cdiv\x3e" + d(b, c).join("\x3cbr /\x3e") + a + "\x3c/div\x3e"
    }

    function g(a) {
        var g = z.Wq(), m = g && (0, z.ib)(g.Ed());
        if (m && !(42 > m.length)) {
            if ("object" === typeof a.originalEvent.clipboardData && (a.originalEvent.clipboardData.setData("text/html", f(g.Of(), b, c)), a.originalEvent.clipboardData.setData("text/plain", d(b, c).join("\n") + m), 0 < a.originalEvent.clipboardData.getData("text/plain").length)) {
                a.preventDefault();
                return
            }
            if (window.getSelection) {
                a = g.Of();
                var n = (0, window.$)(f(a, b, c)).css({position: "fixed", left: "-9999px"}).appendTo("body");
                window.getSelection().selectAllChildren(n.get(0));
                (0, window.setTimeout)(function () {
                    g.select();
                    n.remove()
                }, 200)
            }
        }
    }

    a && b && c && (z.Fa(b, "http") || (b = window.location.protocol + "//" + window.location.host + b), a.on("copy", g))
};
DIYgod Hi, DIYgod

Running for 4344 days

© 2026 DIYgod. All rights reserved. Please credit when sharing.