匿名
未登录
中文(简体)
登录
Dhamma.cn
搜索
有觉性 以安住且中立的心 照见身心的实相
查看“︁MediaWiki:Common.js”︁的源代码
来自Dhamma.cn
命名空间
系统消息
讨论
更多
更多
页面操作
阅读
查看源代码
历史
刷新
←
MediaWiki:Common.js
因为以下原因,您没有权限编辑该页面:
您请求的操作仅限属于该用户组的用户执行:
管理员
此页面为本wiki上的软件提供界面文本,并受到保护以防止滥用。如欲修改所有wiki的翻译,请访问
translatewiki.net
上的MediaWiki本地化项目。
您无权编辑此JavaScript页面,因为编辑此页面可能会影响所有访问者。
您可以查看和复制此页面的源代码。
/* ========================================================= Dhamma.cn 划词复制开示及 UrlShortener 短网址落款(精准侧边栏版) ========================================================= */ (function () { if (window.dhammaCopyLoaded) return; window.dhammaCopyLoaded = true; function initDhammaCopy() { var cachedShortUrl = ''; // 统一域名修正:强制修正为 https://www.dhamma.cn function forceMainDomain(url) { if (!url) return 'https://www.dhamma.cn'; if (url.indexOf('http') !== 0) { return 'https://www.dhamma.cn' + (url.indexOf('/') === 0 ? '' : '/') + url; } return url.replace(/^https?:\/\/[^\/]+/, 'https://www.dhamma.cn'); } // 1. 异步获取真正的页面短网址 function getShortUrlAsync() { if (cachedShortUrl) return Promise.resolve(cachedShortUrl); // 关键修复:仅在侧边栏/工具栏(#t-shorturl 或 #p-tb)查找官方短网址,避免抓取正文内嵌入的表单/外链 var tShort = document.querySelector('#t-shorturl a, #p-tb a[href*="/s/"], .mw-portlet-tb a[href*="/s/"]'); if (tShort && tShort.href && tShort.href.indexOf('/s/') !== -1) { cachedShortUrl = forceMainDomain(tShort.href); return Promise.resolve(cachedShortUrl); } // 若侧边栏未开启或不存在短链接,则构建当前页面的规范 URL 并向 API 申请短网址 var pageName = (window.mw && mw.config && mw.config.get('wgPageName')) || ''; var scriptPath = (window.mw && mw.config && mw.config.get('wgScriptPath')) || ''; var targetUrl = ''; if (pageName) { targetUrl = 'https://www.dhamma.cn' + scriptPath + '/index.php?title=' + encodeURIComponent(pageName); } else { targetUrl = forceMainDomain(window.location.href.split('?')[0].split('#')[0]); } // POST 请求 UrlShortener API var apiUrl = scriptPath + '/api.php'; var params = new URLSearchParams(); params.append('action', 'shortenurl'); params.append('format', 'json'); params.append('url', targetUrl); return fetch(apiUrl, { method: 'POST', headers: { 'Content-Type': 'application/x-www-form-urlencoded' }, body: params.toString() }) .then(function (res) { return res.json(); }) .then(function (data) { if (data && data.shortenurl && data.shortenurl.shorturl) { cachedShortUrl = forceMainDomain(data.shortenurl.shorturl); } else { cachedShortUrl = targetUrl; } return cachedShortUrl; }) .catch(function () { cachedShortUrl = targetUrl; return cachedShortUrl; }); } // 后台预取 getShortUrlAsync(); // 2. 创建浮窗节点 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; transition:background 0.2s;'; btn.onmouseover = function () { if (this.innerText.indexOf('✅') === -1) this.style.background = '#3b5343'; }; btn.onmouseout = function () { if (this.innerText.indexOf('✅') === -1) this.style.background = '#4d6b57'; }; pop.appendChild(btn); document.body.appendChild(pop); } return pop; } var currentText = ''; // 3. 处理划词选择逻辑 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(); getShortUrlAsync().then(function (shortUrl) { 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 output = '“' + currentText + '”\n\n' + '----------------------------------------\n' + '摘自:' + site + '\n' + '标题:' + title + '\n' + '地址:' + shortUrl + '\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); } }); }; } } // 4. 反馈与降级处理 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'; } }); } if (document.readyState === 'loading') { document.addEventListener('DOMContentLoaded', initDhammaCopy); } else { initDhammaCopy(); } })();
返回
MediaWiki:Common.js
。
导航
导航
首页
随机页面
wiki工具
wiki工具
上传文件
特殊页面
获取短链接
页面工具
页面工具
用户页面工具
更多
链入页面
相关更改
页面信息
页面日志