Greasy Fork

Greasy Fork is available in English.

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-01-15 提交的版本,查看 最新版本

您需要先安装一款用户脚本管理器扩展,例如 Tampermonkey 篡改猴Greasemonkey 油猴子Violentmonkey 暴力猴,才能安装此脚本。

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

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

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

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

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

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

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

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

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

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

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

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

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

// ==UserScript==
// @name         Switch to Mobile Wikipedia/Wiktionary and Back, Autoload Mobile Page
// @namespace    http://tampermonkey.net/
// @version      0.4
// @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       http://greasyfork.icu/en/users/728793-keyboard-shortcuts
// @icon         https://en.wikipedia.org/favicon.ico
// @include      https://*.wikipedia.org/*
// @include      https://*.wiktionary.org/*
// @include      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;
// (END OF CONFIGURATION)

const FULL_URL_REGEX = /^(?<prefix>https?:\/\/[^\/]*)(?<suffix>\.(wikipedia|wiktionary|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;
        }
    }
}

(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

    if (AUTOLOAD_MOBILE) {
        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
                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) {
                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
                    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);
    }

})();