handle: function() { if (!event.target.id) return; let url = dq('.long-url').value || window.location.href; let button = event.target.id; let oldText = event.target.innerText; if (button == 'operation'){ if (oldText=='生成') { suoIm.shortUrl(url); event.target.innerText = '清空'; } else { dq('.long-url').value = ''; event.target.innerText = '生成'; } } else { if (button == 'copy' && !dq('.long-url').value){ dq('.long-url').setAttribute('placeholder','请先点 [生成] 后再复制'); return; } dq('.dialog-container').remove(); dq('style:last-child').remove(); } }, shortUrl: function(long_url) { /* let long_url = 'https://www.bilibili.com/video/BV1EW411u7th?p=1'; */ this.jsonp({ format:'jsonp', key: suo_key, expireDate:'2030-03-31', url: encodeURIComponent(long_url) },'jsoncallback'); },
jsonp: function(data, callback) { let queryString = '?'; for(let k in data){ queryString += k + '=' + data[k] + '&'; } /*回调函数*/ let _url = suo_api + queryString + 'callback=' + callback; let head = document.getElementsByTagName('head')[0]; let script = document.createElement('script'); head.appendChild(script); /*创建jsonp回调函数*/ window[callback] = json => { dq('.long-url').value = json.url; dq('.long-url').select(); dq('#copy').addEventListener('click', this.copyText); head.removeChild(script); window[callback] = null; }; /*发送请求*/ script.src = _url; },
copyText: function() { let ele = dq('.long-url'); if (navigator.userAgent.match(/ipad|iphone/i)) { let range = document.createRange(); range.selectNode(ele); window.getSelection().addRange(range); document.execCommand('copy');
/*Remove the selections - NOTE: Should use*/ window.getSelection().removeAllRanges(); } else { ele.focus(); ele.select(); document.execCommand('copy'); } } };