有觉性 以安住且中立的心 照见身心的实相

MediaWiki:Common.js:修订间差异

来自Dhamma.cn
法藏​(留言 | 贡献)
小无编辑摘要
法藏​(留言 | 贡献)
小无编辑摘要
第1行: 第1行:
/* =========================================================
  Dhamma.cn 划词复制开示及 UrlShortener 短网址落款(纯前端 JS 版)
  适用于:MediaWiki:Common.js / MediaWiki:Timeless.js
  ========================================================= */
(function () {
(function () {
     if (window.dhammaCopyLoaded) return;
     if (window.dhammaCopyLoaded) return;
     window.dhammaCopyLoaded = true;
     window.dhammaCopyLoaded = true;


     var cachedShortUrl = '';
     function initDhammaCopy() {
        var cachedShortUrl = '';


    // 异步通过 POST 方式向 UrlShortener API 请求真正的 /s/ 短网址
        // 统一域名修正:强制将任何子域名(如 reg.dhamma.cn)修正为规范的 https://www.dhamma.cn
    function getShortUrlAsync() {
        function normalizeUrl(url) {
        if (cachedShortUrl) return Promise.resolve(cachedShortUrl);
            if (!url) return url;
 
            return url.replace(/:\/\/[^\/]+/, '://www.dhamma.cn');
        // 1. 优先在 DOM 中查找侧边栏或页面现成的 /s/ 短链接
        var sLink = document.querySelector('a[href*="/s/"]');
        if (sLink && sLink.href && sLink.href.indexOf('/s/') !== -1) {
            cachedShortUrl = sLink.href;
            return Promise.resolve(cachedShortUrl);
         }
         }


         var tShort = document.querySelector('#t-shorturl a');
         // 1. 异步通过 POST 方式向 UrlShortener API 请求真正的 /s/ 短网址
         if (tShort && tShort.href && tShort.href.indexOf('/s/') !== -1) {
         function getShortUrlAsync() {
             cachedShortUrl = tShort.href;
             if (cachedShortUrl) return Promise.resolve(cachedShortUrl);
            return Promise.resolve(cachedShortUrl);
        }


        // 2. 构建干净的页面原生 URL(彻底剥离 &oldid=12715 等历史版本参数)
            // 优先在 DOM 中查找现成的 /s/ 短链接
        var pageName = (window.mw && mw.config && mw.config.get('wgPageName')) || '';
            var sLink = document.querySelector('a[href*="/s/"]');
        var scriptPath = (window.mw && mw.config && mw.config.get('wgScriptPath')) || '';
            if (sLink && sLink.href && sLink.href.indexOf('/s/') !== -1) {
        var targetUrl = '';
                cachedShortUrl = normalizeUrl(sLink.href);
       
                return Promise.resolve(cachedShortUrl);
        if (pageName) {
            }
            targetUrl = window.location.origin + scriptPath + '/index.php?title=' + pageName;
        } else {
            targetUrl = window.location.href.split('?')[0].split('#')[0];
        }


        // 3. MediaWiki 规范:action=shortenurl 必须以 POST 提交
            var tShort = document.querySelector('#t-shorturl a');
        var apiUrl = scriptPath + '/api.php';
            if (tShort && tShort.href && tShort.href.indexOf('/s/') !== -1) {
        var params = new URLSearchParams();
                cachedShortUrl = normalizeUrl(tShort.href);
        params.append('action', 'shortenurl');
                return Promise.resolve(cachedShortUrl);
        params.append('format', 'json');
            }
        params.append('url', targetUrl);


        return fetch(apiUrl, {
             // 构建干净的页面原生 URL(彻底剥离 &oldid=12715 等历史版本参数)
             method: 'POST',
             var pageName = (window.mw && mw.config && mw.config.get('wgPageName')) || '';
             headers: { 'Content-Type': 'application/x-www-form-urlencoded' },
             var scriptPath = (window.mw && mw.config && mw.config.get('wgScriptPath')) || '';
             body: params.toString()
            var targetUrl = '';
        })
           
        .then(function (res) { return res.json(); })
             if (pageName) {
        .then(function (data) {
                 targetUrl = 'https://www.dhamma.cn' + scriptPath + '/index.php?title=' + pageName;
             if (data && data.shortenurl && data.shortenurl.shorturl) {
                 cachedShortUrl = data.shortenurl.shorturl;
             } else {
             } else {
                 cachedShortUrl = targetUrl;
                 targetUrl = normalizeUrl(window.location.href.split('?')[0].split('#')[0]);
             }
             }
            return cachedShortUrl;
        })
        .catch(function () {
            cachedShortUrl = targetUrl;
            return cachedShortUrl;
        });
    }


    // 页面加载时后台静默预取短网址
            // MediaWiki 规范:action=shortenurl 必须以 POST 提交
    getShortUrlAsync();
            var apiUrl = scriptPath + '/api.php';
            var params = new URLSearchParams();
            params.append('action', 'shortenurl');
            params.append('format', 'json');
            params.append('url', targetUrl);


    // 创建浮窗节点
            return fetch(apiUrl, {
    function getPopover() {
                method: 'POST',
        var pop = document.getElementById('dhamma-copy-popover');
                headers: { 'Content-Type': 'application/x-www-form-urlencoded' },
        if (!pop) {
                body: params.toString()
            pop = document.createElement('div');
             })
            pop.id = 'dhamma-copy-popover';
             .then(function (res) { return res.json(); })
            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;';
             .then(function (data) {
              
                if (data && data.shortenurl && data.shortenurl.shorturl) {
             var btn = document.createElement('button');
                    cachedShortUrl = normalizeUrl(data.shortenurl.shorturl);
             btn.id = 'dhamma-copy-btn';
                } else {
            btn.innerText = '复制该段开示(带出处)';
                    cachedShortUrl = targetUrl;
            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;';
                }
              
                return cachedShortUrl;
             pop.appendChild(btn);
             })
             document.body.appendChild(pop);
             .catch(function () {
                cachedShortUrl = targetUrl;
                return cachedShortUrl;
             });
         }
         }
        return pop;
    }


    var currentText = '';
        // 页面加载时后台静默预取短网址
        getShortUrlAsync();


    function handleSelection() {
        // 2. 创建浮窗节点
        var popover = getPopover();
        function getPopover() {
        var btn = document.getElementById('dhamma-copy-btn');
            var pop = document.getElementById('dhamma-copy-popover');
        var sel = window.getSelection();
            if (!pop) {
        var text = sel ? sel.toString().trim() : '';
                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'; };


        if (!text || text.length < 2) {
                pop.appendChild(btn);
            popover.style.display = 'none';
                document.body.appendChild(pop);
             return;
            }
             return pop;
         }
         }


         currentText = text;
         var currentText = '';


         if (sel.rangeCount > 0) {
         // 3. 处理划词选择逻辑
             var range = sel.getRangeAt(0);
        function handleSelection() {
             var rect = range.getBoundingClientRect();
             var popover = getPopover();
            var btn = document.getElementById('dhamma-copy-btn');
            var sel = window.getSelection();
             var text = sel ? sel.toString().trim() : '';


             if (rect.width === 0 || rect.height === 0) {
             if (!text || text.length < 2) {
                 popover.style.display = 'none';
                 popover.style.display = 'none';
                 return;
                 return;
             }
             }


             var popWidth = popover.offsetWidth || 190;
             currentText = text;
            var popHeight = popover.offsetHeight || 38;
 
            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 top = rect.top - popHeight - 10;
            var left = rect.left + (rect.width / 2) - (popWidth / 2);
                var left = rect.left + (rect.width / 2) - (popWidth / 2);


            if (left < 10) left = 10;
                if (left < 10) left = 10;
            if (top < 10) top = rect.bottom + 10;
                if (top < 10) top = rect.bottom + 10;


            popover.style.top = top + 'px';
                popover.style.top = top + 'px';
            popover.style.left = left + 'px';
                popover.style.left = left + 'px';
            popover.style.display = 'block';
                popover.style.display = 'block';


            btn.onclick = function (e) {
                btn.onclick = function (e) {
                e.stopPropagation();
                    e.stopPropagation();
                e.preventDefault();
                    e.preventDefault();


                getShortUrlAsync().then(function (shortUrl) {
                    getShortUrlAsync().then(function (shortUrl) {
                    var title = (window.mw && mw.config && mw.config.get('wgTitle')) || document.title;
                        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 site = (window.mw && mw.config && mw.config.get('wgSiteName')) || 'Dhamma.cn';


                // 格式化输出:摘自、标题、地址、声明各独占一行
                        // 格式化输出:摘自、标题、地址、声明各独占一行
                    var output =  
                        var output =  
                        '“' + currentText + '”\n\n' +
                            '“' + currentText + '”\n\n' +
                        '----------------------------------------\n' +
                            '----------------------------------------\n' +
                        '摘自:' + site + '\n' +
                            '摘自:' + site + '\n' +
                        '标题:' + title + '\n' +
                            '标题:' + title + '\n' +
                        '地址:' + shortUrl + '\n' +
                            '地址:' + shortUrl + '\n' +
                        '转载请保持原文,随喜分享,Sadhu!';
                            '转载请保持原文,随喜分享,Sadhu!';


                    if (navigator.clipboard && navigator.clipboard.writeText) {
                        if (navigator.clipboard && navigator.clipboard.writeText) {
                        navigator.clipboard.writeText(output).then(function () {
                            navigator.clipboard.writeText(output).then(function () {
                            showSuccess(btn, popover);
                                showSuccess(btn, popover);
                        }).catch(function () {
                            }).catch(function () {
                                fallbackCopy(output, btn, popover);
                            });
                        } else {
                             fallbackCopy(output, btn, popover);
                             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);
        }
 
        // 5. 兼容性降级复制
        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);
         }
         }
    }


    function showSuccess(btn, popover) {
        // 事件监听
         btn.innerText = '✅ 已复制开示及出处';
        document.addEventListener('mouseup', function () { setTimeout(handleSelection, 30); });
         btn.style.background = '#2e7d32';
         document.addEventListener('touchend', function () { setTimeout(handleSelection, 30); });
        setTimeout(function () {
 
             btn.innerText = '📋 复制该段开示(带出处)';
         document.addEventListener('mousedown', function (e) {
             btn.style.background = '#4d6b57';
             var pop = document.getElementById('dhamma-copy-popover');
            popover.style.display = 'none';
             if (pop && !pop.contains(e.target)) {
         }, 1200);
                pop.style.display = 'none';
            }
         });
     }
     }


     function fallbackCopy(text, btn, popover) {
     // 防脚本过早执行报错:安全等待 DOM Ready 后初始化
        var ta = document.createElement('textarea');
    if (document.readyState === 'loading') {
        ta.value = text;
         document.addEventListener('DOMContentLoaded', initDhammaCopy);
        ta.style.position = 'fixed';
    } else {
        ta.style.opacity = '0';
         initDhammaCopy();
        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';
        }
    });
})();
})();

2026年9月19日 (六) 23:21的版本

/* =========================================================
   Dhamma.cn 划词复制开示及 UrlShortener 短网址落款(纯前端 JS 版)
   适用于:MediaWiki:Common.js / MediaWiki:Timeless.js
   ========================================================= */
(function () {
    if (window.dhammaCopyLoaded) return;
    window.dhammaCopyLoaded = true;

    function initDhammaCopy() {
        var cachedShortUrl = '';

        // 统一域名修正:强制将任何子域名(如 reg.dhamma.cn)修正为规范的 https://www.dhamma.cn
        function normalizeUrl(url) {
            if (!url) return url;
            return url.replace(/:\/\/[^\/]+/, '://www.dhamma.cn');
        }

        // 1. 异步通过 POST 方式向 UrlShortener API 请求真正的 /s/ 短网址
        function getShortUrlAsync() {
            if (cachedShortUrl) return Promise.resolve(cachedShortUrl);

            // 优先在 DOM 中查找现成的 /s/ 短链接
            var sLink = document.querySelector('a[href*="/s/"]');
            if (sLink && sLink.href && sLink.href.indexOf('/s/') !== -1) {
                cachedShortUrl = normalizeUrl(sLink.href);
                return Promise.resolve(cachedShortUrl);
            }

            var tShort = document.querySelector('#t-shorturl a');
            if (tShort && tShort.href && tShort.href.indexOf('/s/') !== -1) {
                cachedShortUrl = normalizeUrl(tShort.href);
                return Promise.resolve(cachedShortUrl);
            }

            // 构建干净的页面原生 URL(彻底剥离 &oldid=12715 等历史版本参数)
            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=' + pageName;
            } else {
                targetUrl = normalizeUrl(window.location.href.split('?')[0].split('#')[0]);
            }

            // MediaWiki 规范:action=shortenurl 必须以 POST 提交
            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 = normalizeUrl(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);
        }

        // 5. 兼容性降级复制
        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';
            }
        });
    }

    // 防脚本过早执行报错:安全等待 DOM Ready 后初始化
    if (document.readyState === 'loading') {
        document.addEventListener('DOMContentLoaded', initDhammaCopy);
    } else {
        initDhammaCopy();
    }
})();