Greasy Fork

Greasy Fork is available in English.

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

// ==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       http://greasyfork.icu/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);

})();