User:Wcam/LicenseTagReplacer.js
外观
注意:保存之后,你必须清除浏览器缓存才能看到做出的更改。Google Chrome、Firefox、Microsoft Edge及Safari:按住⇧ Shift键并单击工具栏的“刷新”按钮。参阅Help:绕过浏览器缓存以获取更多帮助。
// <nowiki>
/**
* LicenseTagReplacer.js - A Wikipedia gadget for replacing non-free copyright tags
* License: CC BY-SA 4.0
*/
(function() {
'use strict';
if (mw.config.get('wgNamespaceNumber') !== 6) {
return;
}
const copyrightTags = {
'藝術': {
'Non-free 2D art': '受著作權保護之低解析度二維作品圖像',
'Non-free 3D art': '受著作權保護之低解析度三維作品圖像',
'Non-free architectural work': '已提案但尚未完工之建築物圖像',
'Non-free destroyed architecture': '已被摧毀、拆除或改變得無法辨認的建築物的圖像'
},
'聲音與音樂': {
'Non-free audio sample': '低品質且低於30秒之Ogg Vorbis格式音訊作品',
'Non-free sheet music': '掃描或重製之樂譜圖像',
'Non-free speech': '歷史事件的口頭文字音頻剪輯'
},
'封面藝術': {
'Non-free album cover': '音樂專輯或單曲封面',
'Non-free board game cover': '桌上遊戲之封面藝術',
'Non-free book cover': '書籍封面',
'Non-free comic': '漫畫書封面或內頁',
'Comiccover': '漫畫書封面',
'Non-free video game cover': '電子遊戲封面',
'Non-free magazine cover': '雜誌封面',
'Non-free newspaper image': '整版掃描之報紙頁面',
'Non-free product cover': '一般品牌產品之封面',
'Non-free software cover': '其他軟體封面',
'Non-free video cover': '錄影帶、DVD、藍光光碟等產品之封面'
},
'標誌': {
'Non-free computer icon': '電腦軟體標誌',
'Non-free logo': '標誌',
'Non-free school logo': '学校標誌',
'Non-free Scout logo': '童軍與女童軍組織之標誌',
'Non-free Mozilla logo': 'Mozilla標誌',
'Non-free seal': '政府機構/單位之官方徽章',
'Non-free symbol': '旗幟、臂章等類似符號'
},
'宣傳材料': {
'Non-free promotional': '以宣傳某物件為目的之圖像',
'Non-free poster': '低解析度之海報圖像'
},
'宣傳照片': {
'Non-free AUSPIC': '從AUSPIC而來之澳大利亞政治人物官方圖像',
'Non-free promotional': '人群或事件之宣傳照片'
},
'螢幕截圖': {
'Non-free game screenshot': '電子遊戲螢幕截圖',
'Non-free Touhou screenshot': '东方Project游戏屏幕截图',
'Non-free software screenshot': '軟體螢幕截圖',
'Non-free Microsoft screenshot': '微軟商業發行產品之螢幕截圖',
'Non-free video screenshot': '影片之螢幕截圖',
'Non-free film screenshot': '電影截圖',
'Non-free title-card': '標誌,標題卡,或者鑑別ID',
'Non-free music video screenshot': '音樂影片或其他音樂家宣傳影片之螢幕截圖或劇照',
'Non-free television screenshot': '電視節目之螢幕截圖',
'Non-free web screenshot': '網頁截圖',
'Non-free vodcast screenshot': '影片播客之螢幕截圖',
'Wikipedia-screenshot': '維基百科頁面螢幕截圖'
},
'郵票與貨幣': {
'Non-free currency': '可能受著作權保護之國家官方貨幣圖像',
'Non-free stamp': '可能受著作權保護之郵票圖像',
'Non-free stamp of Canada': '1958年及其之後發行之加拿大郵票',
'Non-free stamp of India': '1948年及其之後發行之印度郵票',
'Non-free USGov-USPS stamp': '1978年及其之後發行之美國郵票'
},
'英國政府': {
'Non-free Crown copyright': '受英國皇家著作權保護之材料',
'Non-free Parliamentary copyright': '英國國會著作權保護之材料'
},
'其他': {
'Non-free biog-pic': '人物或團體之辨識用途的圖像',
'Non-free character': '漫畫書、動畫電視影集或電影角色之描述性藝術作品',
'Non-free Denver Public Library image': '丹佛公共圖書館收集之照片',
'Non-free Otto Perry image': '丹佛公共圖書館收集之奧托·佩利收藏照片',
'Non-free ESA media': '歐洲太空總署製作之圖像',
'Non-free historic image': '獨特且知名之歷史圖像',
'Non-free Olympics media': '奧林匹克相關之圖像',
'Non-free fair use in': '其他非自由圖像'
}
};
// 版权标签重定向映射
const templateRedirects = {
'Promotional': 'Non-free promotional',
'Non-free-poster': 'Non-free poster',
'Game-screenshot': 'Non-free game screenshot',
'Software-screenshot': 'Non-free software screenshot',
'Film-screenshot': 'Non-free film screenshot',
'TV-screenshot': 'Non-free television screenshot',
'Record cover': 'Non-free album cover',
'Albumcover': 'Non-free album cover',
'Video game cover': 'Non-free video game cover',
'Bookcover': 'Non-free book cover',
'Magazine-cover': 'Non-free magazine cover',
'Video-cover': 'Non-free video cover',
'Logo': 'Non-free logo',
'Fair-logo': 'Non-free logo',
'fairuse': 'Non-free fair use in',
'Fair use in': 'Non-free fair use in',
'Non-free fair use': 'Non-free fair use in'
};
$(mw.util.addPortletLink(
'p-cactions',
'#',
'更換版權標籤',
'ca-copyright-replacer',
'替換非自由文件的版權標籤'
)).click(function(e) {
e.preventDefault();
showDialog();
});
function showDialog() {
const $dialog = $('<div>')
.attr('title', '替換版權標籤')
.append(createDropdownUI());
$dialog.dialog({
width: 550,
modal: true,
buttons: {
'取消': function() {
$(this).dialog('close');
},
'替換': function() {
replaceTag($(this));
$(this).dialog('close');
}
}
});
}
function createDropdownUI() {
const $container = $('<div>');
const $tagSelect = $('<select>')
.attr('id', 'copyright-tag')
.append($('<option>').text('請選擇標籤...'));
Object.entries(copyrightTags).forEach(([category, tags]) => {
$tagSelect.append($('<option>').text(`➤ ${category}`).attr('disabled', 'disabled'));
Object.entries(tags).forEach(([tag, desc]) => {
$tagSelect.append($('<option>').val(tag).text(` • ${tag} - ${desc}`));
});
});
$container
.append($('<p>').text('選擇要替換的標籤:'))
.append($tagSelect);
return $container;
}
function replaceTag($dialog) {
let newTag = $dialog.find('#copyright-tag').val();
if (!newTag || newTag === '請選擇標籤...') {
mw.notify('請選擇一個有效的標籤', { type: 'warn' });
return;
}
// 确保新标签是标准名称,而不是重定向
newTag = templateRedirects[newTag] || newTag;
new mw.Api().get({
action: 'query',
prop: 'revisions',
rvprop: 'content',
rvslots: 'main',
titles: mw.config.get('wgPageName')
}).done(function(data) {
const page = Object.values(data.query.pages)[0];
if (!page.revisions) {
mw.notify('無法獲取頁面內容', { type: 'error' });
return;
}
let content = page.revisions[0].slots.main['*'];
// 获取当前的版权标签,并检查是否需要重定向
const regex = /\{\{\s*([^}|]+)[^}]*\}\}/gi;
let match;
let newContent = content;
// 检查旧标签是否在版权标签列表中
const allTags = Object.values(copyrightTags)
.flatMap(tags => Object.keys(tags))
.concat(Object.keys(templateRedirects));
while ((match = regex.exec(content)) !== null) {
let oldTag = match[1].trim();
let standardOldTag = templateRedirects[oldTag] || oldTag;
if (allTags.some(tag => tag.toLowerCase() === oldTag.toLowerCase()) || allTags.some(tag => tag.toLowerCase() === standardOldTag.toLowerCase())) {
newContent = newContent.replace(
new RegExp(`\\{\\{\\s*(${oldTag}|${standardOldTag})\\s*[^}]*\\}\\}`, 'i'),
`{{${newTag}}}`
);
}
}
if (newContent !== content) {
new mw.Api().postWithToken('csrf', {
action: 'edit',
title: mw.config.get('wgPageName'),
text: newContent,
summary: `替換非自由版權標籤: ${newTag}`,
minor: true
}).done(function() {
mw.notify('版權標籤已成功更新');
location.reload();
}).fail(function() {
mw.notify('更新失敗,請重試', { type: 'error' });
});
} else {
mw.notify('未找到可替換的版權標籤', { type: 'warn' });
}
}).fail(function() {
mw.notify('無法讀取頁面數據', { type: 'error' });
});
}
})();