Greasy Fork 还支持 简体中文。

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

Useful to see previous categories that you used

您需要先安裝使用者腳本管理器擴展,如 TampermonkeyGreasemonkeyViolentmonkey 之後才能安裝該腳本。

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

您需要先安裝使用者腳本管理器擴充功能,如 TampermonkeyViolentmonkey 後才能安裝該腳本。

您需要先安裝使用者腳本管理器擴充功能,如 TampermonkeyUserscripts 後才能安裝該腳本。

你需要先安裝一款使用者腳本管理器擴展,比如 Tampermonkey,才能安裝此腳本

您需要先安裝使用者腳本管理器擴充功能後才能安裝該腳本。

(我已經安裝了使用者腳本管理器,讓我安裝!)

你需要先安裝一款使用者樣式管理器擴展,比如 Stylus,才能安裝此樣式

你需要先安裝一款使用者樣式管理器擴展,比如 Stylus,才能安裝此樣式

你需要先安裝一款使用者樣式管理器擴展,比如 Stylus,才能安裝此樣式

你需要先安裝一款使用者樣式管理器擴展後才能安裝此樣式

你需要先安裝一款使用者樣式管理器擴展後才能安裝此樣式

你需要先安裝一款使用者樣式管理器擴展後才能安裝此樣式

(我已經安裝了使用者樣式管理器,讓我安裝!)

// ==UserScript==
// @name         Wikimedia Commons upload page: near Categories input - set previous clickable categories
// @namespace    http://greasyfork.org/
// @version      1.3
// @author       Vitaly Zdanevich
// @match        https://commons.wikimedia.org/wiki/Special:UploadWizard
// @match        https://commons.wikimedia.org/w/index.php?title=Special:UploadWizard*
// @supportURL   https://gitlab.com/vitaly-zdanevich-userscripts/commonsUploadSetPrevCategories
// @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 a = document.createElement('a')
						a.id = 'prevCats'
						a.innerText = 'Previous:\n'
						a.href = '//commons.wikimedia.org/wiki/' + filename
						document.querySelector('.oo-ui-draggableGroupElement').append(a, ...cats)

						setPreviousCategories.isStarted = false
					})
			})
	}

	observer.observe(parent, config)

})()

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

	const copy = document.createElement('span')
	copy.innerText = '📋'
	copy.style = 'cursor: pointer; margin-left: 10px'

	copy.onclick = function() {
		this.remove()
		navigator.clipboard.writeText(a.innerText)
	}

	div.appendChild(a)
	div.appendChild(copy)

	acc.push(div)
	return acc
}