上一主题 下一主题
ScriptCat,新一代的脚本管理器脚本站,与全世界分享你的用户脚本油猴脚本开发指南教程目录
12下一页
返回列表 发新帖

油猴脚本使用Zustand

[复制链接]
  • TA的每日心情
    开心
    2023-12-25 06:51
  • 签到天数: 35 天

    [LV.5]常住居民I

    9

    主题

    56

    回帖

    67

    积分

    初级工程师

    积分
    67

    油中3周年

    发表于 2023-10-27 09:26:02 | 显示全部楼层 | 阅读模式
    悬赏2油猫币已解决

    本帖最后由 tansuo 于 2023-10-27 09:30 编辑

    油猴脚本使用Zustand时好像加载不到Zustand库 请问如何解决

    // ==UserScript==
    // @name         My Greasemonkey Script
    // @namespace    http://your-namespace.example/
    // @version      1.0
    // @description  Example script using Zustand in Greasemonkey
    // @match        http://127.0.0.1/*
    // @require      https://fastly.jsdelivr.net/npm/zustand@4.4.1/umd/vanilla.development.js
    // ==/UserScript==
    (function () {
        'use strict';
        console.log(`3333: `);
        // 导出Zustand库到全局对象
        const { create } = window.Zustand;
        // 创建状态存储对象
        const useStore = create((set) => ({
            count: 0,
            increment: () => set((state) => ({ count: state.count + 1 })),
            decrement: () => set((state) => ({ count: state.count - 1 })),
        }));
        // 获取当前状态
        const currentState = useStore.getState();
        console.log(currentState);
        console.log(`2222: ${currentState}`);
        // 调用操作来更新状态
        useStore.getState().increment();
        useStore.getState().increment();
        useStore.getState().decrement();
        // 获取更新后的状态
        const updatedState = useStore.getState();
        console.log(updatedState);
        console.log(`1111: ${updatedState}`);
    })();

    最佳答案

    查看完整内容

    [md]善读论坛指南 ```js (function (global, factory) { typeof exports === 'object' && typeof module !== 'undefined' ? factory(exports) : typeof define === 'function' && define.amd ? define(['exports'], factory) : (global = typeof globalThis !== 'undefined' ? globalThis : global || self, factory(global.zustandVanilla = {})); })(this, (function (exports) { 'use strict'; ``` 标准的umd格式,globa ...
  • TA的每日心情
    开心
    2023-2-28 23:59
  • 签到天数: 191 天

    [LV.7]常住居民III

    637

    主题

    5202

    回帖

    6082

    积分

    管理员

    非物质文化遗产社会摇传承人

    积分
    6082

    荣誉开发者管理员油中2周年生态建设者喜迎中秋

    发表于 2023-10-27 09:26:03 | 显示全部楼层

    善读论坛指南

    (function (global, factory) {
      typeof exports === 'object' && typeof module !== 'undefined' ? factory(exports) :
      typeof define === 'function' && define.amd ? define(['exports'], factory) :
      (global = typeof globalThis !== 'undefined' ? globalThis : global || self, factory(global.zustandVanilla = {}));
    })(this, (function (exports) { 'use strict';
    

    标准的umd格式,global是this,复制到了zustandVanilla 为对象
    所以获取对象应该是this.zustandVanilla
    导出有

      exports.createStore = createStore;
      exports.default = vanilla;
      Object.defineProperty(exports, '__esModule', { value: true });

    应该拿createStore方法
    this.zustandVanilla.createStore才是create的函数
    完整代码

    // ==UserScript==
    // @name         My Greasemonkey Script
    // @namespace    http://your-namespace.example/
    // @version      1.0
    // @description  Example script using Zustand in Greasemonkey
    // @match        https://bbs.tampermonkey.net.cn/thread-5233-1-1.html
    // @require      https://fastly.jsdelivr.net/npm/zustand@4.4.1/umd/vanilla.development.js
    // ==/UserScript==
    
    console.log(`3333: `);
    // 导出Zustand库到全局对象
    const create = this.zustandVanilla.createStore;
    // 创建状态存储对象
    const useStore = create((set) => ({
        count: 0,
        increment: () => set((state) => ({ count: state.count + 1 })),
        decrement: () => set((state) => ({ count: state.count - 1 })),
    }));
    // 获取当前状态
    const currentState = useStore.getState();
    console.log(currentState);
    console.log(`2222: ${currentState}`);
    // 调用操作来更新状态
    useStore.getState().increment();
    useStore.getState().increment();
    useStore.getState().decrement();
    // 获取更新后的状态
    const updatedState = useStore.getState();
    console.log(updatedState);
    console.log(`1111: ${updatedState}`);
    

    参考资料
    https://bbs.tampermonkey.net.cn/forum.php?mod=viewthread&tid=3054
    https://learn.scriptcat.org/docs/library/Vue%E7%9A%84%E5%BC%95%E7%94%A8/
    https://learn.scriptcat.org/docs/library/Element%20Plus%E7%9A%84%E5%BC%95%E7%94%A8/

    混的人。
    ------------------------------------------
    進撃!永遠の帝国の破壊虎---李恒道

    入驻了爱发电https://afdian.net/a/lihengdao666
    个人宣言:この世界で私に胜てる人とコードはまだ生まれていません。死ぬのが怖くなければ来てください。
    回复

    使用道具 举报

  • TA的每日心情
    开心
    2024-3-13 10:14
  • 签到天数: 211 天

    [LV.7]常住居民III

    294

    主题

    3907

    回帖

    3828

    积分

    管理员

    积分
    3828

    管理员荣誉开发者油中2周年生态建设者喜迎中秋油中3周年挑战者 lv2

    发表于 2023-10-27 09:45:10 | 显示全部楼层
    是这个对象?对这个库不了解,要么就是这个require有问题,我看了一下内容,很少

    fastly.jsdelivr.net/npm/zustand@4.4.1/umd/vanilla.development.js

    zustandVanilla
    上不慕古,下不肖俗。为疏为懒,不敢为狂。为拙为愚,不敢为恶。/ 微信公众号:一之哥哥
    回复

    使用道具 举报

  • TA的每日心情
    开心
    2023-12-25 06:51
  • 签到天数: 35 天

    [LV.5]常住居民I

    9

    主题

    56

    回帖

    67

    积分

    初级工程师

    积分
    67

    油中3周年

    发表于 2023-10-27 09:53:28 | 显示全部楼层

    如何解决那,大佬

    回复

    使用道具 举报

  • TA的每日心情
    开心
    2023-12-25 06:51
  • 签到天数: 35 天

    [LV.5]常住居民I

    9

    主题

    56

    回帖

    67

    积分

    初级工程师

    积分
    67

    油中3周年

    发表于 2023-10-27 09:54:29 | 显示全部楼层
    王一之 发表于 2023-10-27 09:45
    是这个对象?对这个库不了解,要么就是这个require有问题,我看了一下内容,很少

    fastly.jsdelivr.net/npm ...

    如何解决,大佬
    回复

    使用道具 举报

  • TA的每日心情
    开心
    2024-3-13 10:14
  • 签到天数: 211 天

    [LV.7]常住居民III

    294

    主题

    3907

    回帖

    3828

    积分

    管理员

    积分
    3828

    管理员荣誉开发者油中2周年生态建设者喜迎中秋油中3周年挑战者 lv2

    发表于 2023-10-27 10:37:29 | 显示全部楼层

    我重新打了一个包,楼主可以试试

    Zustand
    https://scriptcat.org/zh-CN/script-show-page/1350

    // @require https://scriptcat.org/lib/1350/1.0.0/Zustand.js
    // ==UserScript==
    // @name         My Greasemonkey Script
    // @namespace    http://your-namespace.example/
    // @version      1.0
    // @description  Example script using Zustand in Greasemonkey
    // @match        https://bbs.tampermonkey.net.cn/thread-5233-1-1.html
    // @require      https://scriptcat.org/lib/1350/1.0.0/Zustand.js
    // ==/UserScript==
    
    (function () {
        'use strict';
        console.log(`3333: `);
        // 导出Zustand库到全局对象
        const { create } = window.Zustand;
        // 创建状态存储对象
        const useStore = create((set) => ({
            count: 0,
            increment: () => set((state) => ({ count: state.count + 1 })),
            decrement: () => set((state) => ({ count: state.count - 1 })),
        }));
        // 获取当前状态
        const currentState = useStore.getState();
        console.log(currentState);
        console.log(`2222: ${currentState}`);
        // 调用操作来更新状态
        useStore.getState().increment();
        useStore.getState().increment();
        useStore.getState().decrement();
        // 获取更新后的状态
        const updatedState = useStore.getState();
        console.log(updatedState);
        console.log(`1111: ${updatedState}`);
    })();
    上不慕古,下不肖俗。为疏为懒,不敢为狂。为拙为愚,不敢为恶。/ 微信公众号:一之哥哥
    回复

    使用道具 举报

  • TA的每日心情
    开心
    2023-12-25 06:51
  • 签到天数: 35 天

    [LV.5]常住居民I

    9

    主题

    56

    回帖

    67

    积分

    初级工程师

    积分
    67

    油中3周年

    发表于 2023-10-27 11:39:32 | 显示全部楼层
    王一之 发表于 2023-10-27 10:37
    [md]我重新打了一个包,楼主可以试试

    Zustand

    大佬你好 这个是使用zustand@4.4.1的部分代码(微信读书),都是使用vanilla.createStore()方法创建了一个存储器对象,并使用一系列中间件对其进行处理,但我不明白如何使用,所以在网上搜索以上代码 出现好像加载不到Zustand库,用了大佬的Zustand.js的确可以加载,但我仍无法了解zustand@4.4.1使用方法,请大佬根据// @require      https://fastly.jsdelivr.net/npm/zustand@4.4.1/umd/vanilla.production.js 实现部分功能,我好学习

    // @require      https://fastly.jsdelivr.net/npm/zustand@4.4.1/umd/vanilla.production.js
    // @require      https://fastly.jsdelivr.net/npm/zustand@4.4.1/umd/middleware.production.js
      const scraperSessionStore = vanilla.createStore()(
        middleware.subscribeWithSelector(
          middleware.persist(() => scraperSessionInitialState, {
            name: "scraper-session-storage",
            storage: middleware.createJSONStorage(() => sessionStorage)
          })
        )
      );
    const scraperGMStore = vanilla.createStore()(

        middleware.subscribeWithSelector(
          middleware.persist(() => scraperGMInitialState, {
            name: "scraper-gm-storage",
            storage: middleware.createJSONStorage(() => GMStorage),
            merge: (persistedState, currentState) => {
              return {
                ...currentState,
                ...persistedState,
                booleanOptions: currentState.booleanOptions.map(
                  (currentBooleanOption) => {
                    const persistedBooleanOption = persistedState.booleanOptions.find(
                      ({ name }) => name === currentBooleanOption.name
                    );
                    if (persistedBooleanOption) {
                      return persistedBooleanOption;
                    }
                    return currentBooleanOption;
                  }
                )
              };
            }
          })
        )
      );
      const scraperPageStore = vanilla.createStore()(
        middleware.subscribeWithSelector(() => scraperPageInitialState)
      );
    回复

    使用道具 举报

  • TA的每日心情
    开心
    2024-3-13 10:14
  • 签到天数: 211 天

    [LV.7]常住居民III

    294

    主题

    3907

    回帖

    3828

    积分

    管理员

    积分
    3828

    管理员荣誉开发者油中2周年生态建设者喜迎中秋油中3周年挑战者 lv2

    发表于 2023-10-27 11:51:57 | 显示全部楼层
    我不了解这个库啊。。。也没有使用场景

    哥哥要学习的话,最好还是去看他的官方文档吧
    上不慕古,下不肖俗。为疏为懒,不敢为狂。为拙为愚,不敢为恶。/ 微信公众号:一之哥哥
    回复

    使用道具 举报

  • TA的每日心情
    开心
    2024-3-13 10:14
  • 签到天数: 211 天

    [LV.7]常住居民III

    294

    主题

    3907

    回帖

    3828

    积分

    管理员

    积分
    3828

    管理员荣誉开发者油中2周年生态建设者喜迎中秋油中3周年挑战者 lv2

    发表于 2023-10-27 11:59:20 | 显示全部楼层
    王一之 发表于 2023-10-27 10:37
    [md]我重新打了一个包,楼主可以试试

    Zustand

    重新打包主要是这里依赖了其它的包

    useSyncExternalStoreExports

    感觉还有其它更优雅的办法去解决

    ----

    找了一下,https://cdn.jsdelivr.net/npm/use-sync-external-store@1.2.0/好像没有提供umd的包
    上不慕古,下不肖俗。为疏为懒,不敢为狂。为拙为愚,不敢为恶。/ 微信公众号:一之哥哥
    回复

    使用道具 举报

  • TA的每日心情
    开心
    2023-2-28 23:59
  • 签到天数: 191 天

    [LV.7]常住居民III

    637

    主题

    5202

    回帖

    6082

    积分

    管理员

    非物质文化遗产社会摇传承人

    积分
    6082

    荣誉开发者管理员油中2周年生态建设者喜迎中秋

    发表于 2023-10-27 13:00:36 | 显示全部楼层
    顺便来一嘴
    状态库我还是更喜欢redux....
    混的人。
    ------------------------------------------
    進撃!永遠の帝国の破壊虎---李恒道

    入驻了爱发电https://afdian.net/a/lihengdao666
    个人宣言:この世界で私に胜てる人とコードはまだ生まれていません。死ぬのが怖くなければ来てください。
    回复

    使用道具 举报

    发表回复

    本版积分规则

    快速回复 返回顶部 返回列表