MediaWiki:Gadget-markreview.js: Difference between revisions

From Taranaki Lab Manual
Jump to navigation Jump to search
Created page with "mw.loader.using(['mediawiki.api', 'mediawiki.util', 'mediawiki.user']).then(function () { var owner = document.querySelector('.doc-meta')?.getAttribute('data-owner'); var user = mw.config.get('wgUserName'); if (!owner || !user || owner.toLowerCase() !== user.toLowerCase()) { return; // Not the owner, don't show button } var link = mw.util.addPortletLink( 'p-cactions', '#', 'Mark as reviewed', 'ca-markreview',..."
 
No edit summary
Line 1: Line 1:
mw.loader.using(['mediawiki.api', 'mediawiki.util', 'mediawiki.user']).then(function () {
/* global mw, $ */
     var owner = document.querySelector('.doc-meta')?.getAttribute('data-owner');
mw.loader.using( [ 'mediawiki.api', 'mediawiki.util', 'mediawiki.user' ] ).then( function () {
     var user = mw.config.get('wgUserName');
  function run( $content ) {
    // Find the DocMeta table and owner
     var $docMeta = $content.find( '.doc-meta' ).first();
     if ( !$docMeta.length ) {
      // Try outside content as a fallback
      $docMeta = $( '.doc-meta' ).first();
      if ( !$docMeta.length ) return;
    }


     if (!owner || !user || owner.toLowerCase() !== user.toLowerCase()) {
    var owner = $docMeta.attr( 'data-owner' );
         return; // Not the owner, don't show button
    var user = mw.config.get( 'wgUserName' );
    var action = mw.config.get( 'wgAction' ) || 'view';
    if ( action !== 'view' ) return;
     if ( !owner || !user || owner.toLowerCase() !== user.toLowerCase() ) return;
 
    function doReview() {
      var page = mw.config.get( 'wgPageName' );
      var stamp = new Date().toISOString();
      var api = new mw.Api();
 
      api.postWithToken( 'csrf', {
        action: 'edit',
        title: page,
        summary: 'Mark as reviewed',
        appendtext: '\n<!-- review-ping: ' + stamp + ' -->',
        minor: true,
        nocreate: true
      } ).done( function () {
         location.reload();
      } ).fail( function ( err ) {
        mw.notify( 'Review failed: ' + ( err && err.error && err.error.info ? err.error.info : err ), { type: 'error' } );
      } );
     }
     }


     var link = mw.util.addPortletLink(
    // Try to add to actions menu; fall back gracefully
        'p-cactions',
    var linkId = 'ca-markreview';
        '#',
     var link = mw.util.addPortletLink( 'p-cactions', '#', 'Mark as reviewed', linkId, 'Mark this page as reviewed' );
        'Mark as reviewed',
    if ( !link ) {
        'ca-markreview',
      link = mw.util.addPortletLink( 'p-views', '#', 'Mark as reviewed', linkId, 'Mark this page as reviewed' );
        'Mark this page as reviewed'
    }
     );
     if ( !link ) {
      link = mw.util.addPortletLink( 'p-tb', '#', 'Mark as reviewed', linkId, 'Mark this page as reviewed' );
    }


     link.addEventListener('click', function (e) {
     if ( link ) {
      link.addEventListener( 'click', function ( e ) {
         e.preventDefault();
         e.preventDefault();
         var page = mw.config.get('wgPageName');
         doReview();
         var stamp = new Date().toISOString();
      } );
         var api = new mw.Api();
      return;
    }
 
    // If no menu exists (e.g., certain skins), inject a button under the DocMeta table
    var $btn = $( '<a>' )
      .attr( 'href', '#' )
      .text( 'Mark as reviewed' )
      .css( {
        display: 'inline-block',
        marginTop: '8px',
        padding: '4px 8px',
        background: '#f0f0f0',
        border: '1px solid #ccc',
        borderRadius: '2px',
         textDecoration: 'none'
      } )
      .on( 'click', function ( e ) {
        e.preventDefault();
         doReview();
      } );
 
    // Insert after the table
    $docMeta.after( $( '<div>' ).append( $btn ) );
  }


        api.postWithToken('csrf', {
  // Run on initial load and after AJAX loads
            action: 'edit',
  mw.hook( 'wikipage.content' ).add( run );
            title: page,
} );
            summary: 'Mark as reviewed',
            appendtext: '\n<!-- review-ping: ' + stamp + ' -->',
            minor: true,
            nocreate: true
        }).done(function () {
            location.reload();
        }).fail(function (err) {
            mw.notify('Review failed: ' + err, { type: 'error' });
        });
    });
});

Revision as of 16:16, 8 September 2025

/* global mw, $ */
mw.loader.using( [ 'mediawiki.api', 'mediawiki.util', 'mediawiki.user' ] ).then( function () {
  function run( $content ) {
    // Find the DocMeta table and owner
    var $docMeta = $content.find( '.doc-meta' ).first();
    if ( !$docMeta.length ) {
      // Try outside content as a fallback
      $docMeta = $( '.doc-meta' ).first();
      if ( !$docMeta.length ) return;
    }

    var owner = $docMeta.attr( 'data-owner' );
    var user = mw.config.get( 'wgUserName' );
    var action = mw.config.get( 'wgAction' ) || 'view';
    if ( action !== 'view' ) return;
    if ( !owner || !user || owner.toLowerCase() !== user.toLowerCase() ) return;

    function doReview() {
      var page = mw.config.get( 'wgPageName' );
      var stamp = new Date().toISOString();
      var api = new mw.Api();

      api.postWithToken( 'csrf', {
        action: 'edit',
        title: page,
        summary: 'Mark as reviewed',
        appendtext: '\n<!-- review-ping: ' + stamp + ' -->',
        minor: true,
        nocreate: true
      } ).done( function () {
        location.reload();
      } ).fail( function ( err ) {
        mw.notify( 'Review failed: ' + ( err && err.error && err.error.info ? err.error.info : err ), { type: 'error' } );
      } );
    }

    // Try to add to actions menu; fall back gracefully
    var linkId = 'ca-markreview';
    var link = mw.util.addPortletLink( 'p-cactions', '#', 'Mark as reviewed', linkId, 'Mark this page as reviewed' );
    if ( !link ) {
      link = mw.util.addPortletLink( 'p-views', '#', 'Mark as reviewed', linkId, 'Mark this page as reviewed' );
    }
    if ( !link ) {
      link = mw.util.addPortletLink( 'p-tb', '#', 'Mark as reviewed', linkId, 'Mark this page as reviewed' );
    }

    if ( link ) {
      link.addEventListener( 'click', function ( e ) {
        e.preventDefault();
        doReview();
      } );
      return;
    }

    // If no menu exists (e.g., certain skins), inject a button under the DocMeta table
    var $btn = $( '<a>' )
      .attr( 'href', '#' )
      .text( 'Mark as reviewed' )
      .css( {
        display: 'inline-block',
        marginTop: '8px',
        padding: '4px 8px',
        background: '#f0f0f0',
        border: '1px solid #ccc',
        borderRadius: '2px',
        textDecoration: 'none'
      } )
      .on( 'click', function ( e ) {
        e.preventDefault();
        doReview();
      } );

    // Insert after the table
    $docMeta.after( $( '<div>' ).append( $btn ) );
  }

  // Run on initial load and after AJAX loads
  mw.hook( 'wikipage.content' ).add( run );
} );