净化剪切板

还原被劫持的剪切板

// ==UserScript==
// @name         净化剪切板
// @namespace    
// @version      0.4.2
// @description  还原被劫持的剪切板
// @author       renmu
// @match        *://*.juejin.cn/*
// @match        *://*.zhihu.com/*
// @match        *://*.leetcode-cn.com/*

// @grant        none
// ==/UserScript==

function getSelectionHtml() {
    var html = "";
    if (typeof window.getSelection != "undefined") {
        var sel = window.getSelection();
        if (sel.rangeCount) {
            var container = document.createElement("div");
            for (var i = 0, len = sel.rangeCount; i < len; ++i) {
                container.appendChild(sel.getRangeAt(i).cloneContents());
            }
            html = container.innerHTML;
        }
    } else if (typeof document.selection != "undefined") {
        if (document.selection.type == "Text") {
            html = document.selection.createRange().htmlText;
        }
    }
    return html;
}

function addLink(e) {
    e.preventDefault();
    var copytext = getSelectionHtml();
    console.log(copytext,window.getSelection())
    var clipboardData = event.clipboardData || window.clipboardData;

    clipboardData.setData("text/html", copytext);
    clipboardData.setData("text", window.getSelection());
}


document.addEventListener("copy", addLink);