Skip to content

Conversation

@cyfung1031
Copy link
Collaborator

@cyfung1031 cyfung1031 commented Oct 1, 2025

概述

#781

  1. 使用 section sign (§) 为原本的API增加二级菜单功能
  2. 加入分隔线功能。空名字为分隔线
  3. 不影响原有功能。兼容TM

例子

// ==UserScript==
// @name         GM_registerMenuCommand Example
// @namespace    http://tampermonkey.net/
// @version      1.0
// @description  Simple demo for GM_registerMenuCommand
// @match        *://*/*
// @grant        GM_registerMenuCommand
// ==/UserScript==

(function() {
    'use strict';

    GM_registerMenuCommand("Say Hello", () => {
        alert("Hello from the userscript!");
    });

    GM_registerMenuCommand("Log to Console", () => {
        console.log("Menu command clicked!");
    });


// 二级
    GM_registerMenuCommand("§ I am outside", () => {
        alert("I am outside");
    });

// 二级,分隔线
    GM_registerMenuCommand("§", () => {
    });

// 二级
    GM_registerMenuCommand("§ I am outside 2", () => {
        alert("I am outside 2");
    });



// 三级
    GM_registerMenuCommand("I am inside", () => {
        alert("I am inside");
    });

// 三级,分隔线
    GM_registerMenuCommand("", () => {
    });

// 三级
    GM_registerMenuCommand("I am inside 2", () => {
        alert("I am inside 2");
    });

})();

变更内容

调整 src/pages/components/ScriptMenuList/index.tsx
对 src/app/service/service_worker/popup.ts 修改增加功能

截图

Screenshot 2025-10-01 at 18 37 31 Screenshot 2025-10-01 at 18 44 55

@CodFrm
Copy link
Member

CodFrm commented Oct 1, 2025

§ 太抽象了吧,我觉得不如增加一个参数

@CodFrm CodFrm requested a review from Copilot October 1, 2025 12:58
Copy link
Contributor

Copilot AI left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull Request Overview

This PR implements submenu and separator functionality for the GM_registerMenuCommand API using section sign (§) notation and empty names. It maintains compatibility with existing scripts while adding new hierarchical menu structure capabilities.

  • 新增了使用 § 符号创建二级菜单的功能
  • 增加了使用空名称创建分隔线的功能
  • 重构了菜单生成逻辑以支持层级结构

Reviewed Changes

Copilot reviewed 2 out of 2 changed files in this pull request and generated 4 comments.

File Description
src/pages/components/ScriptMenuList/index.tsx 更新菜单项组件以处理§前缀并隐藏空名称的分隔线菜单
src/app/service/service_worker/popup.ts 重构菜单生成逻辑以支持二级菜单和分隔线功能

title: menu.name,
const subMenuEntries = [] as chrome.contextMenus.CreateProperties[];
let withMenuItem = false;
// eslint-disable-next-line prefer-const
Copy link

Copilot AI Oct 1, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

使用 eslint-disable 注释来绕过 prefer-const 规则不是最佳实践。既然需要修改 name 变量,应该使用 let 声明并移除这个 eslint-disable 注释。

Suggested change
// eslint-disable-next-line prefer-const

Copilot uses AI. Check for mistakes.
Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

不要

// 如果是带输入框的菜单则不在页面内注册
if (options?.inputType) return;
let level = 3;
if (name[0] === "\xA7") {
Copy link

Copilot AI Oct 1, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

使用十六进制转义序列 \xA7 不如直接使用字符 § 清晰易读。建议改为 if (name[0] === '§') { 以提高代码可读性。

Suggested change
if (name[0] === "\xA7") {
if (name[0] === "§") {

Copilot uses AI. Check for mistakes.
Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

避免build后脚本代码包括非通用ASCII字符

}
})();

const menuName = menu.name.replace(/^\xA7+/, "").trim();
Copy link

Copilot AI Oct 1, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

与上面相同,这里的正则表达式使用 \xA7 不如使用 § 字符清晰。建议改为 /^§+/ 以提高代码可读性。

Suggested change
const menuName = menu.name.replace(/^\xA7+/, "").trim();
const menuName = menu.name.replace(/^§+/, "").trim();

Copilot uses AI. Check for mistakes.
Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

避免build后脚本代码包括非通用ASCII字符

@cyfung1031
Copy link
Collaborator Author

cyfung1031 commented Oct 1, 2025

§ 太抽象了吧,我觉得不如增加一个参数

不会吧
加参数影响跟TM的兼容

原来 GM_registerMenuCommand 有 options.
可以用 options 实现


加了 options.nested 3abd3f9

@cyfung1031 cyfung1031 marked this pull request as draft October 1, 2025 15:02
@CodFrm
Copy link
Member

CodFrm commented Oct 9, 2025

这个PR还在草稿中,是等 #790 吗?

@cyfung1031
Copy link
Collaborator Author

这个PR还在草稿中,是等 #790 吗?

是。
790 代码改动大,这个785要重新修改吧
这个是 加功能 ,不急

@cyfung1031
Copy link
Collaborator Author

cyfung1031 commented Oct 12, 2025

見 PR #831

@cyfung1031 cyfung1031 closed this Oct 12, 2025
@cyfung1031 cyfung1031 deleted the ui-rightclick branch October 18, 2025 15:28
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

Successfully merging this pull request may close these issues.

2 participants