Jump to content

User:GhostInTheMachine/TalkHelper1.js

From Wikipedia, the free encyclopedia
Note: After saving, you have to bypass your browser's cache to see the changes. Google Chrome, Firefox, Microsoft Edge and Safari: Hold down the ⇧ Shift key and click the Reload toolbar button. For details and instructions about other browsers, see Wikipedia:Bypass your cache.
mw.loader.using(
  "ext.gadget.CommentsInLocalTime",
  TalkHelperInit,
  function () {
    alert('CommentsInLocalTime is not enabled');
  }
);

function TalkHelperInit() {
  if (window.commentsInLocalTimeWasRun) {
    TalkHelperRun();
  } else {
    setTimeout(TalkHelperInit, 10);
  }
}

function TalkHelperRun() {
  mw.util.addCSS(`

  .localcomments {
    border-bottom: solid 2px cornflowerblue;
  }

  .talkHelperToday {
    background-color: moccasin;
    border-color: #3366cc;
    border-style: solid;
    border-width: thin;
    padding: 5px;
  }

  .talkHelperYesterday {
    background-color: cornsilk;
    border-color: #3366cc;
    border-style: dashed;
    border-width: thin;
    padding: 5px;
  }

  `);

  findposts('Yesterday');
  findposts('Today');

  function findposts(when) {
    var posts = 0;
    $.each(
      $(".localcomments:contains(', " + when + "')").parent(),
      function(){
        $(this).addClass('talkHelper' + when);
        posts++;
      }
    );

    if(posts) {
      mw.util.addPortletLink(
        'p-namespaces',
        '#',
        posts + ' ' + when.toLowerCase(),
        'ca-goto-' + when,
        'Go to first post for ' + when
      );

      $('#ca-goto-' + when).click( function(e){
        e.preventDefault();
        $('html, body').animate({scrollTop: $('.talkHelper' + when).first().offset().top - 20});
      });
    }
  }
}