User:Eighty5cacao/cleanArrows.js

From Pin Eight
Jump to: navigation, search
function cleanArrowMarkup( ) {
  var editbox = $( '#wpTextbox1' );
  if ( !editbox ) return;
  var editboxtext = editbox.val( );
  if ( typeof editboxtext != 'string' ) return;

  var neweditboxtext = editboxtext;
  neweditboxtext = neweditboxtext.replace( /←/g, '←' ).replace( /→/g, '→' );
  neweditboxtext = neweditboxtext.replace( /≤/g,   '≤' ).replace( /≥/g,   '≥' );
  neweditboxtext = neweditboxtext.replace( /≈/g, '≈' );
  neweditboxtext = neweditboxtext.replace( /§/g, '§' );
  neweditboxtext = neweditboxtext.replace( / —/g, ' –' );

  if ( mw.config.get( 'wgNamespaceNumber' ) === 0 ) {
    neweditboxtext = neweditboxtext.replace( /\[https?:\/\/(forums|wiki)\.nesdev\.com\//g, '[//$1.nesdev.com/' );
  }
  else {
    neweditboxtext = neweditboxtext.replace( /\[http:\/\/(forums|wiki)\.nesdev\.com\//g, '[https' + '://$1.nesdev.com/' );
  }

  neweditboxtext = neweditboxtext.replace( /\[http:\/\/(arstechnica\.com|www\.livescience\.com|notalwaysright\.com|shmups\.system11\.org|slashdot\.org|www\.theverge\.com|www\.cracked\.com)\//g, '[https://$1/' );

  // We don't secure /. subdomains automatically because there may be some, not used for regular posting,
  // hosted on misconfigured 3rd-party services
  var shouldWarnAboutSlashdotSubdoms = /\[http:\/\/\w+\.slashdot\.org\//.test( neweditboxtext );

  // Unlike entities, tags are not case-sensitive
  neweditboxtext = neweditboxtext.replace( /<br>/gi, '<br />' );

  // XXX: Should this be a strict === comparison?
  if ( neweditboxtext == editboxtext ) {
    if ( shouldWarnAboutSlashdotSubdoms ) {
      mw.notify(
        'Nothing replaced automatically, but ' +
        'there are links to Slashdot subdomains; please check them manually',
        { tag: 'cleanarrows-slashdot' }
      );
    }
    else {
      mw.notify( 'Nothing to replace', { tag: 'cleanarrows' } );
    }
  }
  else {
    if ( shouldWarnAboutSlashdotSubdoms ) {
      mw.notify( 'There are links to Slashdot subdomains; please check them manually', { tag: 'cleanarrows-slashdot' } );
    }
    editbox.val( neweditboxtext );
  }
}

// edit, submit
// XXX: should also check for wgPageContentModel==='wikitext'?
if ( /it$/.test( mw.config.get( 'wgAction' ) ) ) {
  $( function ( ) {
    var plnk = mw.util.addPortletLink(
      'p-cactions',
      '#cm-' + Math.random( ),
      'Clean→',
      'ca-cleanarrowmarkup'
    );
    $( plnk ).click( function ( e ) {
      e.preventDefault( );
      cleanArrowMarkup( );
    });
  });
}