|
|
| 第1行: |
第1行: |
| /* 将为Timeless皮肤的用户加载此处的所有JavaScript */
| |
|
| |
|
|
| |
| /* =========================================================
| |
| Dhamma.cn 划词复制开示及短网址落款 - 独立稳定版
| |
| ========================================================= */
| |
| (function () {
| |
| if (window.dhammaCopyLoaded) return;
| |
| window.dhammaCopyLoaded = true;
| |
|
| |
| function getPopover() {
| |
| var pop = document.getElementById('dhamma-copy-popover');
| |
| if (!pop) {
| |
| pop = document.createElement('div');
| |
| pop.id = 'dhamma-copy-popover';
| |
| pop.style.cssText = 'display:none; position:fixed; z-index:2147483647; background:#ffffff; border:1px solid rgba(0,0,0,0.18); border-radius:8px; box-shadow:0 6px 20px rgba(0,0,0,0.25); padding:6px; font-size:13px; line-height:1.4; white-space:nowrap;';
| |
|
| |
| var btn = document.createElement('button');
| |
| btn.id = 'dhamma-copy-btn';
| |
| btn.innerText = '复制该段开示(带出处)';
| |
| btn.style.cssText = 'background:#4d6b57; color:#ffffff; border:none; border-radius:5px; padding:6px 14px; font-size:13px; cursor:pointer; font-weight:600; font-family:sans-serif;';
| |
|
| |
| pop.appendChild(btn);
| |
| document.body.appendChild(pop);
| |
| }
| |
| return pop;
| |
| }
| |
|
| |
| function getShortUrl() {
| |
| var link = document.querySelector('link[rel="shortlink"]');
| |
| if (link && link.href) {
| |
| return link.href.indexOf('http') === 0 ? link.href : window.location.origin + link.href;
| |
| }
| |
| if (window.mw && mw.config && mw.config.get('wgShortUrl')) {
| |
| var shortUrl = mw.config.get('wgShortUrl');
| |
| return shortUrl.indexOf('http') === 0 ? shortUrl : window.location.origin + shortUrl;
| |
| }
| |
| var sidebarLink = document.querySelector('#t-shorturl a, #t-permalink a');
| |
| if (sidebarLink && sidebarLink.href) {
| |
| return sidebarLink.href.indexOf('http') === 0 ? sidebarLink.href : window.location.origin + sidebarLink.href;
| |
| }
| |
| return window.location.href.split('#')[0].split('?')[0];
| |
| }
| |
|
| |
| var currentText = '';
| |
|
| |
| function handleSelection() {
| |
| var popover = getPopover();
| |
| var btn = document.getElementById('dhamma-copy-btn');
| |
| var sel = window.getSelection();
| |
| var text = sel ? sel.toString().trim() : '';
| |
|
| |
| if (!text || text.length < 2) {
| |
| popover.style.display = 'none';
| |
| return;
| |
| }
| |
|
| |
| currentText = text;
| |
|
| |
| if (sel.rangeCount > 0) {
| |
| var range = sel.getRangeAt(0);
| |
| var rect = range.getBoundingClientRect();
| |
|
| |
| if (rect.width === 0 || rect.height === 0) {
| |
| popover.style.display = 'none';
| |
| return;
| |
| }
| |
|
| |
| var popWidth = popover.offsetWidth || 190;
| |
| var popHeight = popover.offsetHeight || 38;
| |
|
| |
| var top = rect.top - popHeight - 10;
| |
| var left = rect.left + (rect.width / 2) - (popWidth / 2);
| |
|
| |
| if (left < 10) left = 10;
| |
| if (top < 10) top = rect.bottom + 10;
| |
|
| |
| popover.style.top = top + 'px';
| |
| popover.style.left = left + 'px';
| |
| popover.style.display = 'block';
| |
|
| |
| btn.onclick = function (e) {
| |
| e.stopPropagation();
| |
| e.preventDefault();
| |
|
| |
| var title = (window.mw && mw.config && mw.config.get('wgTitle')) || document.title;
| |
| var site = (window.mw && mw.config && mw.config.get('wgSiteName')) || 'Dhamma.cn';
| |
| var url = getShortUrl();
| |
|
| |
| var output =
| |
| '“' + currentText + '”\n\n' +
| |
| '----------------------------------------\n' +
| |
| '法谈摘自:' + site + ' ' + title + ' \n' +
| |
| '该篇网址:' + url + '\n' +
| |
| '转载请保持原文,随喜分享,Sadhu!';
| |
|
| |
| if (navigator.clipboard && navigator.clipboard.writeText) {
| |
| navigator.clipboard.writeText(output).then(function () {
| |
| showSuccess(btn, popover);
| |
| }).catch(function () {
| |
| fallbackCopy(output, btn, popover);
| |
| });
| |
| } else {
| |
| fallbackCopy(output, btn, popover);
| |
| }
| |
| };
| |
| }
| |
| }
| |
|
| |
| function showSuccess(btn, popover) {
| |
| btn.innerText = '✅ 已复制开示及出处';
| |
| btn.style.background = '#2e7d32';
| |
| setTimeout(function () {
| |
| btn.innerText = '📋 复制该段开示(带出处)';
| |
| btn.style.background = '#4d6b57';
| |
| popover.style.display = 'none';
| |
| }, 1200);
| |
| }
| |
|
| |
| function fallbackCopy(text, btn, popover) {
| |
| var ta = document.createElement('textarea');
| |
| ta.value = text;
| |
| ta.style.position = 'fixed';
| |
| ta.style.opacity = '0';
| |
| document.body.appendChild(ta);
| |
| ta.select();
| |
| document.execCommand('copy');
| |
| document.body.removeChild(ta);
| |
| showSuccess(btn, popover);
| |
| }
| |
|
| |
| document.addEventListener('mouseup', function () { setTimeout(handleSelection, 30); });
| |
| document.addEventListener('touchend', function () { setTimeout(handleSelection, 30); });
| |
|
| |
| document.addEventListener('mousedown', function (e) {
| |
| var pop = document.getElementById('dhamma-copy-popover');
| |
| if (pop && !pop.contains(e.target)) {
| |
| pop.style.display = 'none';
| |
| }
| |
| });
| |
| })();
| |