Post
Topic
Board Meta
Merits 22 from 8 users
Re: Enhanced merit UI [1.1]
by
EcuaMobi
on 23/02/2018, 18:58:58 UTC
⭐ Merited by ETFbitcoin (6) ,xandry (4) ,dbshck (3) ,Vod (2) ,akamit (2) ,hugeblack (2) ,Welsh (2) ,Quickseller (1)
I have combined my own suggestion as well as sncc's and modified grue's script to implement them.

This is the result:
Loading image...

The available sMerit points are shown. 'Available' is a link which opens the default https://bitcointalk.org/index.php?action=merit;msg=30923337 (with the corresponding msg) in a new window to keep the original functionality.

Here's the modified source. grue, feel free to update your original code if you like the change:
Code:
// ==UserScript==
// @name        bitcointalk merit
// @namespace   grue
// @include     https://bitcointalk.org/index.php?topic=*
// @require     https://ajax.aspnetcdn.com/ajax/jQuery/jquery-3.3.1.min.js
// @version     1.1-em0.1
// @downloadURL https://grue.blob.core.windows.net/scripts/Merit.user.js?sv=2014-02-14&si=1&sr=c&sig=k%2BqstGBI3oQ8TrHfPWjS5HgjrazuDPmKJ6rYNs7rvRk%3D&.user.js
// @grant none
// ==/UserScript==

(() => {
  var sMerit;
  
  //get csrf token from the logout link
  let sc = $('td.maintab_back a[href*="index.php?action=logout;sesc="').attr("href");
  sc = /;sesc=(.*)/.exec(sc)[1];
  
  //Added by EcuaMobi: Get remaining sMerit
  $.post(
"https://bitcointalk.org/index.php?action=merit;msg=29048068"
  ).then((data) => {
    sMerit = /You have ([0-9]+)<\/b> sendable/.exec(data)[1];
  }).catch(() => sMerit = null);

  //selector for the "+Merit" link
  $('td.td_headerandpost div[id^=ignmsgbttns] a[href*="index.php?action=merit;msg="]')
  .each((i, e) => {
    const msgId = /msg=([0-9]+)/.exec(e.href)[1];
    
    const $popup = $(['
',
      '  
',
      '    
',
      '      Merit points: ',
      '    
',
 // Modified by EcuaMobi
      '    
',
      '  
',
      '
'
    ].join("\n"));
    $popup.find("form").submit( (e) => {
      e.preventDefault();
      $popup.find('input[type="submit"]')
        .prop("disabled", true)
        .val("Sending...");
      const merits = e.target.elements["merits"].value;
      
      $.post(
        "https://bitcointalk.org/index.php?action=merit",
        {merits, msgID: msgId, sc}
      ).then((data) => {
        //Error pages usually have this (rough heuristic)
        if(data.includes("An Error Has Occurred!</title")) {<br>          throw "error";<br>        }<br>        //double check and see whether the post we merited was added to the list. Its msgId should be visible in the page source.<br>        if(data.includes("#msg" + msgId)) {<br>          alert("Merit added.");<br>          $("#grue-merit-popup" + msgId).toggle(false);<br><span style="white-space: pre;"> </span><span style="white-space: pre;"> </span>  // Added by EcuaMobi<br><span style="white-space: pre;"> </span><span style="white-space: pre;"> </span>  if(sMerit!=null) { sMerit -= merits }<br>          return;<br>        }<br>        alert("Server response indeterminate.");<br>      })<br>      .catch(() => alert("Failed to add merit."))<br>      .always(() => {<br>        $popup.find('input[type="submit"]')<br>        .prop("disabled", false)<br>        .val("Send");<br>      });<br>    });<br>    $popup.insertAfter(e);<br>    <br>    $(e).click((e) => {<br>      e.preventDefault();<br>      $("#grue-merit-popup" + msgId).toggle();<br><span style="white-space: pre;"> </span>  // Added by EcuaMobi<br><span style="white-space: pre;"> </span>  if(sMerit!=null) { $("#em-smerit-count" + msgId).html('<a href="https://bitcointalk.org/index.php?action=merit;msg='+msgId+'" target="_blank">Available:</a> <b>'+sMerit+'</b>    ') };<br>    });<br>  });<br>   $(".grue-merit-popup").toggle(false);<br>   <br>})();</div>I've clearly marked the modified code with either "Added by EcuaMobi" or "Modified by EcuaMobi". I also changed the version<br><br>A small limitation is that it queries the available sMerit points once (when the thread is loaded). It does subtract them when points are sent. However, if sMerit points are received the change won't be reflected unless the page is reloaded. That would require re-querying every time which I considered an overkill.<br><br>To install this version, you can just modify grue's script or install this from scratch:<br><a class="ul" href="https://openuserjs.org/scripts/EcuaMobi/bitcointalk_merit">https://openuserjs.org/scripts/EcuaMobi/bitcointalk_merit</a><br><br>(grue, I assumed this code is open-source. If that's not the case, let me know to unpublish this)