Switch to Mobile Wikipedia/Wiktionary and Back, Autoload Mobile Page

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.

目前為 2022-04-19 提交的版本,檢視 最新版本

// ==UserScript==
// @name         Switch to Mobile Wikipedia/Wiktionary and Back, Autoload Mobile Page
// @namespace    http://tampermonkey.net/
// @version      0.5
// @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
// @match        https://*.wikipedia.org/*
// @match        https://*.wiktionary.org/*
// @match        https://*.wikimedia.org/*
// @grant        none
// @license      MIT
// ==/UserScript==

/* jshint esversion: 6 */

// CONFIGURE THE SCRIPT HERE:
const AUTOLOAD_MOBILE = true;
const ENABLE_MOBILE_DESKTOP_SHORTCUT = true;
const ENABLE_EDIT_SHORTCUT = true;
const VIEW_HISTORY_IN_DESKTOP_MODE = true;
// (END OF CONFIGURATION)

const FULL_URL_REGEX = /^(?<prefix>https?:\/\/[^\/]*)(?<suffix>\.(wikipedia|wiktionary|wikimedia)\.org\/.*)/;
const DISABLED_ON_DOMAINS_REGEX = /^(upload\.wikimedia\.org)$/;
const DOMAIN_REGEX = /^(?<prefix>[^\/]*)(?<suffix>\.(wikipedia|wiktionary|wikimedia)\.org)/;
const LANGUAGE_REGEX = /^(https?:\/\/)(?<lang>[a-z]+)(\.(wikipedia|wiktionary|wikimedia)\.org\/.*)/;
const EDIT_SUFFIX = 'action=edit';

function isMobile(match) {
    return match.groups.prefix.endsWith('.m');
}

function changeToMobile(match) {
    return match.groups.prefix + '.m' + match.groups.suffix;
}

function loadMobilePage(match) {
    const mobileUrl = new URL(changeToMobile(match));
    location.href = mobileUrl.protocol + '//' + mobileUrl.host + mobileUrl.pathname;
}

function switchToMobileOrBack(match) {
    const url = new URL(location.href);
    const altDomain = changeDomainToMobileOrBack(url.hostname);
    location.href = url.protocol + '//' + altDomain + url.pathname;
}

function changeDomainToMobileOrBack(domain) {
    const match = DOMAIN_REGEX.exec(domain);
    if (!match) {
        console.log('Failed to convert domain:', domain);
        return domain;
    } else if (isMobile(match)) {
        return match.groups.prefix.substr(0, match.groups.prefix.length - 2) + match.groups.suffix;
    } else {
        return changeToMobile(match);
    }
}

function currentlyEditing() {
    const url = new URL(location.href);
    return (url.search.indexOf(EDIT_SUFFIX) !== -1 || url.hash.indexOf('#/editor/') != -1);
}

function switchToEditMode(match) {
    if (!currentlyEditing()) {
        const url = new URL(location.href);
        if (isMobile(match)) { // mobile: switch back to non-mobile page and add edit suffix
            location.href = url.protocol + '//' + changeDomainToMobileOrBack(url.hostname) + url.pathname + '?' + EDIT_SUFFIX;
        } else {
            location.href = url.protocol + '//' + url.host + url.pathname + '?' + EDIT_SUFFIX;
        }
    }
}

/**
 * If configured with VIEW_HISTORY_IN_DESKTOP_MODE, return whether the current page
 * should be exempt from the automatic switch to mobile view.
 */
function isMobileDisabledForHistoryPage() {
    const isHistoryPage = location.href.indexOf('/Special:History/') !== -1;
    return isHistoryPage && VIEW_HISTORY_IN_DESKTOP_MODE;
}

/**
 * An addition regex lets us disable the auto-switch behavior for some domains, like upload.wikimedia.org which doesn't have a .m version.
 */
function isMobileDisabledForThisDomain() {
    const url = new URL(location.href);
    const match = DISABLED_ON_DOMAINS_REGEX.exec(url.hostname);
    return !!match;
}

(function() {
    'use strict';

    const previousHref = document.referrer || '';
    const referrerWiki = FULL_URL_REGEX.exec(previousHref); // match object for the referrer
    const currentWiki = FULL_URL_REGEX.exec(location.href); // match object for the current address
    const disabledForHistoryPage = isMobileDisabledForHistoryPage(); // FIXME: doesn't work: we stay in mobile because of the referrer
    const disabledForDomain = isMobileDisabledForThisDomain(); // make sure we don't have an exclusion rule for this domain

    if (AUTOLOAD_MOBILE) {
        const willSwitch = (currentWiki && !isMobile(currentWiki) && !disabledForHistoryPage && !disabledForDomain);
        if (!referrerWiki) { // referrer is *not* Wikimedia: make sure we hit the mobile website first
            if (willSwitch) { // First visit: switch to mobile by default
                loadMobilePage(currentWiki);
            }
        } else { // referrer is a Wikimedia site; check if it's a different language and switch to mobile if so
            const curLangMatch = LANGUAGE_REGEX.exec(location.href);
            const prevLangMatch = LANGUAGE_REGEX.exec(previousHref);
            if (curLangMatch && prevLangMatch && curLangMatch.groups.lang !== prevLangMatch.groups.lang) {
                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
                    loadMobilePage(currentWiki);
                }
            }
        }
    }

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

    if (ENABLE_MOBILE_DESKTOP_SHORTCUT || ENABLE_EDIT_SHORTCUT) {
        addEventListener("keydown", handleEvent);
        addEventListener("keypress", handleEvent);
    }

})();

QingJ © 2025

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