User:Eighty5cacao/common.js

From Pin Eight
< User:Eighty5cacao
Revision as of 19:16, 28 November 2018 by Eighty5cacao (talk | contribs) (try moving this to vector.js (1/2))
(diff) ← Older revision | Latest revision (diff) | Newer revision → (diff)
Jump to: navigation, search
mw.loader.using( [ 'mediawiki.util', 'mediawiki.user', 'mediawiki.notify' ], function ( ) {
  window.zzAction = mw.config.get( 'wgAction' );
  window.zzNamespaceNumber = mw.config.get( 'wgNamespaceNumber' );
  window.zzPageContentModel = mw.config.get( 'wgPageContentModel' );
  window.zzPageName = mw.config.get( 'wgPageName' );

  window.jsfnPortletLink = function ( container, myFunc, dispName, liId, myArg ) {
    var typ = typeof myFunc;
    if ( typ !== 'function' ) {
      var errStr = 'myFunc was a(n) ' + typ + ', but we needed a function';
      mw.notify( $( '<strong class="error">[jsfnPortletLink] ' + errStr + '</strong>' ) );
      throw new TypeError( errStr );
    }
    //...let addPortletLink do its own error handling for the rest

    var plnk = mw.util.addPortletLink(
                            container,    '#', dispName, liId
    );
    $( plnk ).click( function ( e ) {
      e.preventDefault( );

      //TODO: expand to handle multiple args
      myFunc.call( this, myArg );
    });
  }

  window.fakeEditNoticeForDotClockRates = function ( ) {
    if ( ( zzAction === 'edit' || zzAction === 'submit' ) && zzPageName === 'Dot_clock_rates' ) {
      mw.notify( $(
          '<strong class="error">' +
          'STOP! Do NOT edit this article every time a GitHub commit affects screen raw params. ' +
          'Put it in userspace and wait a day or two.' +
          '</strong>'
        ), { autoHide: false }
      );
    }
  }

  var invState = Math.floor( Math.random( ) * 3 );
  var hue1 = Math.floor( Math.random( ) * 360 );
  var hue2 = Math.floor( Math.random( ) * 360 );
  var sat1 = Math.floor( Math.random( ) * 100 ) + Math.ceil( Math.random( ) * 100 );
  var sat2 = Math.floor( Math.random( ) * 100 ) + Math.ceil( Math.random( ) * 100 );
  mw.util.addCSS(       '#p-logo { filter:' + ( invState == 1 ? 'invert(100%) ' : '' ) + 'hue-rotate(' + hue1 + 'deg) saturate(' + sat1 + '%); }' );
  mw.util.addCSS( '#p-logo:hover { filter:' + ( invState == 2 ? 'invert(100%) ' : '' ) + 'hue-rotate(' + hue2 + 'deg) saturate(' + sat2 + '%); }' );

  mw.loader.load( '/mw/index.php?title=User:Eighty5cacao/enterInSummaryPreviews.js&action=raw&ctype=text/javascript' );

  window.printSHStatus = function ( ) {
    mw.notify( $( '<samp>TOut: ' + syntaxHighlighterConfig.timeout        + 'ms<br />'
                           + 'Best: ' + window.syntaxHlBestTime                + 'ms<br />'
                           + 'Wrst: ' + window.syntaxHlWorstTime               + 'ms<br />'
                           + 'UpLf: ' + window.syntaxHlTimeoutUpdatesLeft      + '<br />'
                           + 'SpTl: ' + syntaxHighlighterConfig.spikeTolerance + 'ms<br />'
                           + 'NumS: ' + window.syntaxHlNumSpikesTolerated      + '<br />'
                           + 'Abrt: ' + window.syntaxHlAborted                 + '<br />'
                           + 'dPRF: ' + window.syntaxHlDetectedResistFingerprintingPref + '</samp>' ) );
  }
  window.syntaxHighlighterConfig = {
    adaptiveTiming: true,
    timeoutCallback: function( thresh, actual ) {
      mw.notify( $( '<samp>syntax highlighter abort ' + actual + 'ms&gt;' + thresh + 'ms<br />'
                             + 'Best: '   + window.syntaxHlBestTime               + 'ms<br />'
                             + 'Wrst: '   + window.syntaxHlWorstTime              + 'ms<br />'
                             + 'UpLf: ' + window.syntaxHlTimeoutUpdatesLeft      + '<br />'
                             + 'SpTl: ' + syntaxHighlighterConfig.spikeTolerance + 'ms<br />'
                             + 'NumS: ' + window.syntaxHlNumSpikesTolerated      + '<br />'
                             + 'Abrt: ' + window.syntaxHlAborted                 + '<br />'
                             + 'dPRF: ' + window.syntaxHlDetectedResistFingerprintingPref + '</samp>' ), { autoHide: false } );
    }
  }
  if ( Math.random( ) < 5/8 ) {
    syntaxHighlighterConfig.timeoutMsg = '';
  }
  else {
    syntaxHighlighterConfig.languageOverride = 'es';
  }
  if ( ( zzAction === 'edit' || zzAction === 'submit' ) && zzPageContentModel === 'wikitext' && syntaxHighlighterConfig.adaptiveTiming ) {
    jsfnPortletLink( 'p-cactions', printSHStatus, 'synHs', 'ca-syntaxhighlightdbg' );
  }
  mw.loader.load( '/mw/index.php?title=User:Eighty5cacao/syntax_highlighter_experimental.js&action=raw&ctype=text/javascript' );

  mw.loader.load( '/mw/index.php?title=User:Eighty5cacao/cleanArrows.js&action=raw&ctype=text/javascript' );

  window.aBBWarnInitially = ( zzPageContentModel !== 'wikitext' || /[:\/]sandbox/i.test( zzPageName ) || ( zzAction == 'submit' && (zzNamespaceNumber % 2) == 0 ) );
  mw.loader.load( '/mw/index.php?title=User:Eighty5cacao/antiBracketBot.js&action=raw&ctype=text/javascript' );

  window.mbLocalContribsName = ['Contributions', 'Contribs', 'Block', 'DeletedContributions', 'EmailUser', 'Emailuser', 'ListFiles'];
  window.mbLoadingOpacity = 0.25;
  window.mbTempStyle = 'opacity:0.63; font-style:normal; text-decoration:line-through';
  window.mbIndefStyle = 'font-style:italic; text-decoration:line-through; text-shadow:#F50800 0px 0px 2px';
  window.mbShowRegDateRel = true;
  window.mbShowYears = true;
  window.mbRemoveWaitingCSSOnError = false;
  window.mbReplaceUserRedLinksWithContribs = true;
  //?
  window.mbNoAutoStart = !(
    ( zzNamespaceNumber === -1 && zzPageName !== 'Special:Contributions/Eighty5cacao' ) ||
    ( ( zzAction === 'history' || zzNamespaceNumber % 2 == 1 ) && Math.random( ) < .37 )
  );
  window.mbOnDemandLinkText = 'Userlink info';
  window.mbOnDemandLinkLoc = 'p-tb';
  mw.loader.load( '/mw/index.php?title=User:Eighty5cacao/markBlockedPlus.js&action=raw&ctype=text/javascript' );

  window.numCBPTRetries = 0;
  window.checkBadPipeTrick = function ( ) {
    var edsumbox = $( '#wpSummary' );
    if ( !edsumbox || !edsumbox.length ) {
      if ( window.numCBPTRetries < 2 ) {
        setTimeout( checkBadPipeTrick, 50 );
      }
      else {
        mw.notify( "checkBadPipeTrick did not get valid edsumbox", { autoHide: false } );
      }
      window.numCBPTRetries++;
      return;
    }
    var edsumboxtext = edsumbox.val( );
    if ( typeof edsumboxtext !== 'string' ) {
      if ( window.numCBPTRetries < 2 ) {
        setTimeout( checkBadPipeTrick, 50 );
        window.numCBPTRetries++;
      }
      else {
        mw.notify( "checkBadPipeTrick did not get valid edsumboxtext", { autoHide: false } );
      }
      return;
    }
    if ( /\[\[.+\|\]\]/.test( edsumboxtext ) ) {
      mw.util.addCSS( '.mw-summary-preview{color:red;}' )
      mw.util.addCSS( '.mw-summary-preview a{text-decoration:line-through;}' )
      mw.util.addCSS( '.mw-summary-preview a:not(.new){color:red;}' )
      mw.notify( $( '<span class="badpipewarn">Attempted pipe trick in edit summary &mdash; does NOT work</span>' ) );
    }
    else if ( /^Undo revision \d+ by \[\[Special:Contributions\/Eighty5cacao\|Eighty5cacao\]\] \(\[\[User talk:Eighty5cacao\|talk\]\]\)$/.test( edsumboxtext ) ) {
      //DIRTY HACK - move this elsewhere
      //if ( zzAction === 'edit' ) { //that is, not 'submit'
        edsumbox.val( edsumboxtext.replace( 'Eighty5cacao]] ([[User talk:Eighty5cacao|talk]])', 'self]]: ' ).replace( 'Undo ', 'Undid ' ) );
      //}
    }

    var editbox = $( '#wpTextbox1' );
    if ( !editbox || !editbox.length ) {
      if ( window.numCBPTRetries < 2 ) {
        setTimeout( checkBadPipeTrick, 50 );
        window.numCBPTRetries++;
      }
      else {
        mw.notify( "checkBadPipeTrick did not get valid editbox", { autoHide: false } );
      }
      return;
    }
    var editboxtext = editbox.val( );
    if ( typeof editboxtext !== 'string' ) {
      if ( window.numCBPTRetries < 2 ) {
        setTimeout( checkBadPipeTrick, 50 );
        window.numCBPTRetries++;
      }
      else {
        mw.notify( "checkBadPipeTrick did not get valid editboxtext", { autoHide: false } );
      }
      return;
    }
    if ( /<ref( name="[^<>]+")?>[^<>]*\[\[[^<>]+\|\]\][^<>]*<\/ref>/.test( editboxtext ) )
      mw.notify( $( '<span class="badpipewarn">Attempted pipe trick in footnote &mdash; does NOT work</span>' ) );

    //XXX: maybe shoehorn this into antiBracketBot.js instead?
    //XXX: string is broken up to avoid warning when editing common.js itself.
    //     better to check wgPageContentModel?
    if ( /\]\] ?\[\[/.test( editboxtext ) ) {
      mw.notify( ']]' + '[[ = :-(' );
    }

    if ( zzNamespaceNumber == 0 && /\[https?:\/\/(forums|wiki)\.nesdev\.com\//.test( editboxtext ) ) {
      mw.notify( $( '<strong class="error">There are absolute links to NESdev; please consider making them protocol-relative</strong>' ) );
    }
  }

  window.miscCommonJsStuff = function ( ) {
      var node = document.getElementById( 'wikiPreview' );
      if ( !node ) { node = document.getElementById( 'mw-content-text' ); }
      if ( node ) {
          var alist = node.getElementsByTagName( 'A' );
          if ( alist.length === 0 ) { return; }

          Array.prototype.forEach.call( alist, function ( a ) {
              var $a = $( a );
              var myURL = $a.attr( 'href' );
              if ( !myURL ) { return; }

              var anchorStart = myURL.indexOf( '#' );
              if ( anchorStart != -1 ) {
                  var myAnchor = myURL.substr( anchorStart );
                  var myTtip = $a.attr( 'title' );
                  if ( !( $a.hasClass( 'external' ) ) ) {
                      myAnchor = myAnchor.replace( /_/g, ' ' );
                  }

                  if ( myTtip ) {
                      $a.attr( 'title', myTtip + myAnchor );
                  }
                  else {
                      $a.attr( 'title', myAnchor );
                  }
              }

              // XXX: Just to be safe, add a configuration setting to disable this
              if (
                      myURL.indexOf( 'title=' ) !== -1 &&
                      myURL.indexOf( 'curid=' ) !== -1 &&
                      !( $a.hasClass( 'external' ) )  &&
                      !( $a.hasClass( 'extiw' ) )
              ) {
                  var fixURL = myURL.replace( /&curid=\d+/, '' );
                  if ( fixURL !== myURL ) {
                      $a.addClass( 'curid-stripped' );
                      $a.attr( 'href', fixURL );
                  }
              }
          });
      }
  }
  miscCommonJsStuff( );

} )