User:Amousey/common.js

// ==UserScript== // @name        This Ain't A Phone // @namespace   https://schiff.io // @version      9 // @description Automatically redirect from mobile webpages to the non-mobile equivalent // @author      Hayden Schiff (oxguy3) // @match       *://m.mediawiki.org/* // @match       *://*.m.wikimedia.org/* // @match       *://*.m.wikipedia.org/* // @grant       none // @run-at      document-start // ==/UserScript==

(function {   'use strict';

// Checks if the location is a mobile site, and returns the URL of the non-mobile equivalent // Params: loc: window.location // Returns: string of new URL, or false if no match function checkLocation(loc) {

// simple hostname replacement rules var hostRules = [ [ 'm.mediawiki.org', 'www.mediawiki.org' ], [ 'en.m.wikipedia.org', 'en.wikipedia.org' ] ];

// shared logic for sites that only require a changed hostname for (var i = 0; i < hostRules.length; i++) { var rule = hostRules[i]; if (loc.host == rule[0]) { return loc.href.replace('//'+rule[0], '//'+rule[1]); }       }

// special logic for Wikimedia sites (too many subdomains to list individually) var wikimediaHosts = [ 'wikipedia.org', 'wikimedia.org' ];       for (var i = 0; i < wikimediaHosts.length; i++) { var wmHost = wikimediaHosts[i]; if (loc.host.endsWith('.m.'+wmHost)) { var newHost = loc.host.slice(0, 0 - ('m.'+wmHost).length) + wmHost; return loc.protocol + "//" + newHost + ":" + loc.port + loc.pathname + loc.search + loc.hash; }       }

return false; }

var destination = checkLocation(window.location); if (destination !== false) { window.location.href = destination; } });

// whitespace fixer from Wikipedia // User:Waldyrious/whitespacefixer.js // Fork of User:Omegatron/monobook.js/headingformattingfixer.js, // without the code to normalize headings // (i.e. add spaces within the equal signs, and newlines after the headings) // since it produced dirty diffs too often. // Diff of the original fork: https://en.wikipedia.org?diff=625426859&oldid=625426743

function whitespacefixer { var txt = document.editform.wpTextbox1;

// 1 blank line before the headings // (this is super-kludgy because of the newlines conflicting when one heading is right after another) // (seems to work, though, for now. some other day...) txt.value = txt.value.replace(/(\n+={2,}) (.*?) (={2,})/g, '\n\n$1 $2 $3');

// Remove all extra newlines? txt.value = txt.value.replace(/(\n[ \t\v\r\f]*){2,}/g, '\n\n');

// One space after * and # lists, and after : and ; txt.value = txt.value.replace(/(^|\n)((\*|\#|\;|\:)+)\s*(\w|\[|\"|\'|\{\{)/g, '$1$2 $4');

// Add a tag to the summary box var editSummary = document.editform.wpSummary; var appendToSummary = "cleanup whitespace (using Regex whitespace fixer)"; if (editSummary.value.indexOf(appendToSummary) == -1) { if (editSummary.value.match(/[^\*\/\s][^\/\s]?\s*$/)) { editSummary.value += "; "; }       editSummary.value += appendToSummary; } }

$(function {    if(document.forms.editform) {        mw.util.addPortletLink('p-cactions', 'javascript:whitespacefixer', '_', 'ca-whitespacefixer', 'Fix whitespace', '-', '');    } });