Created
September 24, 2026 03:06
-
-
Save qianlifeng/bf1b14e74b6cbecd6fcc8fc6ede94f7f to your computer and use it in GitHub Desktop.
Wox.Plugin.WordCount
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| // { | |
| // "Id": "32457cd4-8e0d-4826-a9f6-1656d7197202", | |
| // "Name": "WordCount", | |
| // "Author": "qianlifeng", | |
| // "Version": "1.0.0", | |
| // "MinWoxVersion": "2.4.5", | |
| // "Runtime": "NODEJS", | |
| // "Description": "Count words and text statistics from a query or selection", | |
| // "Icon": "svg:<svg xmlns='http://www.w3.org/2000/svg' width='48' height='48' viewBox='0 0 48 48'><rect width='48' height='48' rx='12' fill='#2563eb'/><path d='M11 13h3v22h-3zM34 13h3v22h-3z' fill='#bfdbfe'/><path d='M20 17h9M20 24h8M20 31h10' fill='none' stroke='#fff' stroke-width='3' stroke-linecap='round'/></svg>", | |
| // "TriggerKeywords": ["wc", "wordcount"], | |
| // "SupportedOS": ["Windows", "Linux", "Macos"], | |
| // "Features": [{ "Name": "querySelection" }], | |
| // "I18n": { | |
| // "en_US": { | |
| // "usage_title": "Type text or select text", | |
| // "usage_subtitle": "Enter text after wc, or run wc on selected text", | |
| // "result_title": "Word Count", | |
| // "words": "%s words", | |
| // "word": "%s word", | |
| // "characters": "%s characters", | |
| // "without_spaces": "%s without spaces", | |
| // "lines": "%s lines", | |
| // "paragraphs": "%s paragraphs", | |
| // "copy_stats": "Copy statistics", | |
| // "copied": "Statistics copied" | |
| // }, | |
| // "zh_CN": { | |
| // "usage_title": "输入文本或先选中文本", | |
| // "usage_subtitle": "在 wc 后输入文本,或对选中文本运行 wc", | |
| // "result_title": "词数统计", | |
| // "words": "%s 个词", | |
| // "word": "%s 个词", | |
| // "characters": "%s 个字符", | |
| // "without_spaces": "%s 个非空白字符", | |
| // "lines": "%s 行", | |
| // "paragraphs": "%s 段", | |
| // "copy_stats": "复制统计结果", | |
| // "copied": "统计结果已复制" | |
| // } | |
| // } | |
| // } | |
| const RESULT_ICON = { ImageType: "svg", ImageData: "<svg xmlns='http://www.w3.org/2000/svg' width='48' height='48' viewBox='0 0 48 48'><rect width='48' height='48' rx='12' fill='#2563eb'/><path d='M11 13h3v22h-3zM34 13h3v22h-3z' fill='#bfdbfe'/><path d='M20 17h9M20 24h8M20 31h10' fill='none' stroke='#fff' stroke-width='3' stroke-linecap='round'/></svg>" } | |
| const COPY_ICON = { ImageType: "svg", ImageData: "<svg xmlns=\"http://www.w3.org/2000/svg\" width=\"24\" height=\"24\" viewBox=\"0 0 24 24\" fill=\"none\" stroke=\"var(--wox-theme-icon-color)\" stroke-width=\"2\" stroke-linecap=\"round\" stroke-linejoin=\"round\"><rect x=\"8\" y=\"8\" width=\"12\" height=\"12\" rx=\"2\"/><path d=\"M16 8V6a2 2 0 0 0-2-2H6a2 2 0 0 0-2 2v8a2 2 0 0 0 2 2h2\"/></svg>" } | |
| function analyzeText(text) { | |
| const normalized = text.replace(/\r\n?/gu, "\n") | |
| const words = [...new Intl.Segmenter(undefined, { granularity: "word" }).segment(text)] | |
| .filter(part => part.isWordLike).length | |
| const characters = [...new Intl.Segmenter(undefined, { granularity: "grapheme" }).segment(text)] | |
| return { | |
| wordCount: words, | |
| characterCount: characters.length, | |
| characterCountWithoutSpaces: characters.filter(part => !/\s/u.test(part.segment)).length, | |
| lineCount: text.length ? normalized.split("\n").length : 0, | |
| paragraphCount: text.trim() ? text.trim().split(/\n\s*\n/u).length : 0 | |
| } | |
| } | |
| class WordCountPlugin { | |
| constructor() { | |
| this.init = this.init.bind(this) | |
| this.query = this.query.bind(this) | |
| } | |
| async init(ctx, params) { | |
| this.api = params.API | |
| await this.api.RegisterPluginTool(ctx, { | |
| Tool: { | |
| Name: "count_text", | |
| Description: "Count words, characters, lines, and paragraphs in text", | |
| InputSchema: { | |
| type: "object", | |
| properties: { text: { type: "string" } }, | |
| required: ["text"] | |
| }, | |
| OutputSchema: { | |
| type: "object", | |
| properties: { | |
| wordCount: { type: "integer" }, | |
| characterCount: { type: "integer" }, | |
| characterCountWithoutSpaces: { type: "integer" }, | |
| lineCount: { type: "integer" }, | |
| paragraphCount: { type: "integer" } | |
| }, | |
| required: ["wordCount", "characterCount", "characterCountWithoutSpaces", "lineCount", "paragraphCount"] | |
| }, | |
| Annotations: { ReadOnly: true, Destructive: false, Idempotent: true, RequiresUI: false } | |
| }, | |
| Handler: async (_ctx, option) => ({ Output: analyzeText(option.Arguments.text) }) | |
| }) | |
| } | |
| async query(ctx, query) { | |
| let text | |
| if (query.Type === "selection") { | |
| const selection = query.Selection || {} | |
| if (selection.Type !== "text" || typeof selection.Text !== "string" || !selection.Text.trim()) { | |
| return { Results: [] } | |
| } | |
| text = selection.Text | |
| } else { | |
| text = String(query.Search || "") | |
| } | |
| if (!text.trim()) { | |
| return { Results: [{ | |
| Title: "i18n:usage_title", | |
| SubTitle: "i18n:usage_subtitle", | |
| Icon: RESULT_ICON, | |
| Score: 100 | |
| }] } | |
| } | |
| const stats = analyzeText(text) | |
| const format = async (key, value) => (await this.api.GetTranslation(ctx, key)).replace("%s", String(value)) | |
| const [title, wordLabel, charactersLabel, linesLabel, paragraphsLabel, copyLabel, copiedLabel] = await Promise.all([ | |
| this.api.GetTranslation(ctx, "result_title"), | |
| format(stats.wordCount === 1 ? "word" : "words", stats.wordCount), | |
| format("characters", stats.characterCount), | |
| format("lines", stats.lineCount), | |
| format("paragraphs", stats.paragraphCount), | |
| this.api.GetTranslation(ctx, "copy_stats"), | |
| this.api.GetTranslation(ctx, "copied") | |
| ]) | |
| const summary = [wordLabel, charactersLabel, linesLabel, paragraphsLabel].join(" · ") | |
| const result = { | |
| Id: "word-count", | |
| Title: title, | |
| SubTitle: `${wordLabel} · ${charactersLabel}`, | |
| Icon: RESULT_ICON, | |
| Score: 100, | |
| Actions: [{ | |
| Id: "copy-stats", | |
| Name: copyLabel, | |
| IsDefault: true, | |
| Icon: COPY_ICON, | |
| Action: async actionCtx => { | |
| await this.api.Copy(actionCtx, { type: "text", text: summary }) | |
| await this.api.Notify(actionCtx, copiedLabel) | |
| } | |
| }] | |
| } | |
| if (query.Type === "selection") { | |
| result.Preview = { | |
| PreviewType: "text", | |
| PreviewData: text, | |
| PreviewTags: [ | |
| { Label: wordLabel }, | |
| { Label: charactersLabel }, | |
| { Label: linesLabel }, | |
| { Label: paragraphsLabel } | |
| ] | |
| } | |
| } else { | |
| result.Tails = [ | |
| { Type: "text", Text: linesLabel }, | |
| { Type: "text", Text: paragraphsLabel } | |
| ] | |
| } | |
| return { Results: [result] } | |
| } | |
| } | |
| module.exports.plugin = new WordCountPlugin() |
qianlifeng
commented
Sep 24, 2026
Author
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment