MediaWiki:Gadget-GrAnnotations.js: Difference between revisions

No edit summary
No edit summary
Line 349: Line 349:
     var author = currentUser
     var author = currentUser
       || ( $cmpName.length ? $cmpName.val().trim() : '' )
       || ( $cmpName.length ? $cmpName.val().trim() : '' )
       || 'अतिथि';
       || 'Guest';


     var id    = uid();
     var id    = uid();
Line 361: Line 361:
     _comments.push( entry );
     _comments.push( entry );
     persistCommentHighlight( id, quote );
     persistCommentHighlight( id, quote );
     saveCommentToWiki( id, author, quote, text, ts );
     saveCommentToWiki( id, author, quote, text, ts, id );
     notifyByEmail( id, author, quote, text, ts );
     notifyByEmail( id, author, quote, text, ts );


Line 369: Line 369:
   }
   }


   function saveCommentToWiki( id, author, quote, text, ts ) {
   function saveCommentToWiki( id, author, quote, text, ts, anchorId ) {
     if ( !window.mw ) return;
     if ( !window.mw ) return;
     var api = new mw.Api();
     var api = new mw.Api();
Line 394: Line 394:
         summary:'Comment — ' + author,
         summary:'Comment — ' + author,
         bot:0,
         bot:0,
      }).then(function(){
        // Silently notify admin by appending a compact notice to their Talk page.
        // MediaWiki Echo picks this up and sends an email to whoever watches that page.
        // This is completely invisible to the commenter — no browser hijack.
        var adminTalk = 'User_talk:Chandrashekars';
        var artPath  = (mw.config.get('wgArticlePath')||'/wiki/$1');
        var link      = window.location.origin
                      + artPath.replace('$1', pageTitle)
                      + (anchorId ? '#' + anchorId : '');
        var notice = '\n<!-- grantha-comment-notify -->\n'
                  + '; [[' + pageTitle.replace(/_/g,' ') + ']] — ' + author + '\n'
                  + ': ' + quote.slice(0,80) + '\n'
                  + ': ' + link + '\n';
        new mw.Api().postWithEditToken({
          action:'edit', title:adminTalk, section:'new',
          sectiontitle:'New comment on ' + pageTitle.replace(/_/g,' '),
          text:notice,
          summary:'Comment notification',
          bot:1,  /* bot=1 suppresses Echo "someone edited your talk page" popup
                      but still triggers email watchlist notifications */
        }).catch(function(){});  /* silent — never block the comment flow */
       }).catch(function(){});
       }).catch(function(){});
     }).catch(function(){});
     }).catch(function(){});
Line 399: Line 420:


   function notifyByEmail( anchorId, author, quote, text, ts ) {
   function notifyByEmail( anchorId, author, quote, text, ts ) {
     var artPath = (window.mw ? mw.config.get('wgArticlePath') : '/wiki/$1') || '/wiki/$1';
     /* Notification is handled server-side by MediaWiki's Echo extension
    var anchorLink = window.location.origin
    * when saveCommentToWiki() edits the Talk page no client-side
                  + artPath.replace('$1', pageTitle)
     * mailto needed. A mailto would abruptly open the user's mail app
                  + (anchorId ? '#' + anchorId : '');
    * mid-session which breaks the commenting flow entirely. */
    var pageDisplay = pageTitle.replace(/_/g,' ');
 
    var subject = encodeURIComponent('[Grantha] Comment "' + pageDisplay + '"');
    var body    = encodeURIComponent(
      'Page    : ' + pageDisplay + '\n'
    + 'Author  : ' + author      + '\n'
    + 'Time    : ' + ts          + '\n'
    + 'Passage : "' + quote      + '"\n\n'
    + 'Comment :\n' + text      + '\n\n'
    + '──────────────────────────────────\n'
    + 'Link: ' + anchorLink     + '\n'
    + 'Comments page: ' + window.location.origin
        + artPath.replace('$1', 'Talk:' + pageTitle + '/GrComments')
    );
 
    // Always notify via mailto to feedback@anandamakaranda.in
    var a = document.createElement('a');
    a.href = 'mailto:' + FEEDBACK_EMAIL + '?subject=' + subject + '&body=' + body;
    a.target = '_blank';
    a.rel = 'noopener';
    document.body.appendChild(a);
    a.click();
    document.body.removeChild(a);
   }
   }


Line 799: Line 797:
     restoreBookmarkHighlights();
     restoreBookmarkHighlights();
     restoreCommentHighlights();
     restoreCommentHighlights();
     loadComments(function(){
     loadComments(function(){}); // pre-load comments in background
      if (_comments.length > 0) {
        $tabComments.find('.gra-icon').after(
          ' <span style="background:#e53935;color:#fff;border-radius:9px;font-size:10px;padding:0 5px;margin-left:2px;">'
          + _comments.length + '</span>'
        );
        var $badge = $( '#gra-toggle-badge' );
        if ($badge.length) $badge.text(_comments.length).css('display','flex');
      }
    });
   });
   });


}() );
}() );