User:Bosco-bot/common.js
From Test Wiki
Note: After publishing, you may have to bypass your browser's cache to see the changes.
- Firefox / Safari: Hold Shift while clicking Reload, or press either Ctrl-F5 or Ctrl-R (⌘-R on a Mac)
- Google Chrome: Press Ctrl-Shift-R (⌘-Shift-R on a Mac)
- Edge: Hold Ctrl while clicking Refresh, or press Ctrl-F5.
/**
* Protection helper (zh-Hans / zh-Hant)
* - Handles protect and unprotect
* - Default language zh-Hans
* - Always ask for expiry (default indefinite)
* - Split edit/move protection levels
* - Namespace restrictions
* - Direct protect URL construction
*/
mw.loader.using(['mediawiki.util'], function () {
var ns = mw.config.get('wgNamespaceNumber');
if (ns === -1 || ns === -2) return;
// Language detection (default zh-Hans)
var rawLang = mw.config.get('wgContentLanguage') || 'zh-hans';
var lang = (['zh', 'zh-hans', 'zh-cn', 'zh-my', 'zh-sg'].includes(rawLang)) ? 'zh-hans' : 'zh-hant';
// Reasons dictionary
var reasonsHans = {
G1: "遭到过度破坏的页面",
G2: "拥有过多垃圾信息的页面",
G3: "拥有负面的编辑战的页面",
G4: "高流量页面",
G5: "当时有争议和谣言风靡的页面",
G6: "备受争议的页面",
G7: "同一时间或不同时间针对性被破坏的页面",
G8: "动画中的宝可梦被公布可能会进化",
R2: "译名备受争议但并无错误的重定向",
R3: "需要保护的重定向",
R4: "刻意双重重定向或者指向不存在页面的重定向",
F1: "有必要保护的文件",
F2: "刻意拥有重复文件",
F3: "备受争议的文件",
O1: "用户请求保护自己的用户页或子页面",
O2: "神奇宝贝百科站务或方针方面的页面",
O3: "刻意使用的空的类别(没有条目也没有子类别)"
};
var reasonsHant = {
G1: "遭到過度破壞的頁面",
G2: "擁有過多垃圾資訊的頁面",
G3: "擁有負面的編輯戰的頁面",
G4: "高流量頁面",
G5: "當時有爭議和謠言風靡的頁面",
G6: "備受爭議的頁面",
G7: "同一時間或不同時間針對性被破壞的頁面",
G8: "動畫中的寶可夢被公布可能會進化",
R2: "譯名備受爭議但並無錯誤的重定向",
R3: "需要保護的重定向",
R4: "刻意雙重重定向或者指向不存在頁面的重定向",
F1: "有必要保護的檔案",
F2: "刻意擁有重複檔案",
F3: "備受爭議的檔案",
O1: "用戶請求保護自己的用戶頁或子頁面",
O2: "神奇寶貝百科站務或方針方面的頁面",
O3: "刻意使用的空的類別(沒有條目也沒有子類別)"
};
var reasonsAll = (lang === 'zh-hans') ? reasonsHans : reasonsHant;
// Filter reasons by namespace
function getValidReasons() {
var valid = {};
Object.keys(reasonsAll).forEach(function (code) {
if (['F1','F2','F3'].includes(code) && ns !== 6) return;
if (code === 'O1' && !(ns === 2 || ns === 3)) return;
if (code === 'O2' && !(ns === 4 || ns === 5)) return;
if (code === 'O3' && ns !== 14) return;
valid[code] = reasonsAll[code];
});
return valid;
}
var linkText = (lang === 'zh-hans') ? '保护/解除保护' : '保護/解除保護';
var tooltip = linkText;
var reasonPrompt = (lang === 'zh-hans') ? '请输入保护/解除保护理由:' : '請輸入保護/解除保護理由:';
var editPrompt = (lang === 'zh-hans')
? "请输入编辑保护级别 (autoconfirmed/sysop/admin/none):"
: "請輸入編輯保護級別 (autoconfirmed/sysop/admin/none):";
var movePrompt = (lang === 'zh-hans')
? "请输入移动保护级别 (autoconfirmed/sysop/admin/none):"
: "請輸入移動保護級別 (autoconfirmed/sysop/admin/none):";
var expiryPrompt = (lang === 'zh-hans') ? "请输入保护期限 (默认 indefinite):" : "請輸入保護期限 (默認 indefinite):";
var link = mw.util.addPortletLink('p-cactions', '#', linkText, 'ca-protectpage', tooltip);
$(link).on('click', function (e) {
e.preventDefault();
var editChoice = prompt(editPrompt);
var moveChoice = prompt(movePrompt);
function normalizeLevel(input) {
if (!input) return "";
var val = input.trim().toLowerCase();
if (val === "autoconfirmed") return "autoconfirmed";
if (val === "sysop" || val === "admin") return "sysop";
if (val === "none") return "";
return "";
}
var editLevel = normalizeLevel(editChoice);
var moveLevel = normalizeLevel(moveChoice);
var protections = [];
if (editLevel) protections.push("mwProtect-level-edit=" + editLevel);
if (moveLevel) protections.push("mwProtect-level-move=" + moveLevel);
var expiry = prompt(expiryPrompt) || "indefinite";
var reason = prompt(reasonPrompt) || "";
var pageName = mw.config.get('wgPageName');
var baseUrl = mw.util.getUrl(pageName);
// Protect mode: require code
var finalReason = reason;
if (protections.length > 0) {
var validReasons = getValidReasons();
var codePrompt = (lang === 'zh-hans' ? "请输入保护代碼:\n" : "請輸入保護代碼:\n") +
Object.keys(validReasons).map(function (k) {
return k + ":" + validReasons[k];
}).join("\n");
var code = prompt(codePrompt);
if (!code || !validReasons[code]) {
alert((lang === 'zh-hans') ? "无效代碼,请重新输入。" : "無效代碼,請重新輸入。");
return;
}
// O1 requires reason
if (code === "O1" && !reason) {
alert((lang === 'zh-hans') ? "O1 必须提供理由。" : "O1 必須提供理由。");
return;
}
finalReason = code + ":" + validReasons[code] + (reason ? ";" + reason : "");
}
var finalUrl = baseUrl + "?action=protect" +
(protections.length ? "&" + protections.join("&") : "") +
"&mwProtect-reason=" + encodeURIComponent(finalReason) +
"&mwProtect-expiry=" + encodeURIComponent(expiry);
window.location.href = finalUrl;
});
});