Sidebar toggle, Ru/En lang buttons for wikipedia.org

24.05.2020, 12:45:17

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

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

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

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

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

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

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

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

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

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

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

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

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

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

// ==UserScript==
// @name        Sidebar toggle, Ru/En lang buttons for wikipedia.org
// @namespace   Violentmonkey Scripts
// @match       https://*.wikipedia.org/*
// @grant       none
// @version     1.1
// @author      -
// @description 24.05.2020, 12:45:17
// ==/UserScript==

const content = document.querySelector('#content')
const sidebar = document.querySelector('#mw-panel')
let open = true

const buttons = document.createElement('div')
const sidebarBtn = document.createElement('button')
const engBtn = document.createElement('button')
const rusBtn = document.createElement('button')

Object.assign(buttons.style, {position: 'absolute', top: '10px', left: '10px'})
sidebarBtn.innerText = 'sidebar'
engBtn.innerText = 'Eng'
rusBtn.innerText = 'Rus'

buttons.append(sidebarBtn, engBtn, rusBtn)

document.body.append(buttons)

function toggleSidebar() {
  if (open) {
    content.style.marginLeft = '0'
    sidebar.style.display = 'none'
  } else {
    content.style.marginLeft = null
    sidebar.style.display = null
  }
  open = !open
}

// document.body.addEventListener('keydown', e => {})

sidebarBtn.onclick = toggleSidebar

rusBtn.onclick = () => rus.click()

toggleSidebar()


const eng = document.querySelector('.interlanguage-link-target[hreflang="en"]')
const rus = document.querySelector('.interlanguage-link-target[hreflang="ru"]')

if (eng) engBtn.onclick = () => eng.click()
else engBtn.remove()
if (rus) rusBtn.onclick = () => rus.click()
else rusBtn.remove()