Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save ngseke/88a73338ad15e2c79a7f545f792ef81d to your computer and use it in GitHub Desktop.
Save ngseke/88a73338ad15e2c79a7f545f792ef81d to your computer and use it in GitHub Desktop.
Wikipedia Title with Chinese Translation
// ==UserScript==
// @name Wikipedia Title with Chinese Translation
// @namespace http://tampermonkey.net/
// @version 0.1
// @description Wikipedia Title with Chinese Translation
// @author ngseke
// @match https://en.wikipedia.org/wiki/*
// @icon https://www.google.com/s2/favicons?sz=64&domain=wikipedia.org
// @grant none
// ==/UserScript==
(async () => {
const rawDocument = new DOMParser()
.parseFromString(document.documentElement.outerHTML, 'text/html')
const $ = (...args) => rawDocument.querySelector(...args)
const getChinesePage = () => {
const element = $('.interlanguage-link-target[lang=zh]')
if (!element) return null
const { title, href } = element
const name = title.replace('– Chinese', '').trim()
return { name, href }
}
const renderChineseElement = (content, link) => {
const a = document.createElement('a')
a.innerText = content
a.href = link
Object.assign(a.style, {
borderRadius: '8px',
fontSize: '14px',
marginLeft: '6px',
padding: '3px 6px',
opacity: 0.9,
fontWeight: 600,
fontFamily: 'sans-serif',
color: '#1E1E1E',
background: '#EBEBEB',
})
return a
}
const appendElementAfterHeading = (element) => {
const heading = document.querySelector('#firstHeading')
heading.appendChild(element)
}
const page = getChinesePage()
if (page) {
const element = renderChineseElement(page.name, page.href)
appendElementAfterHeading(element)
}
})()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment