GM_registerMenuCommand()注册菜单模板,TemperMonkey油猴脚本开发
注册菜单
- 点击事件
- 注册实例
- 设置默认值
- 将handle(手握实例的变量)赋值给一个变量
function menu_Func_click() { GM_setValue('Func', !GM_getValue('Func')); // 开关 GM_unregisterMenuCommand(menu_Func); //卸载再注册 // 强制等待下一个事件循环 setTimeout(() => { menu_Func = menu_Func_regist(); }, 0); }; function menu_Func_regist() { return GM_registerMenuCommand( `${GM_getValue('Func') ? '✅' : '❌'}`, menu_Func_click, { accessKey: 'f', autoClose: false } ); }; if (GM_getValue('Func') === undefined) GM_setValue('Func', true); let menu_Func = menu_Func_regist();
别的脚本采用了全部刷新的策略,用list然后全部遍历更新
问题:脚本加载了不止1次,但我希望只运行一次
https://github.com/Tampermonkey/tampermonkey/issues/1730
https://github.com/Tampermonkey/tampermonkey/issues/1279
解决:添加@noframes
chrome系利用@require
调试本地脚本
https://www.cnblogs.com/hyaray/p/7509572.html
// ==UserScript== // @name Local Script DEBUG // @namespace http://tampermonkey.net/ // @version 2024-10-08 // @description try to take over the world! // @author You // @match https://*/* // @icon data:image/gif;base64,R0lGODlhAQABAAAAACH5BAEKAAEALAAAAAABAAEAAAICTAEAOw== // @grant GM_registerMenuCommand // @grant GM_unregisterMenuCommand // @grant GM_setValue // @grant GM_getValue // @grant GM_deleteValue // @require file:///home/root/my_local_script.js // ==/UserScript== (function() { 'use strict'; // Your code here... })();
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
2023-10-08 异步中.then()的坑爹