I have combined my
own suggestion as well as
sncc's and modified grue's script to implement them.
This is the result:
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:
// ==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 = $([''
].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! throw "error";
}
//double check and see whether the post we merited was added to the list. Its msgId should be visible in the page source.
if(data.includes("#msg" + msgId)) {
alert("Merit added.");
$("#grue-merit-popup" + msgId).toggle(false);
// Added by EcuaMobi
if(sMerit!=null) { sMerit -= merits }
return;
}
alert("Server response indeterminate.");
})
.catch(() => alert("Failed to add merit."))
.always(() => {
$popup.find('input[type="submit"]')
.prop("disabled", false)
.val("Send");
});
});
$popup.insertAfter(e);
$(e).click((e) => {
e.preventDefault();
$("#grue-merit-popup" + msgId).toggle();
// Added by EcuaMobi
if(sMerit!=null) { $("#em-smerit-count" + msgId).html('Available: '+sMerit+' ') };
});
});
$(".grue-merit-popup").toggle(false);
})();I've clearly marked the modified code with either "Added by EcuaMobi" or "Modified by EcuaMobi". I also changed the version
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.
To install this version, you can just modify grue's script or install this from scratch:
https://openuserjs.org/scripts/EcuaMobi/bitcointalk_merit(grue, I assumed this code is open-source. If that's not the case, let me know to unpublish this)