Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

关于使用 vite-plugin-monkey脚手架搭建的react项目无法使用GM_SETVALUE的问题 #35

Closed
duoluodexiaoxiaoyuan opened this issue Dec 7, 2022 · 2 comments

Comments

@duoluodexiaoxiaoyuan
Copy link

node版本:v16.18.0 npm v8.19.2
image
image

不太懂引入的正确方式,希望可以指点一下

@duoluodexiaoxiaoyuan
Copy link
Author

看了之前别人提的issue,加上答复,问题解决了
image

因为我之前看见默认构建的时候
image

然后看下之前的issue说过这样配置有效(不知道下面的方法是否还是可行的)
image

@lisonge
Copy link
Owner

lisonge commented Dec 7, 2022

然后看下之前的issue说过这样配置有效(不知道下面的方法是否还是可行的)

你写的这个配置 不能解决 你遇到的这个问题


你的 react 代码是运行在 unsafeWindow 作用域下,这个作用域没有 GM_api ,因此会报未定义错误
这个问题有两种解决方法,我个人推荐第一种

  1. 在你的代码里头部加上 import {GM_xxx} from 'vite-plugin-monkey/dist/client'; ,由于 vite-plugin-monkey/dist/client 名字太长,我给它设置了一个默认的别名 $ ,所以用 import {GM_xxx} from '$'; 就行
  2. 设置 MonkeyOption.server.mountGmApi 为 true ,它的原理就是告诉 桥接脚本,把所有的 GM_api 全部挂载到 unsafeWindow

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants