全站通知:
用户:123855714/CustomizeToolbarButtons.js
刷
历
编
跳到导航
跳到搜索
注意:在发布之后,您可能需要清除浏览器缓存才能看到所作出的变更的影响。
- Firefox或Safari:按住Shift的同时单击刷新,或按Ctrl-F5或Ctrl-R(Mac为⌘-R)
- Google Chrome:按Ctrl-Shift-R(Mac为⌘-Shift-R)
- Internet Explorer或Edge:按住Ctrl的同时单击刷新,或按Ctrl-F5
- Opera:按 Ctrl-F5。
/*
** CustomizeToolbarButtons
** Author:马小萌
** Bilibili:https://space.bilibili.com/123855714
**/
"use strict";
var ctb = {
about: {
ver: "v2.0.2 beta",
info: function(){
console.log("%c BWIKI快速插入模板按钮 %c%s\nhttps://wiki.biligame.com/wiki/?curid=2496", "color: #fff; padding: 5px 0; background: #cc66cc;", "padding: 5px 5px 5px 0; background: #ffe5ff;", ctb.about.ver);
}
},
data: {
pages: [
// 常用格式
{
"id": "format",
"label": "常用格式",
"templates": [
{
"display": "空格",
"title": "插入转义的空格符",
"wikitext": " "
},
{
"display": "< >",
"title": "插入转义的尖括号",
"wikitext": "<$1>"
},
{
"display": "[ ]",
"title": "插入转义的方括号",
"wikitext": "[$1]"
},
{
"display": "换行",
"title": "<br>:插入换行符",
"wikitext": "<br>"
},
{
"display": "注释",
"title": "<!-->:插入注释",
"wikitext": "<!-- $1 -->"
},
{
"display": "删除线",
"wikitext": "<s>$1</s>"
},
{
"display": "下划线",
"wikitext": "<u>$1</u>"
},
{
"display": "红色文本",
"title": "<span>:带颜色的文本",
"wikitext": "<span style=\"color:red;\">$1</span>"
}
]
},
// 页面编辑
{
"id": "edit",
"label": "页面编辑",
"templates": [
{
"display": "页面链接",
"title": "[[xxx]]:指向某个页面的超链接",
"wikitext": "[[$1|显示文字]]",
"$1": "页面名称"
},
{
"display": "外部链接",
"title": "[xxx]:指向外部网页的超链接",
"wikitext": "[$1 显示文字]",
"$1": "//www.bilibili.com/video/av170001"
},
{
"display": "参考来源",
"title": "<ref>:为一段文字增加参考链接",
"wikitext": "<ref>$1</ref>",
"$1": "参考链接"
},
{
"display": "图片",
"title": "[[文件]]:插入一张图片",
"wikitext": "[[文件:$1|link=]]",
"$1": "文件名"
},
{
"display": "分类",
"title": "为页面添加分类",
"wikitext": "[[分类:$1]]",
"$1": "分类名称"
},
{
"display": "重定向",
"title": "置于页面顶端,将页面重定向到另一个页面",
"wikitext": "#重定向 [[$1]]",
"$1": "页面名称"
},
{
"display": "<div> 标签",
"wikitext": "<div>$1</div>",
"$1": "内容"
},
{
"display": "表格(HTML)",
"title": "<table class=\"wikitable\"><tr><td>内容</td></tr></table>",
"wikitext": "<table class=\"wikitable\"><tr><td>$1</td></tr></table>",
"$1": "内容"
},
{
"display": "表格(WIKI)",
"title": "{| class=\"wikitable\"\n!标题\n|-\n|内容\n|}",
"wikitext": "{| class=\"wikitable\"\n!标题\n|-\n|$1\n|}",
"$1": "内容"
}
]
},
// 常用模板
{
"id": "template",
"label": "常用模板",
"templates": []
}
],
url: "/tools/api.php",
},
generatePagesData: function(db) {
//将标签设定转换为wikiEditor定义的结构
var pages_data = {};
for (var i in db) {
var x = db[i];
var characters = [];
for (var j in x.templates) {
var t = x.templates[j];
var m = (t.wikitext || '').match(/^([\s\S]*)\$1([\s\S]*)$/);
characters[j] = {
label : t.display,
action: {
type: 'encapsulate',
options: {
pre: m && m[1] || t.wikitext || '',
peri: t['$1'] || '',
post: m && m[2] || ''
}
}
};
}
pages_data[x.id] = {
label: x.label.concat("(",x.templates.length,")"),
layout: 'characters',
characters: characters
};
}
return pages_data
},
init: function() {
ctb.data.url = mw.config.get("wgScript").replace("index","api");
ctb.load();
ctb.about.info();
},
load: function() {
if($.inArray(mw.config.get("wgAction"), ["edit", "submit"]) === -1) return;
if(mw.config.get("wgUserGroups").indexOf("sysop") != -1){
ctb.getdata();
}
},
postAddToolbarPages: function(db) {
//为标签添加工具提示
for (var i in db) {
var id = db[i].id;
var templates = db[i].templates;
$('.section > .pages > .page[rel="'+id+'"] > div > span').each(function(i) {
templates[i] && templates[i].title && $(this).attr('title', templates[i].title);
})
}
},
refreshToolbar: function() {
//刷新工具列表
$.ajax({
url:ctb.data.url,
type:"post",
data:{
"action":"query",
"list":"categorymembers",
"cmtitle":"分类:模板",
"cmnamespace":"10",
"cmtype":"page",
"cmlimit":"max",
"format":"json",
"formatversion":2
},
}).then(async function(result){
for(var category of result.query.categorymembers){
await ctb.getdocname(category.title).then((result)=>{
if(result[1][0]){
ctb.getdoc(result[1][0]).then((result)=>{
try{
ctb.push(`\n\n${result.parse.text.match(/<pre>([\s\S]+?)<\/pre>/)[1]}\n\n详细格式请查看:“${result.parse.title.split("/")[0]}”`,result.parse.title.split("/")[0]);
}catch{
ctb.push("未知",result.parse.title.split("/")[0]);
}
},(e)=>{
ctb.push("未知",result.parse.title.split("/")[0]);
console.error("opensearch:",e);
});
}else{
ctb.push("未知",result[0].split("/")[0]);
}
});
};
ctb.token("/data/api.php").then(function(csrftoken){
ctb.setdata(csrftoken);
});
});
},
push: function(f,title) {
ctb.data.pages[2].templates.push({
display: title.replace(/^模板:(.+)$/,"{{$1}}"),
title: `格式:${f}`,
wikitext: title.replace(/^模板:(.+)$/,"{{$1}}")
});
},
getdocname: async function(title) {
return await new Promise((resolve) => {
$.ajax({
url:ctb.data.url,
type:"post",
data:{
"action":"opensearch",
"search":title+"/",
"namespace":"10",
"format":"json",
"formatversion":2
}
}).then((result)=>{
resolve(result);
},function(e){
resolve(e);
});
});
},
getdoc: async function(title) {
return await new Promise((resolve) => {
$.ajax({
url:ctb.data.url,
type:"post",
data:{
"action":"parse",
"page":title,
"prop":"text",
"format":"json",
"formatversion":2
}
}).then((result)=>{
resolve(result);
},function(e){
resolve(e);
});
});
},
getdata: function() {
$.ajax({
url:`/data/User_talk:GLOBAL/CTB${mw.config.get("wgScriptPath")}.json`,
type:"post",
data:{
"action":"raw",
"_":Math.random()
}
}).then((result)=>{
try{
var tempdb = JSON.parse(result);
var nts=new Date().getTime()/1000;
if(tempdb.timestamp+3600<nts){
ctb.refreshToolbar();
}else{
ctb.data.pages[2].templates = tempdb.templates;
}
}catch{
ctb.refreshToolbar();
}
$("#wpTextbox1").wikiEditor("removeFromToolbar", {
"section": "insertion",
"page": "template"
});
$("#wpTextbox1").wikiEditor("addToToolbar", {
"section": "insertion",
"pages": ctb.generatePagesData(ctb.data.pages)
});
ctb.postAddToolbarPages(ctb.data.pages);
},(e)=>{
ctb.refreshToolbar();
// console.error("getdata:",e);
});
},
setdata: function(csrftoken) {
var tempdb = {};
tempdb.timestamp = parseInt(new Date().getTime()/1000);
tempdb.templates = ctb.data.pages[2].templates;
$.ajax({
url:"/data/api.php",
type:"post",
data:{
"action":"edit",
"summary":"CTB缓存",
"watchlist":"unwatch",
"text":JSON.stringify(tempdb),
"title":`User_talk:GLOBAL/CTB${mw.config.get("wgScriptPath")}.json`,
"token":csrftoken,
"format":"json"
}
}).then((result)=>{
//console.log("CustomizeToolbarButtons: setdata ",result.edit.result);
},(e)=>{
console.error("setdata:",e);
});
},
token: async function(url){
return await new Promise((resolve) => {
$.ajax({
url:url,
type:"post",
data:{
"action":"query",
"format":"json",
"meta":"tokens"
}
}).then((result)=>{
resolve(result.query.tokens.csrftoken);
},(e)=>{
console.error("getToken:",e);
});
});
}
}
setTimeout(function() {
ctb.init();
}, 3000);