مدیاویکی:Gadget-AncreTitres.js

از ویکی‌پدیا، دانشنامهٔ آزاد

نکته: برای دیدن تغییرات، ممکن است نیاز باشد که حافظهٔ نهانی مرورگر خود را پس از انتشار پاک‌سازی کنید. گوگل کروم، فایرفاکس، مایکروسافت اج و سافاری: کلید Shift را نگه دارید و روی دکمهٔ Reload در نوار ابزار مرورگر کلیک کنید. برای آگاهی از جزئیات و نحوهٔ پاک‌سازی حافظهٔ نهانی سایر مرورگرها، صفحهٔ ویکی‌پدیا:میانگیر مرورگرتان را خالی کنید را ببینید.

/*jslint browser:true */
/*global $,  */
/*global alert, jQuery, mediaWiki, window */
// <nowiki>
(function (mw, $) {
    'use strict';
    function toClipboard(what, text) {
        // https://stackoverflow.com/a/30810322/2705757
        var textArea = document.createElement("textarea");
        // Prevent scrolling to bottom of page in MS Edge/FF
        textArea.style.position = "fixed";
        textArea.style.top = 0;
        textArea.style.left = 0;
        // Ensure it has a small width and height. Setting to 1px / 1em
        // doesn't work as this gives a negative w/h on some browsers.
        textArea.style.width = '2em';
        textArea.style.height = '2em';
        // We don't need padding, reducing the size if it does flash render.
        textArea.style.padding = 0;
        // Clean up any borders.
        textArea.style.border = 'none';
        textArea.style.outline = 'none';
        textArea.style.boxShadow = 'none';
        // Avoid flash of white box if rendered for any reason.
        textArea.style.background = 'transparent';
        textArea.value = text;
        document.body.appendChild(textArea);
        textArea.focus();
        textArea.select();
        try {
            document.execCommand('copy');
        } catch (err) {
            mw.notify('امکان انتقال به بریده‌دان نبود');
            return;
        }
        finally {
            document.body.removeChild(textArea);
        }
        mw.notify(what + ' به بریده‌دان منتقل شد');
    }
    $(function () {
        var _option = {
            nom_permalink: 'پایاپیوند',
            nom_ancre: 'نشانی وب',
            nom_lien_interne: 'ویکی‌پیوند',
            description_pl: 'فرستادن پیوند پایدار به بریده‌دان',
            description: 'انتقال نشانی وب ساده به بریده‌دان',
            descinterne: 'انتقال ویکی‌پیوند به بریده‌دان',
        };
        if (typeof window.AncreTitres !== 'undefined') {
            $.extend(_option, window.AncreTitres);
        }

        var sep = $('<span> | </span>');
        var permalink = $('#t-permalink a').length && $('#t-permalink a')[0].href;
        var urlPrefix = 'https:' + mw.config.get('wgServer') + mw.config.get('wgScript') + '?title=' + mw.config.get('wgPageName');

        $('.mw-headline').each(function (_, headline) {
            var linksSection = $('<span> <span class="mw-editsection-bracket">[</span><span class="mw-editsection-bracket">]</span></span>');
            var lastBracket = linksSection.find('.mw-editsection-bracket').last();
            linksSection.css({
                '-moz-user-select': 'none',
                '-ms-user-select': 'none',
                '-webkit-user-select': 'none',
                'user-select': 'none',
                'font-size': 'small',
                'font-weight': 'normal',
                'line-height': '1em'
            });

            if (headline.id) {
                var anchor = '#' + headline.id.replace(/\|/g, encodeURIComponent);
            } else {
                var anchor = '';
            }

            $('<a href="#" title="' + _option.description + '">' + _option.nom_ancre + '</a>').click(function () {
                toClipboard('نشانی وب معمولی', urlPrefix + anchor);
                return false;
            }).insertBefore(lastBracket);

            if (permalink) {
                sep.clone().insertBefore(lastBracket);
                $('<a href="#" title="' + _option.description_pl + '">' + _option.nom_permalink + '</a>').click(function () {
                    toClipboard('پایاپیوند', permalink + anchor);
                    return false;
                }).insertBefore(lastBracket);
            }

            sep.clone().insertBefore(lastBracket);
            $('<a href="#" title="' + _option.descinterne + '">' + _option.nom_lien_interne + '</a>').click(function () {
                toClipboard('ویکی‌پیوند', '[[' + mw.config.get('wgPageName') + anchor + ']]');
                return false;
            }).insertBefore(lastBracket);

            if (headline.nextElementSibling && headline.nextElementSibling.className === 'mw-editsection') {
                linksSection.appendTo(headline.nextElementSibling);
            } else {
                linksSection.appendTo(headline);
            }
        });
    });
}(mediaWiki, jQuery));
// </nowiki>