banner
DIYgod

Hi, DIYgod

写代码是热爱,写到世界充满爱!
github
twitter
follow
bilibili
telegram
email
steam
playstation
nintendo switch

Copyright dog retreats - Copying text from Zhihu comes with its own copyright statement.

What the hell?#

Copyright dog retreat, mom no longer has to worry about me being infringed (actually only prevent gentlemen).

Similar to Zhihu, when copying text longer than 42 characters on the website, automatically add such a copyright statement:

Copyright belongs to the author.
For commercial reprint, please contact the author for authorization. For non-commercial reprint, please indicate the source.
Author: DIYgod
Link: https://www.anotherhome.net/
Source: Anotherhome

Principle#

  • Listen for copy events

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

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

Code#

document.body.addEventListener('copy', function (e) {
    if (window.getSelection().toString() && window.getSelection().toString().length > 42) {
        setClipboardText(e);
        alert('For commercial reprint, please contact the author for authorization. For non-commercial reprint, please indicate the source. Thank you for your cooperation.');
    }
});

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

        var htmlData = ''
            + 'Copyright belongs to the author.<br>'
            + 'For commercial reprint, please contact the author for authorization. For non-commercial reprint, please indicate the source.<br>'
            + 'Author: DIYgod<br>'
            + 'Link: ' + window.location.href + '<br>'
            + 'Source: Anotherhome<br><br>'
            + window.getSelection().toString();
        var textData = ''
            + 'Copyright belongs to the author.\n'
            + 'For commercial reprint, please contact the author for authorization. For non-commercial reprint, please indicate the source.\n'
            + 'Author: DIYgod\n'
            + 'Link: ' + window.location.href + '\n'
            + 'Source: 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 ineffective on iOS Safari.

Zhihu Original Version#

The following is the key code of the original version on Zhihu. The principle is similar, you can also take a look if you are interested:

var lz = function (a, b, c) {
    function d(a, b) {
        return ["Copyright belongs to the author.", "For commercial reprint, please contact the author for authorization. For non-commercial reprint, please indicate the source.", "Author: " + b, "Link: " + a, "Source: Zhihu", "", ""]
    }

    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))
};
Loading...
Ownership of this post data is guaranteed by blockchain and smart contracts to the creator alone.