lisonge / vite-plugin-monkey

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

不支持 GM_cookie #6

Closed imbytecat closed 2 years ago

imbytecat commented 2 years ago

在用的 Tampermonkey BETA 的一个特性,似乎这里不支持。

GM_cookie_not_supported

lisonge commented 2 years ago

@imbytecat

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

// https://vitejs.dev/config/
export default defineConfig(({ command, mode }) => ({
  plugins: [
    monkey({
      entry: 'src/main.ts',
      userscript: {
        name: 'name',
        namespace: 'https://github.com/lisonge',
        version: '1.0.1',
        grant: ['GM.addElement'],
        $extra: {
          grant: ['GM_cookie'],
        },
      },
    }),
  ],
}));
lisonge commented 2 years ago

感谢提醒,后续版本会添加类型提示

imbytecat commented 2 years ago

即使在 $extra 中添加了对应的方法似乎也无法正常使用吗?我在浏览器中调用的时候遇到了一些问题:

GM_cookie_is_not_defined

另外,这些内置方法暂时还没有类型提示吗,还是需要另外引入 types 文件?我这边 TypeScript 全部报错了,好难过 o(╥﹏╥)o

cannot_use_internal_functions

lisonge commented 2 years ago

@imbytecat

第一个问题,注入GM的方法是固定的,你需要手动需要更换注入模板,我一会会修复这个

第二个问题,类型声明在这个本库的package.json 的peers依赖

pnpm add -D @types/tampermonkey
imbytecat commented 2 years ago

第一个问题,注入GM的方法是固定的,你需要手动需要更换注入模板,我一会会修复这个

第二个问题,类型声明在这个本库的package.json 的peers依赖

谢谢,第二个问题安装了 @types/tampermonkey 就没问题了。

很棒的项目,已 Star!

lisonge commented 2 years ago

fixed -> https://github.com/lisonge/vite-plugin-monkey/blob/v1.1.4/packages/vite-plugin-monkey/CHANGELOG.md#bug-fixes 由于 GM_cookie 属于 beta 的特性,故没有添加 GM_cookie 的类型注释,仍然需要通过以下方式添加

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

export default defineConfig(({ command, mode }) => ({
  plugins: [
    monkey({
      entry: 'src/main.ts',
      userscript: {
        name: 'name',
        namespace: 'https://github.com/lisonge',
        version: '1.0.1',
        grant: ['GM.addElement'],
        $extra: {
          grant: ['GM_cookie'],
        },
      },
    }),
  ],
}));

vite serve 模式下,会将 以下字段和 $extra.grant 添加的字段 获取到的对象挂载到 unsafeWindow

[
        'GM',
        'GM_addElement',
        'GM_addStyle',
        'GM_addValueChangeListener',
        'GM_deleteValue',
        'GM_download',
        'GM_getResourceText',
        'GM_getResourceURL',
        'GM_getTab',
        'GM_getTabs',
        'GM_getValue',
        'GM_info',
        'GM_listValues',
        'GM_log',
        'GM_notification',
        'GM_openInTab',
        'GM_registerMenuCommand',
        'GM_removeValueChangeListener',
        'GM_saveTab',
        'GM_setClipboard',
        'GM_setValue',
        'GM_unregisterMenuCommand',
        'GM_xmlhttpRequest',
      ]
lisonge commented 2 years ago

supported by https://github.com/lisonge/vite-plugin-monkey/commit/a603ae3750a86f3193d8a3cbfb9310d793a776e8

v2 预发布:GM_api 现在可以通过 ESM 导入的方式使用,自带类型提示/文档

综合了 tampermonkey/violentmonkey/greasemonkey 三者的 类型提示

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

GM_cookie.list({}, (cookies, error) => {
  if (error) {
    console.log(error);
  } else {
    const [cookie] = cookies;
    if (cookie) {
      console.log(cookie);
      // {
      //   "domain": "i.songe.li",
      //   "httpOnly": false,
      //   "secure": false,
      //   "name": "k",
      //   "path": "/",
      //   "sameSite": "unspecified",
      //   "value": "v",
      //   "session": true,
      //   "hostOnly": true
      // }
    }
  }
});
lisonge commented 2 years ago

fixes by https://github.com/lisonge/vite-plugin-monkey/blob/v2.0.0/packages/vite-plugin-monkey/CHANGELOG.md#200-features