Greasy Fork is available in English.
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.
当前为
// ==UserScript==
// @name Switch to Mobile Wikipedia/Wiktionary and back, Autoload Mobile
// @namespace http://tampermonkey.net/
// @version 0.3
// @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
// @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)\.org\/.*)/;
const DOMAIN_REGEX = /^(?<prefix>[^\/]*)(?<suffix>\.(wikipedia|wiktionary)\.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 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
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);
}
})();