LLWiki正在建设中,欢迎加入我们

User:Bhsd/tool.js

来自LLWiki
< User:Bhsd
Bhsd讨论 | 贡献2021年2月24日 (三) 01:47的版本
跳转到导航 跳转到搜索

注意:在保存之后,您可能需要清除浏览器缓存才能看到所作出的变更的影响。

  • Firefox或Safari:按住Shift的同时单击刷新,或按Ctrl-F5Ctrl-R(Mac为⌘-R
  • Google Chrome:Ctrl-Shift-R(Mac为⌘-Shift-R
  • Internet Explorer:按住Ctrl的同时单击刷新,或按Ctrl-F5
  • Opera:前往菜单 → 设置(Mac为Opera → Preferences),然后隐私和安全 → 清除浏览数据 → 缓存的图片和文件
// 由[[Special:Mypage/common.js]]调用,可以使用ES6语法
// 生成JS文件大小
"use strict";
/*global mw, $*/
$('.fa-play').click(function() {
    const audio = $(this).toggleClass('fa-play fa-pause').prev().children( 'audio' )[0];
    audio[audio.paused ? 'play' : 'pause']();
});
let audio;
const menu = mw.menu([ {text: '音量上调', icon: 'volume-up', data: 1, click: () => {
    audio.volume += 0.1;
    setDisabled();
}}, {text: '音量下调', icon: 'volume-down', data: 2, click: () => {
    audio.volume -= 0.1;
    setDisabled();
}}, {text: '下载', icon: 'download', data: 3}
], {id: 'listen-menu'}),
    volumeup = menu.findItemFromData( 1 ),
    volumedown = menu.findItemFromData( 2 ),
    setDisabled = () => {
    volumeup.setDisabled( audio.volume == 1 );
    volumedown.setDisabled( audio.volume === 0 );
    volumedown.$label.children( 'i' ).toggleClass('fa-volume-down', audio.volume > 0)
        .toggleClass('fa-volume-mute', audio.volume === 0);
},
    $download = $('<a>');
menu.$element.appendTo( document.body );
menu.findItemFromData( 3 ).$label.wrap( $download );
menu.on('toggle', visible => { if (visible) { menu.unselectItem(); } });
$('.fa-ellipsis-v').click(function() {
    const $this = $(this),
        $audio = $this.parent().find( 'audio' ),
        src = $audio.children( 'source' ).attr( 'src' ),
        parts = src.split( '/' );
    audio = $audio[0];
    menu.setFloatableContainer( $this );
    $download.attr({href: src, download: parts[ parts.length - 1 ]});
    setDisabled();
});

mw.myCopyright = function(cmtitle = mw.config.get('wgTitle')) {
    mw.loader.using( 'mediawiki.api' ).then(() => {
        const api = new mw.Api();
        console.log('API request: 查询分类下无授权协议的文件');
        const timerStart = mw.now();
        api.get({action: 'query', prop: 'categories', clcategories: 'Category:原作者保留权利的文件', formatversion: 2,
            generator: 'categorymembers', gcmtitle: `Category:${cmtitle}`, gcmtype: 'file', gcmlimit: 'max'
        }).then(res => {
            console.log(`End API request: 已获得分类下无授权协议的文件,用时 ${mw.now() - timerStart} ms`);
            res.query.pages.filter(ele => !ele.categories).forEach(ele => {
                api.postWithToken('csrf', {action: 'edit', pageid: ele.pageid, minor: 1, bot: 1,
                    prependtext: '==授权协议==\n{{copyright}}\n', summary: '使用API批量添加授权协议',
                }).catch(reason => { console.error(`页面${ele.pageid}无法添加授权协议,错误原因:${reason}`); });
            });
        }, reason => { mw.notify(`无法获得对应文件。错误原因:${reason}`, {type: 'error'}); });
    });
};

mw.myJsSize = function(table) {
    const items = table.find( 'td:first-child' ),
        titles = items.children( 'a' ).toArray().map(ele => ele.title);
    mw.loader.using( 'mediawiki.api' ).then(() => {
        console.log('API request: 查询JS页面大小');
        const timerStart = mw.now();
        new mw.Api().get({ action:'query', prop:'info', titles:titles.join( '|' ), formatversion:2 }).then(data => {
            console.log(`End API request: 已获得JS页面大小,用时 ${mw.now() - timerStart} ms`);
            table.find( 'th:last-child' ).show();
            const lengths = Object.fromEntries( data.query.pages.map(ele => [ele.title, ele.length]) );
            items.parent().append(i => $('<td>', {class: 'video-link',
                text: (lengths[ titles[i] ] / 1024).toFixed(1)}).css('text-align', 'right'));
        }, reason => {
            mw.notify(`无法获得JS页面大小。错误原因:${reason}`, {type: 'error'});
            $('#myJsSize').off( 'click' ).one('click', function() { mw.myJsSize( $(this).parent().next() ); });
        });
    });
};

(function() {
    $('#myJsSize').one('click', function() { mw.myJsSize( $(this).parent().next() ); });
    const start = mw.now();
    ['postEdit', 'wikipage.content', 'wikipage.collapsibleContent', 'wikipage.categories', 'wikipage.diff',
        'wikipage.editform', 'structuredChangeFilters.ui.initialized', 'codeEditor.configure', // MW原生Hook
        'code.prettify', 'wikiplus.dialog', 'transclusion.preview' // 小工具Hook
    ].forEach(ele => { mw.hook(ele).add(x => {
        console.log(`Hook: ${ele} after ${mw.now() - start} ms`);
        if (x) { console.info(x); }
    }); });
}) ();