Last active
June 10, 2023 07:06
-
-
Save ngseke/88a73338ad15e2c79a7f545f792ef81d to your computer and use it in GitHub Desktop.
Wikipedia Title with Chinese Translation
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// ==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