lisonge / vite-plugin-monkey

A vite plugin server and build your.user.js for userscript engine like Tampermonkey, Violentmonkey, Greasemonkey, ScriptCat
MIT License
1.39k stars 71 forks source link

reactv18.2.0 work in chrome v122, also has window.event = windowEvent; error. #140

Closed mouday closed 8 months ago

mouday commented 8 months ago

reactv18.2.0 work in chrome v122, also has this error.

window.event = windowEvent;

TypeError: Cannot set property event of # which has only a getter

mouday commented 8 months ago

破案了

解决方法:取消自动授权,让js运行在window环境中,而非unsafeWindow

monkey({
    userscript: {
            grant: 'none',
  },
        build: {
          autoGrant: false,
  }
})

最后打包出的产物

// @grant      none

昨日遇到的坑。当脚本中调用window时,若grant unsafeWindow,则window是tampermonkey虚拟出来的一个不完全的window,里面东西少很多。

若要使用完整window,则要grant none。

参考:

lisonge commented 8 months ago

这个 window.event = windowEvent; 是哪个框架的代码?

如果是库的话,写个 vite plugin 在这些用到 window 的代码文件上加一个 import {unsafeWindow as window} from '$' 就行

具体也可参考 https://github.com/lisonge/vite-plugin-monkey/issues/139