lisonge / vite-plugin-monkey

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

[Bug] GM is not defined #142

Closed zhuozhiyongde closed 3 months ago

zhuozhiyongde commented 3 months ago
GM.xmlHttpRequest({
    method: 'GET',
    url: downloadLink,
    responseType: 'blob',
    onload: function (response) {
        const blob = response.response;
        const url = URL.createObjectURL(blob); // 为Blob对象创建一个临时URL
        const a = document.createElement('a'); // 创建一个a标签
        a.href = url;
        a.download = fileName;
        document.body.appendChild(a); // 将a标签添加到文档中
        a.click(); // 模拟点击a标签以触发下载
        document.body.removeChild(a); // 下载后移除a标签
        URL.revokeObjectURL(url); // 释放URL对象
    },
    onerror: function (err) {
        console.error(err);
    },
});

以上是脚本 main.js 内容,已经开了授权(自动开启的),但是无效。

zhuozhiyongde commented 3 months ago

即使改变 API,依旧报错:

ReferenceError: GM_xmlhttpRequest is not defined

zhuozhiyongde commented 3 months ago

测试平台:chrome,tampermonkey

lisonge commented 3 months ago

你最好先查看文档了解如何正确使用 GM api

而不是直接发 issue

zhuozhiyongde commented 3 months ago

抱歉,可能是我表述的不清楚

是这样的,我在开发 dev 模式下确实无法使用这个 API,无论是现在这种写法还是 GM_xmlHttpRequdst 都不行,但是同样的代码我直接不打包放入一个新的脚本,却能正常工作。这让我感到很迷惑,所以来此提问orz

lisonge commented 3 months ago

你好,你确定你是按照文档里的 写法

import { GM_cookie, unsafeWindow, monkeyWindow, GM_addElement } from '$';

这样引入之后仍然报错吗

zhuozhiyongde commented 3 months ago

我使用了 image 这个写法,仍然报错 image

zhuozhiyongde commented 3 months ago

image

zhuozhiyongde commented 3 months ago

这个是换了那个引入写法之后依然报错

lisonge commented 3 months ago

麻烦上传一个最小复现的 demo.zip 看看

zhuozhiyongde commented 3 months ago

image

使用最新的 pnpm create monkey 直接创建然后修改的结果

zhuozhiyongde commented 3 months ago

vite-plugin-monkey-bug.zip

lisonge commented 3 months ago

image

看起来是你的浏览器扩展的问题

zhuozhiyongde commented 3 months ago

我使用的是最新的 5.1.0 的 TamperMonkey

lisonge commented 3 months ago

image

这个是符合预期的,因为正确的变量名是 GM_xmlhttpRequest 不是 GM_xmlHttpRequest

lisonge commented 3 months ago

我不知道为什么脚本作用域的 window 上现在无法动态获取到 GM_api 了,看起来和 https://github.com/Tampermonkey/tampermonkey/issues/1567 相关

你的问题可以通过如下的代码暂时解决

import { defineConfig } from 'vite';
import monkey from 'vite-plugin-monkey';

export default defineConfig({
  plugins: [
    monkey({
      entry: 'src/main.tsx',
      userscript: {
        icon: 'https://vitejs.dev/logo.svg',
        namespace: 'npm/vite-plugin-monkey',
        match: ['https://www.google.com/'],
      },
      format: {
        generate(uOptions) {
          if (uOptions.mode == 'serve') {
            return (
              uOptions.userscript +
              '\n' +
              ['unsafeWindow', 'GM', 'GM_xmlhttpRequest'] // your userscript API from window
                .map((v) => `unsafeWindow.${v}=${v}`)
                .join(';\n')
            );
          }
          return uOptions.userscript;
        },
      },
    }),
  ],
});
lisonge commented 3 months ago

https://github.com/lisonge/vite-plugin-monkey/releases/tag/v3.5.2

zhuozhiyongde commented 3 months ago

感谢大佬,最新版已经可用。

zikkurat commented 2 months ago

大佬,解决了没有哇?! 使用最新版了,v3.5.2 用的是Violentmonkey(暴力猴) 输出:[vite-plugin-monkey] mount 0/25 GM_api to unsafeWindow