xgqfrms™, xgqfrms® : xgqfrms's offical website of GitHub!

js & auto copy

https://developer.mozilla.org/zh-CN/docs/Web/Events/copy

Ctrl + C

Command + C

document.execCommand("copy");

https://developer.mozilla.org/zh-CN/docs/Web/API/Document/execCommand

https://developer.mozilla.org/zh-CN/docs/Mozilla/Add-ons/WebExtensions/API/tabs/executeScript


document.execCommand("copy");

Chrome copy


JSON.stringify(JSON.parse(window.sessionStorage.getItem("flipObj")), null, 4);

let obj = {
    "dataType": "0",
    "publishTime": "2019-01-23 10:18:46",
    "newsId": "05201901230103749163041",
    "dataStatus": "",
    "handleUser": "",
    "xwbt": "",
    "mtcc": "",
    "gkbz": ""
};

copy(obj);

chromeCopy() {
    try {
        if (window.copy) {
            window.copy(obj);
        }
    } catch (error) {
        //
    }
},


click copy & demo


    clickGetNewsLink(data_ref = `newsLink`) {
        let that = this;
        let newsLink = document.querySelector(`[data-uid="${data_ref}"]`);
        // let newsLink = document.querySelector(`[data-uid="newsLink"]`);
        if (newsLink) {
            let result = that.commonHandle.newsLink || ``;
            let input = newsLink.lastElementChild;
            input.addEventListener(`click`, (e) => {
                try {
                    if (result) {
                        input.select();
                        document.execCommand("copy");
                        that.$hMessage.success(`资讯链接地址, 复制成功!`);
                    } else {
                        that.$hMessage.info(`资讯链接地址, 无法复制!`);
                    }
                } catch (err) {
                    console.log(`click copy error =`, err);
                    that.$hMessage.error(`你的浏览器太古老了,暂时不支持点击复制的功能!`);
                }
            });
        }
    },
    clickGetNewsId() {
        let that = this;
        let newsID = document.querySelector(`[data-uid="newsId"]`);
        if (newsID) {
            let result = that.commonHandle.newsId || ``;
            let input = newsID.lastElementChild;
            // input.style = ` 100%; min- 217px; color: #000; cursor: pointer !important;`;
            input.addEventListener(`click`, (e) => {
                try {
                    if (result) {
                        input.select();
                        document.execCommand("copy");
                        that.$hMessage.success(`资讯 ID, 复制成功!`);
                    } else {
                        that.$hMessage.info(`资讯 ID 为空, 无法复制!`);
                    }
                } catch (err) {
                    console.log(`click copy error =`, err);
                    that.$hMessage.error(`你的浏览器太古老了,暂时不支持点击复制的功能!`);
                }
            });
        } else {
            //
        }
    },

getSelection & execCommand

window.getSelection().toString()


window.document.execCommand(`copy`);
// true

document.execCommand(`copy`);
// true


©xgqfrms 2012-2020

www.cnblogs.com 发布文章使用:只允许注册用户才可以访问!


原文地址:https://www.cnblogs.com/xgqfrms/p/10307949.html