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

js & auto copy

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 = `width: 100%; min-width: 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

refs



©xgqfrms 2012-2020

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

原创文章,版权所有©️xgqfrms, 禁止转载 🈲️,侵权必究⚠️!


posted @ 2019-01-23 11:33  xgqfrms  阅读(253)  评论(2编辑  收藏  举报