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 Page
// @namespace http://tampermonkey.net/
// @version 0.7
// @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
// @match https://*.wikipedia.org/*
// @match https://*.wiktionary.org/*
// @match https://*.wikimedia.org/*
// @match https://*.wikiquote.org/*
// @grant none
// @license MIT
// ==/UserScript==
/* jshint esversion: 6 */
// CONFIGURE THE SCRIPT HERE (set any of these to true or false, no other value):
const AUTOLOAD_MOBILE = true; // if set to true, loads the mobile page automatically when landing on a Wikimedia website.
const ENABLE_MOBILE_DESKTOP_SHORTCUT = true; // if set to true, lets you switch between mobile and non-mobile with ctrl-m on Windows or alt-m on Mac.
const ENABLE_EDIT_SHORTCUT = true; // if set to true, lets you edit an article with ctrl-e on Windows or alt-e on Mac.
const VIEW_HISTORY_IN_DESKTOP_MODE = true; // if set to true, view a wiki page's edit history in desktop mode. This is set to true by default since the history view on mobile is severely limited.
// (END OF CONFIGURATION)
const WEBSITES = ['wikipedia', 'wiktionary', 'wikimedia', 'wikiquote'].join('|'); // in order to reuse this list underneath we have to use the RegExp constructor instead of the literal syntax.
const FULL_URL_REGEX = new RegExp('^(?<prefix>https?://[^/]*)(?<suffix>\\.(' + WEBSITES + ')\\.org/.*)');
const DOMAIN_REGEX = new RegExp('^(?<prefix>[^/]*)(?<suffix>\\.(' + WEBSITES + ')\\.org)');
const LANGUAGE_REGEX = new RegExp('^(https?://)(?<lang>[a-z]+)(\\.(' + WEBSITES + ')\\.org/.*)');
const DISABLED_ON_DOMAINS_REGEX = /^(upload\.wikimedia\.org)|(wikitech.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;
}
}
}
/**
* Returns whether this is a history view of a page's changes, on a mobile Wiki.
*/
function isMobileSpecialHistoryPage(currentWiki) {
return isMobile(currentWiki) && (location.href.indexOf('/Special:History/') !== -1);
}
/**
* For a Special:History page, return the name of the Wiki page being examined.
*/
function extractPageNameFromSpecialHistoryPage(pathname) {
const regex = /\/wiki\/Special:History\/(?<pagename>[^\/]+)\/?.*$/;
const match = regex.exec(pathname);
return match ? match.groups.pagename : null;
}
/**
* An additional 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 disabledForDomain = isMobileDisabledForThisDomain(); // make sure we don't have an exclusion rule for this domain
if (VIEW_HISTORY_IN_DESKTOP_MODE && isMobileSpecialHistoryPage(currentWiki)) { // extract domain and page, and redirect to full equivalent desktop page
const url = new URL(location.href);
const nonMobileDomain = changeDomainToMobileOrBack(url.hostname);
const pageName = extractPageNameFromSpecialHistoryPage(url.pathname);
if (nonMobileDomain && pageName) {
console.log(`VIEW_HISTORY_IN_DESKTOP_MODE enabled, going to ${nonMobileDomain} for ${pageName} history`);
const newUrl = `https://${nonMobileDomain}/w/index.php?title=${pageName}&action=history`;
location.href = newUrl;
return;
}
}
if (AUTOLOAD_MOBILE) {
const willSwitch = (currentWiki && !isMobile(currentWiki) && !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);
}
})();