Switch to Mobile Wikipedia/Wiktionary and back

Automatically loads Mobile Wikipedia if you visit the desktop version of Wikipedia from a different site; provides a shortcut to switch between the two and stays on the version you switched to.

目前为 2021-08-25 提交的版本。查看 最新版本

// ==UserScript==
// @name         Switch to Mobile Wikipedia/Wiktionary and back
// @namespace    http://tampermonkey.net/
// @version      0.1
// @description  Automatically loads Mobile Wikipedia if you visit the desktop version of Wikipedia from a different site; provides a shortcut to switch between the two and stays on the version you switched to.
// @author       https://gf.qytechs.cn/en/users/728793-keyboard-shortcuts
// @icon         https://en.wikipedia.org/favicon.ico
// @include      https://*.wikipedia.org/*
// @include      https://*.wiktionary.org/*
// @grant        none
// ==/UserScript==

/* jshint esversion: 6 */

function isMobile(match) {
    return match[1].endsWith('.m');
}

function switchToMobile(match) {
    location.href = match[1] + '.m' + match[2];
}

function switchToMobileOrBack(match) {
    if (isMobile(match)) { // mobile: remove tailing '.m'
        location.href = match[1].substr(0, match[1].length - 2) + match[2];
    } else {
        switchToMobile(match);
    }
}


function switchToEditMode(match) {
    const editSuffix = '?action=edit';
    const currentlyEditing = (location.href.indexOf(editSuffix) != -1 || location.href.indexOf('#/editor/') != -1);
    if (!currentlyEditing) {
        if (isMobile(match)) { // mobile: switch back to non-mobile page and add edit suffix
            location.href = match[1].substr(0, match[1].length - 2) + match[2] + editSuffix;
        } else {
            location.href = location.href + editSuffix;
        }
    } else {
        const newUrl = isMobile(match) ? location.href : match[1] + '.m' + match[2];
        location.href = newUrl.replaceAll(/#\/editor\/.*/g, '').replaceAll(editSuffix, ''); // was editing: go back to regular mobile page
    }
}

(function() {
    'use strict';
    const websiteRegex = /^(https?:\/\/[^\/]*)(\.(wikipedia|wiktionary)\.org\/.*)/;

    const previousHref = document.referrer || '';
    const referrerWiki = websiteRegex.exec(previousHref); // match object for the referrer
    const currentWiki = websiteRegex.exec(location.href); // match object for the current address

    if (!referrerWiki) { // referrer is *not* Wikimedia: make sure we hit the mobile website first
        if (currentWiki && !isMobile(currentWiki)) { // First visit: switch to mobile by default
            switchToMobile(currentWiki);
        }
    } else { // referrer is a Wikimedia site; check if it's a different language and switch to mobile if so
        const languageRegex = /^(https?:\/\/)(?<lang>[a-z]+)(\.(wikipedia|wiktionary)\.org\/.*)/;
        const curLangMatch = languageRegex.exec(location.href);
        const prevLangMatch = languageRegex.exec(previousHref);
        if (curLangMatch && prevLangMatch && curLangMatch.groups.lang !== prevLangMatch.groups.lang) {
            const willSwitch = (currentWiki && !isMobile(currentWiki));
            console.log(`Referrer is a Wikimedia site, but language went from ${ prevLangMatch.groups.lang } to ${ curLangMatch.groups.lang }. Will switch: ${ (willSwitch ? 'YES' : 'NO') }`);
            if (willSwitch) { // Language change: switch to mobile as if we came from a third-party website
                switchToMobile(currentWiki);
            }
        }
    }

    function handleEvent(e) {
        if (!currentWiki) { // not on a Wikimedia website
            return;
        }
        //if(e.target.getAttribute("contenteditable") == "true") { // not for edit modes
        //    return;
        //}
        const isEditing = (location.href.indexOf('action=edit') !== -1);
        if ((e.altKey || e.ctrlKey) && e.code === 'KeyM') { // ctrl-m or alt-m
            switchToMobileOrBack(currentWiki);
        } else if ((e.altKey || e.ctrlKey) && e.code === 'KeyE') { // ctrl-e or alt-e
            console.log('switching to edit mode');
            switchToEditMode(currentWiki);
        }
    }

    addEventListener("keydown", handleEvent);
    addEventListener("keypress", handleEvent);

})();

QingJ © 2025

镜像随时可能失效,请加Q群300939539或关注我们的公众号极客氢云获取最新地址