网页复制内容追加到剪切板

function addLink() {
            let body_element = document.body;
            let selection = window.getSelection();
            let locationHref = document.location.href;
            let appendLink = "\r\n\r\n 原文出自[ ushowtime ] 转载请保留原文链接: <a href='" + locationHref + "'>" + locationHref + "</a>";
            if (window.clipboardData) { // Internet Explorer
                let copytext = selection + appendLink;
                window.clipboardData.setData("Text", copytext);
                return false;
            } else {
                let copytext = selection + appendLink;
                let newdiv = document.createElement('div');
                newdiv.style.position = 'absolute';
                newdiv.style.left = '-99999px';
                body_element.appendChild(newdiv);
                newdiv.innerHTML = copytext;
                selection.selectAllChildren(newdiv);
                window.setTimeout(function() {
                    body_element.removeChild(newdiv);
                }, 0);
            }
        }

 

posted @ 2021-06-12 13:47  执笔coding  阅读(106)  评论(0编辑  收藏  举报