Free NPM Explore

Allow exploring NPM packages for free

您需要先安裝使用者腳本管理器擴展,如 TampermonkeyGreasemonkeyViolentmonkey 之後才能安裝該腳本。

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

您需要先安裝使用者腳本管理器擴充功能,如 TampermonkeyViolentmonkey 後才能安裝該腳本。

您需要先安裝使用者腳本管理器擴充功能,如 TampermonkeyUserscripts 後才能安裝該腳本。

你需要先安裝一款使用者腳本管理器擴展,比如 Tampermonkey,才能安裝此腳本

您需要先安裝使用者腳本管理器擴充功能後才能安裝該腳本。

(我已經安裝了使用者腳本管理器,讓我安裝!)

你需要先安裝一款使用者樣式管理器擴展,比如 Stylus,才能安裝此樣式

你需要先安裝一款使用者樣式管理器擴展,比如 Stylus,才能安裝此樣式

你需要先安裝一款使用者樣式管理器擴展,比如 Stylus,才能安裝此樣式

你需要先安裝一款使用者樣式管理器擴展後才能安裝此樣式

你需要先安裝一款使用者樣式管理器擴展後才能安裝此樣式

你需要先安裝一款使用者樣式管理器擴展後才能安裝此樣式

(我已經安裝了使用者樣式管理器,讓我安裝!)

// ==UserScript==
// @match       https://www.npmjs.com/package/*
// @match       https://js.stripe.com/v3/controller-3d240f15c7d33335abc8bc509e1f19ad.html*
// @name        Free NPM Explore
// @description Allow exploring NPM packages for free
// @grant       GM.addElement
// @version     0.1.0
// @author      KaKi87
// @license     GPL-3.0-or-later
// @namespace   https://git.kaki87.net/KaKi87/userscripts/src/branch/master/FreeNpmExplore
// ==/UserScript==

if(window.location.href.startsWith('https://www.npmjs.com/package/')){

    let iframe;

    document.querySelector('#package-tab-explore').addEventListener('click', async event => {
        await new Promise(resolve => {
            const _ = () => {
                const el = document.querySelector('#tabpanel-explore div');
                if(el)
                    resolve(el.setAttribute('style', 'display: none;'));
                else
                    window.requestAnimationFrame(_);
            };
            _();
        });
        if(iframe)
            iframe.style.display = 'block';
        else {
            const [, package, version] = window.location.href.match(/^https:\/\/www\.npmjs\.com\/package\/(.+?)(?:\/v\/(.+))?$/);
            iframe = document.createElement('iframe');
            iframe.setAttribute('src', `https://js.stripe.com/v3/controller-3d240f15c7d33335abc8bc509e1f19ad.html?${new URLSearchParams({ package, version })}`);
            iframe.setAttribute('style', 'border: none; width: 100%; height: 40rem');
        }
        if(!document.body.contains(iframe))
            document.querySelector('#tabpanel-explore').appendChild(iframe);
    });

    document.querySelectorAll('[id^="package-tab"]:not(#package-tab-explore)').forEach(el => el.addEventListener('click', () => {
        if(iframe)
            iframe.style.display = 'none';
    }));

}

else if(window.location.href.startsWith('https://js.stripe.com/v3/controller-3d240f15c7d33335abc8bc509e1f19ad.html')){

    if(window.self !== window.top){
        const
            sharedCss = 'position: absolute; top: 0; left: 0; width: 100vw; height: 100vh;',
            splash = document.createElement('div');
        splash.setAttribute('style', sharedCss + 'z-index: 1; background-color: black; display: flex; justify-content: center; align-items: center; font-size: 2.5rem');
        splash.innerText = 'Loading UNPKG...';
        document.body.appendChild(splash);
        const
            {
                package,
                version
            } = Object.fromEntries(new URL(window.location.href).searchParams.entries()),
            iframe = document.createElement('iframe');
        iframe.setAttribute('src', `https://unpkg.com/browse/${package}${version === 'undefined' ? '' : `@${version}`}/`);
        iframe.setAttribute('style', sharedCss + 'border: none; z-index: 2');
        document.body.appendChild(iframe);
    }

}