V2EX = way to explore
V2EX 是一个关于分享和探索的地方
现在注册
已注册用户请  登录
Get Google Chrome
Vimium · 在 Chrome 里使用 vim 快捷键
jinreal
V2EX  ›  Chrome

维基百科中英文页面切换插件

  •  
  •   jinreal · 6 天前 · 312 次点击

    昨天开发今天刚上架的,欢迎试用 :]

    https://chromewebstore.google.com/detail/wikipedia-language-switch/bkgagbbneincfejedomdhfpfmfaceabo

    使用方法:安装后维基页面标题右方会附上对应的英/中页面链接。

    2 条回复    2025-08-27 15:28:36 +08:00
    troilus
        1
    troilus  
       6 天前
    有没有可能用扩展太“重”了,一个油猴脚本:
    ```
    // ==UserScript==
    // @name 维基百科跨语言跳转助手(改进版)
    // @namespace http://tampermonkey.net/
    // @version 1.1
    // @description 在维基百科页面显示对应语言版本的链接,从语言链接栏获取准确 URL
    // @author ChatGPT
    // @match https://zh.wikipedia.org/*
    // @match https://en.wikipedia.org/*
    // @match https://*.wikipedia.org/*
    // @icon https://www.wikipedia.org/static/favicon/wikipedia.ico
    // @grant none
    // @license MIT
    // ==/UserScript==

    (function() {
    'use strict';

    // 添加自定义样式
    const style = document.createElement('style');
    style.textContent = `
    .wp-crosslang-container {
    display: inline-block;
    margin-left: 12px;
    vertical-align: middle;
    }

    .wp-crosslang-btn {
    background: #f8f9fa;
    border: 1px solid #a2a9b1;
    border-radius: 2px;
    padding: 3px 8px;
    font-size: 12px;
    color: #0645ad;
    cursor: pointer;
    transition: all 0.2s ease;
    text-decoration: none;
    display: inline-block;
    }

    .wp-crosslang-btn:hover {
    background: #fff;
    border-color: #0645ad;
    color: #0645ad;
    text-decoration: none;
    }

    .wp-crosslang-btn:active {
    background: #f0f0f0;
    }

    .wp-crosslang-loading {
    opacity: 0.7;
    cursor: wait;
    }

    @media screen and (max-width: 768px) {
    .wp-crosslang-container {
    display: block;
    margin-left: 0;
    margin-top: 8px;
    }
    }
    `;
    document.head.appendChild(style);

    // 获取对应语言的链接
    function getLanguageLink(targetLang) {
    // 在侧边栏语言列表中找到目标语言链接
    const langList = document.querySelector('#p-lang .wbc-editpage, #p-lang .wb-langlinks-link');
    if (langList) {
    const langLinks = document.querySelectorAll('#p-lang li a');
    for (const link of langLinks) {
    const href = link.getAttribute('href');
    const lang = link.getAttribute('lang') || link.getAttribute('hreflang') || '';
    const text = link.textContent.trim();

    if (lang === targetLang ||
    (targetLang === 'en' && text === 'English') ||
    (targetLang === 'zh' && text === '中文')) {
    return href;
    }
    }
    }

    // 尝试在页面底部的语言链接中找到
    const interwikiLinks = document.querySelectorAll('.interlanguage-link a');
    for (const link of interwikiLinks) {
    const lang = link.getAttribute('lang') || link.getAttribute('hreflang') || '';
    const text = link.textContent.trim();
    const href = link.getAttribute('href');

    if (lang === targetLang ||
    (targetLang === 'en' && text === 'English') ||
    (targetLang === 'zh' && text === '中文')) {
    return href;
    }
    }

    return null;
    }

    // 获取当前语言和目标语言
    function getLanguages() {
    const hostname = window.location.hostname;

    if (hostname.includes('zh.') || hostname.includes('.zh.')) {
    return { current: 'zh', target: 'en', targetName: 'English' };
    } else if (hostname.includes('en.') || hostname.includes('.en.')) {
    return { current: 'en', target: 'zh', targetName: '中文' };
    }

    // 默认情况
    return { current: 'en', target: 'zh', targetName: '中文' };
    }

    // 创建跳转按钮
    function createJumpButton(href, targetName) {
    const container = document.createElement('div');
    container.className = 'wp-crosslang-container';

    const button = document.createElement('a');
    button.className = 'wp-crosslang-btn';
    button.href = href;
    button.target = '_blank';
    button.rel = 'noopener noreferrer';
    button.textContent = targetName;

    // 添加点击事件
    button.addEventListener('click', function(e) {
    console.log(`跳转到${targetName}版维基百科`);
    });

    container.appendChild(button);
    return container;
    }

    // 创建加载中的按钮
    function createLoadingButton(targetName) {
    const container = document.createElement('div');
    container.className = 'wp-crosslang-container';

    const button = document.createElement('span');
    button.className = 'wp-crosslang-btn wp-crosslang-loading';
    button.textContent = `加载${targetName}...`;

    container.appendChild(button);
    return container;
    }

    // 主函数
    function init() {
    // 获取语言信息
    const { target, targetName } = getLanguages();

    // 获取标题元素
    const titleElement = document.querySelector('#firstHeading, .mw-first-heading, .firstHeading, h1');
    if (!titleElement) {
    console.log('无法找到标题元素');
    return;
    }

    // 先创建加载中的按钮
    const loadingButton = createLoadingButton(targetName);
    titleElement.appendChild(loadingButton);

    // 尝试获取语言链接
    const langLink = getLanguageLink(target);

    if (langLink) {
    // 移除加载按钮,添加实际链接按钮
    titleElement.removeChild(loadingButton);
    const button = createJumpButton(langLink, targetName);
    titleElement.appendChild(button);
    } else {
    // 没有找到对应语言链接,更新按钮状态
    loadingButton.querySelector('span').textContent = `无${targetName}版本`;
    loadingButton.querySelector('span').classList.remove('wp-crosslang-loading');
    }
    }

    // 页面加载完成后执行
    if (document.readyState === 'loading') {
    document.addEventListener('DOMContentLoaded', init);
    } else {
    init();
    }
    })();
    ```
    jinreal
        2
    jinreal  
    OP
       6 天前   ❤️ 1
    @troilus 谢谢,厉害!我很多年前应该就是写了个 tampermonkey 脚本(代码很短自己用的,没发布)。轻重的话,一般的 app 和微信小程序哪个重?可能不是那么容易简单比较。现在算是个最简版或极简版,有很多可升级的方面,后面可能改变会很大,谢谢期待哈。
    关于   ·   帮助文档   ·   自助推广系统   ·   博客   ·   API   ·   FAQ   ·   实用小工具   ·   2880 人在线   最高记录 6679   ·     Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 · 26ms · UTC 14:44 · PVG 22:44 · LAX 07:44 · JFK 10:44
    Developed with CodeLauncher
    ♥ Do have faith in what you're doing.