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 🙏

© 2026 – Pkg Stats / Ryan Hefner

wujie-event-scope

v1.0.11

Published

处理wujie子应用HTML事件执行undefined问题

Readme

wujie-event-scope

在无界(wujie)子应用沙箱中,内联 HTML 事件(如 onclick)里的代码往往默认指向宿主全局 window,访问子应用里挂载的变量或 API 时容易出现 undefined 或跑错环境。本库在不改业务写法的前提下,把事件函数字符串改写为在子应用代理 window 上执行。

目标

  • onclick 属性element.onclick 函数 toString 两类来源的事件逻辑,在运行时使用的 window 与无界子应用一致。
  • 通过 IIFE 形参遮蔽(function(window) { … })(window.__WUJIE_xxx),使补丁里出现的 window.xxx 指向传入的代理,而不是宿主全局。
  • 函数声明、变量声明、以及裸标识符的简单赋值(如 obj = {})等会在全局产生「名字」的写法,额外生成 window.名字 = 名字,把这些绑定同步到形参 window(子应用代理)上,避免只在局部存在、代理上读不到的问题。

工作原理(简要)

  1. 表达式侧改写findIdentifiers
    Acorn 遍历代码,收集调用 callee赋值左侧等位置;对未包含 window 子串的片段前缀 window.,把对全局的访问改写成 window.xxx,与 IIFE 内形参 window 一致。

  2. 声明 / 隐式全局绑定同步findDeclarationIdentifiers
    单独扫描函数声明变量声明(含解构左侧)、以及 id = … 且左侧为裸标识符的简单赋值(脚本语义下会挂到全局对象上的名字)。
    对每个收集到的名字 x,生成一段 window.x = x; 拼接进最终可执行字符串。这样在 (function(window) { … }) 里,先执行用户逻辑产生绑定,再把同名属性挂到形参 window(无界子应用代理)上,后续通过 window.xxx 的读写才能落在同一对象上。

  3. 子应用 window 注入

    • 首次处理某个子应用 Window 时,读取 targetWindow.__WUJIE.id,生成宿主上的全局路径 __WUJIE_<规范化后的 id>(字母数字大写,见 targetToLNU),并执行 window[路径] = targetWindow
    • 路径缓存在 WeakMap<Window, string>elementDataStore),同一子应用只注册一次。
  4. 回写
    patchElementHook 会把改写后的函数体包进:
    (function(window) { … })(window.__WUJIE_xxx)
    若还需要声明同步,请把 findDeclarationIdentifiers 返回的 window.x=x;… 串在函数体前部与经 findIdentifiers 处理后的正文拼在一起,再包进同一 IIFE(与无界注入路径一致)。

使用

import wujieEventScope from "wujie-event-scope";

通过 WUJIE提供的Plugins扩展使用

源码中可直接使用:

  • findIdentifiers:表达式侧改写(window.xxx 前缀)。
  • findDeclarationIdentifiers:返回 window.x=x; 形式的声明同步片段(见 src/babelParse.ts)。二者可按需组合

前提:子应用 Window 上存在无界提供的 __WUJIE.id,用于生成稳定的全局路径名。

构建

npm install
npm run build

产物:dist/index.cjs.jsdist/index.es.js 及类型声明。

限制与注意

  • 覆盖范围findIdentifiers 只处理扫描到的调用/赋值等位置,并非任意标识符;!ast.includes('window') 为启发式判断,极端写法可能漏改或误跳过。
  • findDeclarationIdentifiers:不包含成员赋值 a.b =、复合赋值、普通表达式里的标识符等;与「声明 / 裸 x =」语义对齐,复杂场景需自行扩展。
  • element.onclick 路径:从 function … () { … } 字符串中用正则抽取函数体(fnBody),复杂嵌套或非 function 声明可能不稳定。
  • 安全:本质仍是执行字符串化后的用户代码;不可信输入场景需配合 CSP、白名单等治理,不能单靠本库视为「安全沙箱」。
  • 多子应用:通过 __WUJIE.id 区分,每个子应用对应宿主上一个 window.__WUJIE_<…> 槽位。

License

ISC