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

MediaWiki:Gadget-SearchNamespace.js

来自LLWiki
跳转到导航 跳转到搜索

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

  • 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),然后隐私和安全 → 清除浏览数据 → 缓存的图片和文件
//<nowiki>
// 由ResourceLoader直接调用,不可使用ES6语法
// 引自[[wikipedia:user:Writ_Keeper/Scripts/SearchNamespace.js]],经大幅修改
"use strict";
/*global mw, $, OO, wgULS*/
var searchBar;
const prefixString = {
    hans: ['(主)', '用户', '文件', '帮助', '分类', '模板', 'LLWiki', '模块', 'Widget', 'MediaWiki', '特殊'],
    hant: ['(主要)', '使用者', '檔案', '使用說明', '分類', '模板', 'LLWiki', '模組', 'Widget', 'MediaWiki', '特殊']
},
    regList = [":?", "(user|用[户戶]|使用者):", "(file|image|文件|[图圖]像|[档檔]案):", "(help|[帮幫]助|使用[说說]明):",
    "(category|分[类類]):", "(template|[模样樣]板):", "(project|llwiki):", "(module|模[块塊组組]):",
    "widget:", "mediawiki:", "(special|特殊):"].map(function(ele) { return '^' + ele; }),
    options = prefixString[wgULS('hans', 'hant')].map(function(ele, i) { return {data: i, label: ele}; }),
    widget = new OO.ui.DropdownInputWidget({classes: ['prefixList'], options: options}).on('change', function() {
    const regExpOld = new RegExp( regList[ widget.getData() ], "i" ),
        val = parseInt( widget.getValue() ),
        regExpNew = new RegExp( regList[val], "i" ),
        prefix = (val === 0 ? "" : options[val].label + ':');
    widget.setData(val);
    var searchText = searchBar.val();
    if (regExpNew.test( searchText ) && val !== 0) { return; }
    searchText = searchText.replace(regExpOld, prefix);
    searchBar.val( searchText ).data("old", searchText);
}).setValue(0).setData(0),

    changeInput = function() {
    const regExp = new RegExp( regList[ widget.getValue() ], "i" ),
        searchText = searchBar.val();
    if (regExp.test( searchText )) { searchBar.data("old", searchText); }
    else { searchBar.val( searchBar.data("old") ); }
    if (widget.getValue() != '0') { return; }
    const index = regList.map(function(ele, i) { return [ele, i]; })
        .filter(function(ele) { return RegExp(ele[0], "i").test(searchText); })
        .pop()[1];
    if (index !== 0) { widget.setValue( index ); }
},
    mobileInit = function() {
    console.log( 'setInterval: 等待搜索框加载完毕' );
    const timerStart = mw.now(),
        timer = setInterval(function() {
        if ( $(".mw-overlays-container .search-box").length === 0 ) { return; }
        clearInterval(timer);
        console.log('End setInterval: 搜索框加载完毕,用时 ' +
            (mw.now() - timerStart) + ' ms,开始添加命名空间下拉选单');
        const searchDiv = $(".mw-overlays-container .search-box").prepend( widget.$element );
        searchBar = searchDiv.children( 'input' ).data("old", '').on("input", changeInput);
        widget.setTabIndex( searchBar.prop("tabIndex") );
        searchDiv.children( 'button' ).on('click', function() {
            searchBar.data("old", "");
            widget.setValue(0).setData(0);
        });
        if (widget.getValue() != '0' && !searchBar.val()) { widget.setValue(0).setData(0); }
        changeInput();
    }, 100);
},
    init = function() {
    $("#simpleSearch").before( widget.$element );
    searchBar = $("#searchInput").data("old", '').on("input", changeInput);
    widget.setTabIndex( searchBar.prop("tabIndex") );
    changeInput();
};
    
$(function() {
    if (mw.config.get('skin') == 'vector') { init(); }
    else { $('.search-box, #searchIcon').on('click touchstart', mobileInit); }
});
//</nowiki>
// [[category:作为模块的小工具]] [[category:搜索工具]] [[category:桌面版小工具]] [[category:手机版小工具]]
// {{DEFAULTSORT:SearchNamespace.js}}