nincognito / Muffin Counter for Callme

// ==UserScript==
// @name         Muffin Counter for Callme
// @namespace    nincognito
// @version      1.0.1
// @description  Count entries in muffin.net
// @author       you
// @copyright    2020, nincognito (https://openuserjs.org/users/nincognito)
// @license      NASA-1.3

// @require      https://cdnjs.cloudflare.com/ajax/libs/babel-standalone/6.18.2/babel.js
// @require      https://cdnjs.cloudflare.com/ajax/libs/babel-polyfill/6.16.0/polyfill.js
// @require      https://ajax.googleapis.com/ajax/libs/jquery/3.4.1/jquery.min.js
// @match        naberu.com/*
// @grant        GM_xmlhttpRequest
// @connect      keksik.net
// ==/UserScript==

/* globals $ */



let FETCH_MUFFIN_CODE = false;

console.log = function () {};
let getMuffinUrl = (phone) => "https://keksik.net/phone/" + phone;
let getMuffinAnchor = (phone) => $("<a>").attr({ href: getMuffinUrl(phone), target: "_blank" }).text("🧁");
let getStatusLabel = (status) => $("<sub>").css({ "font-weight": 100 }).text(status);

let pathname = window.location.pathname;

if (pathname === "/" || pathname.split("/")[1] === "search") {
  let cardHeaders = pathname === "/" ? $(".container.mob .card-title") : $(".row .card-title");
  console.log("Cards: %s", cardHeaders.length);
  
  cardHeaders.each((i, headerEl) => {
    let profileQuickViewUrl = $(headerEl).find(".link-default").data("href");
    $.ajax({
      url: profileQuickViewUrl,
      cache: false,
      timeout: 5000,
      dataType: "html",
      headerJq: $(headerEl),
      error: (xhr, status, exception) => console.error("Ajax error %s --- %s: %s", status, i + 1, profileQuickViewUrl),
      
      success: function (htmlData, status, xhr) {
        let phoneNumber = $(htmlData).find("a.copy-phone").text().slice(-10);
        let validPhoneNumber = /^\d{10}$/.test(phoneNumber);
        if (validPhoneNumber) this.headerJq.append(getMuffinAnchor(phoneNumber));
        
        console.log("%s: %s%s %s", i + 1, (validPhoneNumber ? "" : "--- "), phoneNumber, profileQuickViewUrl);

        if (FETCH_MUFFIN_CODE) {
          var that = this;
          GM_xmlhttpRequest({
            method: "GET",
            url: getMuffinUrl(phoneNumber),
            onload: (response) => that.header.append(getStatusLabel(response.status))
          });
        }
      }

    });
  });
}
else if (/\d+/.test(pathname.split("/").slice(-1)[0])) { // card route ends with digits
  let phoneNumber = $("a.number-search").text().trim();
  $(".phone-item-large").prepend(getMuffinAnchor(phoneNumber));
}
else {
  console.error("Uknown route: %s", window.location.href);
}