工学1号馆

home

复制文本加上版权信息功能的实现

Wu Yudong    February 26, 2021     HTML5+CSS3   970   

当我们复制别的网站的文章的时候会自带复制版权提醒的信息,正好我的项目中也需要这个功能,于是实现了一下,分享给大家:

//复制自带版权信息
document.body.addEventListener('copy', function (e) {
    if (window.getSelection().toString() && window.getSelection().toString().length > 42) {
        setClipboardText(e);
    }
});

function setClipboardText(event) {
    var clipboardData = event.clipboardData || window.clipboardData;
    var author = $(".post_author").text();
    if (clipboardData) {
        event.preventDefault();
        var htmlData = ''
            + window.getSelection().toString();
            + '<br><br>作者:' + author + '<br>'
            + '链接:' + window.location.href + '<br>'
            + '来源:工学1号馆<br>'
            + '著作权归作者所有。商业转载请联系作者获得授权,非商业转载请注明出处。<br>';
        var textData = ''
            + window.getSelection().toString()
            + '\n\n作者:' + author + '\n'
            + '链接:' + window.location.href + '\n'
            + '来源:工学1号馆\n'
            + '著作权归作者所有。商业转载请联系作者获得授权,非商业转载请注明出处。\n';
        clipboardData.setData('text/html', htmlData);
        clipboardData.setData('text/plain',textData);
    }
}

亲测可用😄

如果文章对您有帮助,欢迎点击下方按钮打赏作者

Comments

No comments yet.
To verify that you are human, please fill in "七"(required)