Wikimedia Commons upload page: near Categories input - set previous clickable categories

Useful to see previous categories that you used

当前为 2024-06-11 提交的版本,查看 最新版本

您需要先安装一个扩展,例如 篡改猴Greasemonkey暴力猴,之后才能安装此脚本。

You will need to install an extension such as Tampermonkey to install this script.

您需要先安装一个扩展,例如 篡改猴暴力猴,之后才能安装此脚本。

您需要先安装一个扩展,例如 篡改猴Userscripts ,之后才能安装此脚本。

您需要先安装一款用户脚本管理器扩展,例如 Tampermonkey,才能安装此脚本。

您需要先安装用户脚本管理器扩展后才能安装此脚本。

(我已经安装了用户脚本管理器,让我安装!)

您需要先安装一款用户样式管理器扩展,比如 Stylus,才能安装此样式。

您需要先安装一款用户样式管理器扩展,比如 Stylus,才能安装此样式。

您需要先安装一款用户样式管理器扩展,比如 Stylus,才能安装此样式。

您需要先安装一款用户样式管理器扩展后才能安装此样式。

您需要先安装一款用户样式管理器扩展后才能安装此样式。

您需要先安装一款用户样式管理器扩展后才能安装此样式。

(我已经安装了用户样式管理器,让我安装!)

// ==UserScript==
// @name         Wikimedia Commons upload page: near Categories input - set previous clickable categories
// @namespace    http://greasyfork.org/
// @version      1.0
// @author       Vitaly Zdanevich
// @match        https://commons.wikimedia.org/wiki/Special:UploadWizard
// @grant        none
// @description  Useful to see previous categories that you used
// @license MIT
// ==/UserScript==

// TODO publish to Commons userscript (userscript of another type)

(function() {
	const parent = document.getElementById('upload-wizard')
	const config = { attributes: true, childList: false, subtree: true }
	const observer = new MutationObserver(_ => {

	if (!setPreviousCategories.isStarted && document.querySelector('.oo-ui-draggableGroupElement') && !document.querySelector('#prevCats'))
			setPreviousCategories()
	})

	function setPreviousCategories() {
		setPreviousCategories.isStarted = true

		// https://www.mediawiki.org/wiki/API:Usercontribs
		fetch(`https://commons.wikimedia.org/w/api.php?action=query&list=usercontribs&uclimit=1&ucuser=${mw.user.getName()}&format=json`)
			.then(resp => resp.json())
			.then(j => {
				const filename = j['query']['usercontribs'][0]['title']
				// https://www.mediawiki.org/w/api.php?action=help&modules=query:categories
				fetch(`https://commons.wikimedia.org/w/api.php?action=query&format=json&prop=categories&meta=&titles=${filename}&formatversion=2&clshow=!hidden`)
					.then(resp => resp.json())
					.then(j => {
						const cats = j['query']['pages'][0]['categories']
							.reduce(reducer, [])
						const div = document.createElement('div')
						div.id = 'prevCats'
						const a = document.createElement('a')
						a.innerText = 'Previous:\n'
						a.href = '//commons.wikimedia.org/wiki/' + filename
						document.querySelector('.oo-ui-draggableGroupElement').append(a, ...cats)
					})
			})
	}

	observer.observe(parent, config)

})()

function reducer(acc, cur) {
	const a = document.createElement('a')
	a.innerText = cur['title'].replace('Category:', '')
	a.href = '//commons.wikimedia.org/wiki/' + cur['title']
	a.style = 'display: block'
	acc.push(a)
	return acc
}