User:Ozuzanna/noSyntaxHighlight.js

/*
 * No Syntax Highlight
 * @description when trying to edit a MW or User JS/CSS page, it will use a surrogate edit page instead to mitigate the terrible syntax highlighting
 * @author Ozuzanna
 * @TODO fix show changes
 * @TODO fix caching

if ((mw.config.get('wgCanonicalNamespace') === "User" || mw.config.get('wgCanonicalNamespace') === "MediaWiki") && mw.config.get('wgAction') === "view" && (mw.config.get('wgTitle').split('.css') !== -1 || mw.config.get('wgTitle').split('.js') !== -1) && $('#ca-edit').length) $('#ca-edit').attr('href',mw.config.get('wgArticlePath').slice(0,-2) + mw.config.get('wgPageName') + '/surrogate?action=edit&nosyntaxhighlight=1');

if (mw.config.get('wgAction') == "edit" && mw.config.get('wgPageName').indexOf('/surrogate') !== -1 && $.getUrlVar('nosyntaxhighlight') == 1) { var page = mw.config.get('wgPageName').slice(0,-10), Api = new mw.Api, articlePath = mw.config.get('wgArticlePath').slice(0,-2), content; document.title = document.title.replace('/surrogate',''); $('#EditPageHeader > h1:nth-child(3) > a:nth-child(1)') .text(page) .attr('href',articlePath + page) .attr('title',page); Api.get({ action: 'query',  prop: 'revisions',  rvprop: 'content',  titles: page  }) .done(function(d) {   if (!d.error) {      for (var i in d.query.pages) {        if (d.query.pages[i].revisions) {          content = d.query.pages[i].revisions[0]["*"];          $('#wpTextbox1').text(content);        }        else {          $('#wpTextbox1').text('Page ' + page + ' does not exist!');        }      }    }    else {      $('#wpTextbox1').text('Failed to get contents of ' + page + ': ' + d.error.code);    }  }) .fail(function {   $('#wpTextbox1').text('Failed to get contents of ' + page);  }); $('#wpSave').click(function(e) {   e.preventDefault;

Api.post({   action: 'edit',    watchlist: 'nochange',    title: page,    summary: $('#wpSummary').val || '',    nocreate: $('#wpTextbox1').val,    text: newContent,    bot: true,    token: mw.user.tokens.get('editToken')    }) .done(function(d) {     if (!d.error) {        $('#wpTextbox1').val('');        setTimeout(function { location.replace(articlePath + page); }, 1000);     }      else {        new BannerNotification('Failed to submit edit for ' + page + ': ' + d.error.code,'error').show;      }    }) .fail(function {     new BannerNotification('Failed to submit edit for ' + page,'error').show;    }); }); }