npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

@just4/load-script

v1.2.0

Published

Just for script loading in browser.

Downloads

11

Readme

@just4/load-script

提供动态加载脚本的相关接口。

安装

npm i @just4/load-script

调用

安装完毕之后,就可以引入并调用相关的方法:

import {
  loadScript,
  jsonp
} from '@just4/load-script';

动态加载脚本文件

加载外部脚本文件,并获取其写入的全局对象:

import { loadScript } from '@just4/load-script';
await loadScript('https://code.jquery.com/jquery-1.12.4.min.js');
const $ = window.jQuery;

JSONP 请求

JSONP 本质上是通过动态加载脚本文件的方式来加载数据,调用示例如下:

import { jsonp } from '@just4/load-script';
const res = await jsonp('a jsonp url', {
  data: { id: 1 },
  timeout: 5 * 1000,
  preventCaching: true
});

备用请求

无论是加载脚本还是 jsonp,都支持通过 backupURL 指定请求的备用地址。指定了备用地址时,如果原 URL 加载失败,就会加载备用 URL。

import { loadScript } from '@just4/load-script';
await loadScript('https://code.jquery.com/jquery-1.12.4.min.js', {
  backupURL: 'https://ajax.aspnetcdn.com/ajax/jquery/jquery-1.12.4.min.js'
});
const $ = window.jQuery;

注意事项

  • 慎用 preventCaching 选项。设为 true 时,会给请求的 url 加上时间戳参数,这可能会导致后端服务缓存穿透。
  • 调用 jsonp 方法时,该方法会按照特定规则生成回调函数名,并确保该名字对应的全局变量没有被占用。如果你希望指定回调函数名,可以使用 callbackName 选项进行控制,但是这种情况下请自行确保 callbackName 对应的全局变量没有被占用。

相关文档

Changelog

v1.2.0

  • 支持备用 URL 的加载。

v1.1.0

  • 升级依赖包版本并对应调整相关 API 的调用。