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

MediaWiki:Gadget-translation.js

来自LLWiki
Bhsd讨论 | 贡献2020年11月29日 (日) 04:39的版本 (创建页面,内容为“"use strict"; const dict = [{reg:'南小[鸟鳥]', hans:'南琴梨'}, {reg:'妮可妮可妮', hans:'微笑小香香'}, {reg:'妮可', hans:'日香'}], reg = new…”)
(差异) ←上一版本 | 最后版本 (差异) | 下一版本→ (差异)
跳转到导航 跳转到搜索

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

  • 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),然后隐私和安全 → 清除浏览数据 → 缓存的图片和文件
"use strict";
const dict = [{reg:'南小[鸟鳥]', hans:'南琴梨'}, {reg:'妮可妮可妮', hans:'微笑小香香'}, {reg:'妮可', hans:'日香'}],
    reg = new RegExp( dict.map(function(ele) { return ele.reg; }).join('|') ),
    replace = function(string) {
    dict.forEach(function(ele) {
        string = string.replace(new RegExp(dict.reg, 'g'), wgUCS(dict.hans, dict.hant || dict.hans));
    });
    return string;
},
    searchReplace = function(ele) {
    const text = ele.innerHTML || ele.textContent;
    if (ele.title && reg.test(ele.title)) { ele.title = replace(ele.title); }
    if (!reg.test(text)) { return; }
    if (ele.nodeType == 3) { ele.textContent = replace(text); }
    else { $(ele).contents().each(function() { searchReplace(this); }); }
};
mw.hook( 'wikipage.content' ).add(function($content) {
    $content.find( '.mw-parser-output' ).filter(function() { return reg.test(this.innerHTML); })
        .each(function() {
        console.log('Hook: wikipage.content,开始替换译名');
        searchReplace(this);
    });
});
$(function() {
    const heading = $('h1');
    if (reg.test( heading.text() )) { searchReplace(heading[0]); }
});