EcuaMobi / btct.ws shortener

// ==UserScript==
// @name        btct.ws shortener
// @namespace   ecuamobi
// @author    EcuaMobi
// @description  Shortens links to bitcointalk.org posts or profiles. For example https://bitcointalk.org/index.php?topic=3001846.msg30865584#msg30865584 => btct.ws/cAUS.25vy0
// @include     https://bitcointalk.org/index.php?topic=*
// @require     https://ajax.aspnetcdn.com/ajax/jQuery/jquery-3.3.1.min.js
// @version     0.2
// @license   MIT
// @grant     none
// ==/UserScript==
(() => {
  const alphabet = "0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ";
  const base = alphabet.length;

  function encode(num) {
    var encoded = '';
    while (num) {
      var remainder = num % base;
      num = Math.floor(num / base);
      encoded = alphabet[remainder].toString() + encoded;
    }
    return encoded;
  }

  // Posts
  $('td.td_headerandpost div[id^=ignmsgbttns] a[href*="index.php?topic="]')
    .each((pos, e) => {
      const params = /topic=(\d+)\.msg(\d+)/.exec(e.href);
      const thread = params[1];
      const post = params[2];
      const link = "btct.ws/" + encode(thread) + "." + encode(post);

      // Delete old one, if present
      $("#btct_ws" + post).remove();
      const $inputLink = $('<div id="btct_ws' + post + '" class="btct_ws" style="position: absolute; right: 40px; background-color: #' + (pos % 2 == 0 ? 'F6F6F6' : 'ECEDF3') + '; font-size: 12px; padding: 8px;border-width: 0px;border-width: 1px;border-color: #666666;border-style: solid;"><input type="text" name="btct_ws_result" id="btct_ws_result' + post + '" value="' + link + '" readonly="readonly" onClick="this.select();" style="background-color: #F6F6F6;" /><br/><input type="text" value="' + e.href + '" readonly="readonly" onClick="this.select();" style="background-color: #F6F6F6;" /></div');
      $inputLink.insertAfter(e);
      $("#btct_ws" + post).toggle(false);

      $(e).click((e) => {
        e.preventDefault();
        $("#btct_ws" + post).toggle(0);
        $("#btct_ws_result" + post).select();
        e.stopImmediatePropagation();
      });

    });

  // Profiles
  $('td.poster_info a[title*="View the profile of"]')
    .each((pos, e) => {
      const userId = /action=profile;u=(\d+)/.exec(e.href)[1];
      const userEncode = encode(userId);
      const profile = 'https://bitcointalk.org/index.php?action=profile;u=' + userId;
      const profileS = "btct.ws/~" + userEncode;
      const trust = 'https://bitcointalk.org/index.php?action=trust;u=' + userId;
      const trustS = "btct.ws/t~" + userEncode;
      const merit = 'https://bitcointalk.org/index.php?action=merit;u=' + userId;
      const meritS = "btct.ws/m~" + userEncode;

      // Delete old ones, if present
      $("#btct_ws_us_link" + pos).remove();
      $("#btct_ws_us" + pos).remove();

      var $link = $('<span id="btct_ws_us_link' + pos + '" style="font-size:10px">  (<a href="https://' + profileS + '">Link</a>)<span>');
      $link.insertAfter(e);

      const $inputLink = $('<div id="btct_ws_us' + pos + '" class="btct_ws_us" style="position: absolute; left: 40px; background-color: #' + (pos % 2 == 0 ? 'F6F6F6' : 'ECEDF3') + '; font-size: 12px; padding: 8px;border-width: 0px;border-width: 1px;border-color: #666666;border-style: solid;">' +
        '<a href="' + profile + '">Profile</a>: <input type="text" name="btct_ws_result_profile" size="12" id="btct_ws_result_profile' + pos + '" value="' + profileS + '" readonly="readonly" onClick="this.select();" style="background-color: #F6F6F6;" /><br/>' +
        '&nbsp;&nbsp;&nbsp;<a href="' + trust + '">Trust</a>: <input type="text" size="12" value="' + trustS + '" readonly="readonly" onClick="this.select();" style="background-color: #F6F6F6;" /><br/>' +
        '&nbsp;&nbsp;&nbsp;<a href="' + merit + '">Merit</a>: <input type="text" size="12" value="' + meritS + '" readonly="readonly" onClick="this.select();" style="background-color: #F6F6F6;" /><br/>' +
        '</div');
      $inputLink.insertAfter(e);
      $("#btct_ws_us" + pos).toggle(false);

      $($link).click((e) => {
        e.preventDefault();
        $("#btct_ws_us" + pos).toggle(0);
        $("#btct_ws_result_profile" + pos).select();
      });
    });
})();