UNPKG

@megalo/api

Version:

重新封装各个端中的API,由 megalo 统一对外抛出方法名。目前微信小程序端提供的 API 最为丰富,所以 API 名称以微信小程序为准。

50 lines (40 loc) 1.25 kB
/** * 设置剪贴板内容,参考 https://stackoverflow.com/questions/47879184/document-execcommandcopy-not-working-on-chrome * @params {Object} {data: String} * return {Promise} */ function setClipboardData(params) { let { data } = params; if (data && document.execCommand) { let textarea = document.getElementById('J_clipboard_textarea'); if (!textarea) { textarea = document.createElement('textarea'); textarea.id = 'J_clipboard_textarea'; textarea.style.opacity = 0; textarea.textContent = data; document.body.appendChild(textarea); } let selection = document.getSelection(); let range = document.createRange(); range.selectNode(textarea); selection.removeAllRanges(); selection.addRange(range); document.execCommand('copy'); selection.removeAllRanges(); document.body.removeChild(textarea); return Promise.resolve(); } } // function getClipboardData() { // document.addEventListener('paste', (e) => { // let cbd = e.clipboardData.getData('text'); // }); // } export default { install(Megalo) { Object.assign(Megalo, { setClipboardData, // getClipboardData, }); } };