[SNOLAB] NPM Typescript Flag

Show "Dt" and "Ts" Icon on npm package search result.

Від 11.11.2023. Дивіться остання версія.

You will need to install an extension such as Tampermonkey, Greasemonkey or Violentmonkey to install this script.

You will need to install an extension such as Tampermonkey or Violentmonkey to install this script.

You will need to install an extension such as Tampermonkey or Violentmonkey to install this script.

You will need to install an extension such as Tampermonkey or Userscripts to install this script.

You will need to install an extension such as Tampermonkey to install this script.

You will need to install a user script manager extension to install this script.

(У мене вже є менеджер скриптів, дайте мені встановити його!)

You will need to install an extension such as Stylus to install this style.

You will need to install an extension such as Stylus to install this style.

You will need to install an extension such as Stylus to install this style.

You will need to install a user style manager extension to install this style.

You will need to install a user style manager extension to install this style.

You will need to install a user style manager extension to install this style.

(I already have a user style manager, let me install it!)

// ==UserScript==
// @name             [SNOLAB] NPM Typescript Flag
// @namespace        https://userscript.snomiao.com/
// @version          0.1.0
// @description      Show "Dt" and "Ts" Icon on npm package search result.
// @author           [email protected]
// @match            *://www.npmjs.com/search*
// @match            *://npmjs.com/search*
// @grant            none
// @contributionURL  https://snomiao.com/donate
// @supportURL       https://github.com/snomiao/userscript.js/issues
// ==/UserScript==

const observer = new IntersectionObserver((entries) =>
  entries.forEach(async (entry) => {
    if (!entry.isIntersecting) return;
    const h3 = entry.target;
    if (h3.ariaChecked) return;
    //const rect = h3.getBoundingClientRect();
    //const isVisible = 0 < rect.bottom  && rect.top < window.innerHeight;
    // if(!isVisible) return;
    const pathname = h3.parentElement.pathname;
    const content = await (
      await fetch(pathname, { cache: "force-cache" })
    ).text();
    const hasDt =
      content.match(
        '(?<="true">)<img.*?This package has TypeScript declarations provided by.*?/>'
      )?.[0] ?? "";
    const hasTs =
      content.match(
        '(?<="true">)<img.*?This package contains built-in TypeScript declarations.*?/>'
      )?.[0] ?? "";
    h3.innerHTML = h3.innerText + " / " + hasDt + hasTs;
    console.log({ pathname, hasDt, hasTs });
    h3.ariaChecked = true;
  })
);

function $$(sel, el = document) {
  return [...el.querySelectorAll(sel)];
}

$$("a>h3").forEach(async (h3) => observer.observe(h3));