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

User:Bhsd/tool.js

来自LLWiki
< User:Bhsd
Bhsd讨论 | 贡献2020年12月30日 (三) 04:37的版本
跳转到导航 跳转到搜索

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

  • 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, $*/
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); }
    }); });
}) ();