// ==UserScript== // @name Switch language button for tour.golang.org // @namespace https://github.com/ipcjs/ // @version 0.3 // @description 给页面添加一个切换中/英文的按钮 // @author ipcjs // @icon https://www.google.com/s2/favicons?domain=golang.org // @match https://tour.go-zh.org/* // @match https://go.dev/tour/* // @grant none // @downloadURL https://update.greasyfork.icu/scripts/435578/Switch%20language%20button%20for%20tourgolangorg.user.js // @updateURL https://update.greasyfork.icu/scripts/435578/Switch%20language%20button%20for%20tourgolangorg.meta.js // ==/UserScript== const hosts = [ "go.dev/tour", "tour.go-zh.org", ] function getNextUrl() { const url = location.href for (const [index, host] of hosts.entries()) { if (url.match(host)) { const nextHost = hosts[(index + 1) % hosts.length] return url.replace(host, nextHost) } } throw new Error('current url not match any host') } const $div = document.createElement('div') $div.innerHTML = ` ` $div.addEventListener('click', () => { location.href = getNextUrl() }) $div.addEventListener('contextmenu', () => { window.open(getNextUrl()) }) const $topBar = document.querySelector('.top-bar') $topBar.appendChild($div)