Actions

MediaWiki

MediaWiki:Common.js

From KAYŌ 💖 DAY

Revision as of 18:24, 18 October 2024 by Dachy (talk | contribs)

Note: After publishing, you may have to bypass your browser's cache to see the changes.

  • Firefox / Safari: Hold Shift while clicking Reload, or press either Ctrl-F5 or Ctrl-R (⌘-R on a Mac)
  • Google Chrome: Press Ctrl-Shift-R (⌘-Shift-R on a Mac)
  • Internet Explorer / Edge: Hold Ctrl while clicking Refresh, or press Ctrl-F5
  • Opera: Press Ctrl-F5.
/* Any JavaScript here will be loaded for all users on every page load. */

$(document).ready(function() {

    // 计算运行天数
    var now = new Date().getTime()
    var then = new Date('2007-07-27 22:19').getTime()
    var runningDays = Math.ceil((now - then) / 1000 / 86400)
    var runningYears = (runningDays / 365).toFixed(1)
    $('#running-days').text(runningDays)
    $('#running-years').text(runningYears)
    if (runningDays && runningYears) {
        $('#running').show().css('display', 'block')
    }

    // 引用播放器代码
    // $('head').append($("<script>").attr({
    //    type: 'text/javascript',
    //     src: 'https://cdnjs.cloudflare.com/ajax/libs/mediaelement/5.0.5/mediaelement-and-player.min.js'
    // }))

    // 导航加随机背景图
    var pipingUrl
    if ($('.mw-default-size a').length > 0) {
        var selectedImage = $('.mw-default-size a')[Math.ceil(Math.random() * $('.mw-default-size a').length) - 1].href
        $.get(selectedImage, function (data) {
            pipingUrl = $(data).find('.fullImageLink a').attr('href')
            // if ($('.piping').length === 0) {
            //     $('#page-content').before('<div class="piping"></div>')
            //     $('footer').after('<div class="piping"></div>')
            // }
            $('.top-bar').css({
                // 'height': $('.top-bar').height(),
                'background': 'linear-gradient(to right, #000 20%, transparent 50%), url(' + pipingUrl + ') 100% center / 80% auto no-repeat',
            })
        })
    };

    // 删除 tagline
    $('#tagline').remove()
    $('#siteSub').remove()

    // 厂牌如果为空,删除十字架符号
    $('.record-label').each(function () {
        if ($(this).text() === '†') {
            $(this).remove()
        }
    });

    // 清除“监视本页”“移动图像”选中状态  
    if (window.location.href.toLowerCase().indexOf('edit') !== -1 || window.location.href.toLowerCase().indexOf('special') !== -1) {  
        // 检查 #wpWatchthis 元素是否存在并设置其 checked 属性  
        $('#wpWatchthis').prop('checked', false);  
  
        // 检查 #wpIgnoreWarning 元素是否存在并设置其 checked 属性  
        $('#wpIgnoreWarning').prop('checked', true);  
  
        // 检查 name 为 wpLeaveRedirect 的元素是否存在并设置其 checked 属性  
        $('[name=wpLeaveRedirect]').prop('checked', false);  
  
        // 确保所有 name 为 wpLeaveRedirect 的元素都被选中  
        $('[name=wpLeaveRedirect]').each(function() {  
            $(this).prop('checked', false);  
        });  
    }

    // 插入亚马逊链接

    // 修改链接的 href 属性
    $('.amz-link a').each(function() {
        var $this = $(this);
        var href = $this.attr('href');

        if (href) {
            // 找到当前链接对应的 dfn 标签
            var $dfn = $this.closest('.amz-link').find('dfn');
            if ($dfn.length > 0) {
                // 移除 dfn 标签内的 small 标签
                $dfn.find('small').remove();

                // 获取 dfn 标签内的文本
                var dfnText = '';
                if ($dfn.find('a').length > 0) {
                    // 如果 dfn 标签内有  标签,获取  标签内的文本
                    dfnText = $dfn.find('a').text().trim();
                } else {
                    // 否则,直接获取 dfn 标签内的文本
                    dfnText = $dfn.text().trim();
                }

console.log(dfnText)

                if (dfnText) {
                    // URL编码并替换空格为+
                    var encodedDfnText = encodeURIComponent(dfnText).replace(/%20/g, '+') || 'default';

                    // 使用正则表达式匹配 k= 后面的内容,并在其后追加新的关键词
                    var newHref = href.replace(/(k=)([^&]*)/, function(match, p1, p2) {
                        return p1 + p2 + '+' + encodedDfnText;
                    });

                    // 更新链接的 href 属性
                    $this.attr('href', newHref);

                    // 删除 dfn 标签
                    $dfn.remove();
                }
            }
        }
    });

    // 播放器
    $('.btn-play-audio:not([data-file="none"])').addClass('show')
    $('*[data-file="none"]').remove()

    var playerCode = '<div id="player"><p class="track-title">&nbsp;</p></div>'
    $('body').append(playerCode)

    if ($.fn.mediaelementplayer) {
        $('video, audio').mediaelementplayer({
            pluginPath: 'https://cdnjs.com/libraries/mediaelement/',
            shimScriptAccess: 'always',
        })
    }

    $('.btn-play-audio').remove()

    $('.btn-play-audio').on('click', function () {
        if ($.fn.mediaelementplayer) {
            $('video, audio').mediaelementplayer({
                pluginPath: 'https://cdnjs.com/libraries/mediaelement/',
                shimScriptAccess: 'always',
            })
        }
        var url = '/audio/' + $(this).attr('data-file') + '.mp3'
        var title = $(this).attr('data-file').split('-')[$(this).attr('data-file').split('-').length - 1]
        $('.btn-play-audio').removeClass('playing')
        $(this).addClass('playing')
        $('#player').addClass('show')
        $('#player .track-title').text(title)
        $('#player audio').attr('src', url)
        $('#player audio')[0].volume = 0.6
        $('#player audio')[0].play()
        // if ($('#player audio')[0].paused) {
        //     $('.btn-play-audio').removeClass('playing')
        // } else {
        //     $('.btn-play-audio').removeClass('playing')
        // }
    })
    
}())