QOwnNotes 脚本系统 Hooks 全解析:22 个事件钩子的调用时机、参数与实战示例

原创2026-10-15 09:21:141,346 阅读
文章标签:桌面应用知识管理富文本插件系统跨平台

QOwnNotes 脚本系统 Hooks 全解析:22 个事件钩子的调用时机、参数与实战示例

本指南以 QOwnNotes 官方脚本文档中的 Hooks 章节为核心,系统讲解 QOwnNotes 脚本引擎暴露的全部 22 个事件钩子(Hook)。这些钩子以约定的 QML 函数名形式存在,脚本引擎会在笔记存储、笔记打开、媒体插入、标签增删、Markdown 预览生成、文本加密、AI 后端配置加载等关键节点自动回调脚本。读完本文,你将掌握每个 Hook 的准确函数签名、参数语义、返回值约定,以及如何借助 docs/scripting/examples/ 下的官方示例快速实现自定义笔记工作流。

一、Hooks 机制与脚本定义方式

QOwnNotes 的脚本基于 QML(Qt Meta Language),脚本文件以 .qml 结尾,通常以 import QtQml 2.0 和 import QOwnNotesTypes 1.0 开头,主体是一个 QtObject 或 Script 对象。所谓 "Hook",就是在脚本对象中定义一个名字固定的函数,脚本引擎会在特定事件发生时通过反射机制调用它。

从源码实现看,脚本引擎的调用逻辑集中在 src/services/scriptingservice.cpp:

  • 引擎通过 methodExistsForObject(object, "hookName(参数签名)") 检测脚本对象是否定义了对应函数(例如 "onNoteStored(QVariant)"、"noteOpenedHook(QVariant)");
  • 检测通过后,使用 QMetaObject::invokeMethod() 反射调用脚本函数,并传入 QVariant 类型的参数;
  • 每次调用都会经过 logScriptHookTiming() 记录耗时,便于在脚本日志中排查性能问题。

因此,只要在脚本对象中按文档约定定义同名函数即可被自动识别,无需注册或声明。需要注意的是,Hook 名称及其参数个数是约定好的,定义错误(如参数数量不匹配)将导致 methodExistsForObject 检测失败、该脚本的 Hook 静默失效。

二、笔记生命周期 Hooks

这类 Hook 覆盖笔记从创建、打开、存储到在列表中显示名称的完整生命周期。

onNoteStored

笔记被写入磁盘后调用。文档明确指出:你不能在此修改已存储的笔记——因为你很可能正在同时手工编辑它们,修改会造成混乱。

/**
 * This function is called when a note gets stored to disk
 * You cannot modify stored notes, that would be a mess since
 * you are most likely editing them by hand at the same time
 *
 * @param {NoteApi} note - the note object of the stored note
 */
function onNoteStored(note);

典型用法是日志记录或触发后续动作。参见示例 on-note-opened.qml,以及 example.qml 中对 note.id、note.name、note.fileName 和 note.tagNames() 的打印演示。

noteOpenedHook

笔记被打开之后调用,常用于读取笔记内容、注册通知等。

/**
 * This function is called after a note was opened
 *
 * @param {NoteApi} note - the note object that was opened
 */
function noteOpenedHook(note);

示例 on-note-opened.qml 展示了通过 note.fullNoteFilePath 记录笔记完整路径的做法。源码中该 Hook 的调用见 src/services/scriptingservice.cpp 中 noteOpenedHook(QVariant) 的检测与分发逻辑。

noteDoubleClickedHook

笔记列表中的笔记被双击之后调用,典型场景是把双击行为重定向到外部编辑器。

/**
 * This function is called after a note was double clicked
 *
 * @param {NoteApi} note - the note object that was clicked
 */
function noteDoubleClickedHook(note);

示例 external-note-open.qml 实现了完整的"双击用外部编辑器打开"流程:它通过 settingsVariables 注册一个 executablePath 设置项,然后在 Hook 内调用 script.startDetachedProcess(executablePath, [note.fullNoteFilePath]) 启动外部程序。

handleNoteNameHook

决定笔记在列表中显示名称时调用,允许修改展示名称。

/**
 * This function is called when the note name is determined for a note
 *
 * It allows you to modify the name of the note that is viewed
 *
 * Return an empty string if the name of the note should not be modified
 *
 * @param {NoteApi} note - the note object of the stored note
 * @return {string} the name of the note
 */
function handleNoteNameHook(note);

文档提示该名称主要用于列表展示;如果开启了"使用文件名作为笔记名"的设置,则不建议使用此 Hook。源码侧,ScriptingService::handleNoteNameHookExists()(见 src/services/scriptingservice.cpp)被 src/managers/noteindexmanager.cpp 用来决定索引构建时是否调用该 Hook。示例 example.qml 中简单返回 note.name + " some text" 演示了追加文本的效果。

handleNewNoteHeadlineHook

新笔记创建之前调用,用于修改标题,是实现笔记模板的核心 Hook。

/**
 * This function is called before a note is created
 *
 * It allows you to modify the headline of the note before it is created
 * Note that you have to take care about a unique note name, otherwise
 * the new note will not be created, it will just be found in the note list
 *
 * You can use this function for creating note templates
 *
 * @param headline text that would be used to create the headline
 * @return {string} the headline of the note
 */
function handleNewNoteHeadlineHook(headline);

文档特别警告:你需要自己保证笔记名唯一,否则新笔记不会真正创建,只会出现在笔记列表中。示例 custom-new-note-headline.qml 演示了在原标题后追加自定义文本并生成 = 下划线(Markdown H1 的 Setext 写法)的模板效果。

handleNoteTextFileNameHook

在启用"允许笔记文件名与标题不同"设置时,笔记存储到磁盘前调用,用于自定义文件名。

/**
 * This function is called when a note gets stored to disk if
 * "Allow note file name to be different from headline" is enabled
 * in the settings
 *
 * It allows you to modify the name of the note file
 * Keep in mind that you have to care about duplicate names yourself!
 *
 * Return an empty string if the file name of the note should
 * not be modified
 *
 * @param {NoteApi} note - the note object of the stored note
 * @return {string} the file name of the note
 */
function handleNoteTextFileNameHook(note);

同样需要自己处理重名问题。示例 use-tag-names-in-filename.qml 展示了完整实战:以笔记首行作为文件名基础,再把 note.tagNames() 得到的标签名拼接为 [tag1] [tag2] 后缀,并用 script.noteExistsByFileName(fileName + ".md", note.id) 检测重名。源码实现见 src/services/scriptingservice.cpp 中 handleNoteTextFileNameHook(QVariant) 的调用分支。

三、内容插入与剪贴板 Hooks

这类 Hook 在向当前笔记插入媒体、附件、粘贴内容时触发。

insertMediaHook

媒体文件插入当前笔记时调用,可改写最终写入笔记的 Markdown 文本。若多个脚本都定义了此函数,第一个返回非空字符串的脚本胜出。

/**
 * @param fileName string the file path of the source media file before it was copied to the media folder
 * @param markdownText string the markdown text of the media file, e.g. my-image
 * @return string the new markdown text of the media file
 */
function insertMediaHook(fileName, markdownText);

示例见 example.qml。源码中通过 insertMediaHook(QVariant,QVariant) 签名检测后逐个脚本调用,见 src/services/scriptingservice.cpp。

insertAttachmentHook

附件文件插入当前笔记时调用,规则与 insertMediaHook 相同(多个脚本定义时,首个返回非空字符串者优先)。

/**
 * @param fileName string the file path of the source attachment file before it was copied to the attachment folder
 * @param markdownText string the markdown text of the attachment file, e.g. my-file.txt
 * @return string the new markdown text of the attachment file
 */
function insertAttachmentHook(fileName, markdownText);

示例见 example.qml。

insertingFromMimeDataHook

使用 Ctrl + Shift + V 向笔记粘贴 HTML 或媒体文件时调用,用于把剪贴板内容转换为自定义文本插入。

/**
 * This function is called when html or a media file is pasted to a note with `Ctrl + Shift + V`
 *
 * @param text text of the QMimeData object
 * @param html html of the QMimeData object
 * @returns the string that should be inserted instead of the text from the QMimeData object
 */
function insertingFromMimeDataHook(text, html);

返回的字符串将替换 QMimeData 对象中的原始文本。两个典型示例:

fetchUrlTitleHook

在链接对话框中,QOwnNotes 尝试抓取网页标题之前调用。多脚本定义时,首个返回非空字符串者优先。

/**
 * @param url string the URL whose title should be fetched
 * @return string the title for the URL, or an empty string to let QOwnNotes fetch it
 */
function fetchUrlTitleHook(url);

返回空字符串表示"交给 QOwnNotes 自己去抓取"。示例 fetch-url-title-with-kanboard-cli.qml 是一个完整的第三方集成案例:它注册 kanboardCliPath 与 kanbanUrlPrefixes 两个设置变量,通过正则从配置的前缀 URL 中提取 Kanboard 任务 ID,再调用 script.startSynchronousProcess(kanboardCliPath, ["--json", "task", "get", taskId]) 获取 JSON 并解析出任务标题返回。

四、Markdown 预览与导出 Hooks

这类 Hook 围绕"Markdown → HTML"转换管线工作,分别作用于转换前与转换后,且都支持区分预览与导出场景。

preNoteToMarkdownHtmlHook

Markdown 转 HTML 之前调用,允许修改送入转换器的 Markdown 内容。多脚本可配合使用,例如把 LaTeX 数学公式或 mermaid 图渲染成预览用的图形表示。注意:此过程不会改动笔记本身。

/**
 * This function is called before the markdown html of a note is generated
 *
 * It allows you to modify what is passed to the markdown to html converter
 *
 * The function can for example be used in multiple scripts to render code (like LaTeX math or mermaid)
 * to its graphical representation for the preview
 *
 * The note will not be changed in this process
 *
 * @param {NoteApi} note - the note object
 * @param {string} markdown - the markdown that is about to being converted to html
 * @param {bool} forExport - true if the html is used for an export, false for the preview
 * @return {string} the modified markdown or an empty string if nothing should be modified
 */
function preNoteToMarkdownHtmlHook(note, markdown, forExport);

示例 preview-styling.qml 中简单用 markdown.replace("- special text", "- something else") 演示了文本改写。源码侧,该 Hook 同时兼容两参数旧签名 preNoteToMarkdownHtmlHook(note, markdown) 与三参数新签名,若检测到旧签名会输出 qWarning 提示改用新签名(见 src/services/scriptingservice.cpp)。

noteToMarkdownHtmlHook

Markdown 转 HTML 之后、渲染之前调用,允许修改最终 HTML。这是定制预览样式的主要入口。

/**
 * This function is called when the markdown html of a note is generated
 *
 * It allows you to modify this html
 * This is for example called before by the note preview
 *
 * The function can be used in multiple scripts to modify the html of the preview
 *
 * @param {NoteApi} note - the note object
 * @param {string} html - the html that is about to being rendered
 * @param {bool} forExport - true if the html is used for an export, false for the preview
 * @return {string} the modified html or an empty string if nothing should be modified
 */
function noteToMarkdownHtmlHook(note, html, forExport);

预览所支持的 CSS 样式子集受 Qt 富文本引擎限制,完整支持列表见 Qt 官方 "Supported HTML Subset" 文档。两个示例:

  • preview-styling.qml:根据 forExport 分别注入不同 CSS(预览用红色缩进、导出用蓝色缩进)到 </style> 之前;
  • example.qml:在 body> 之后插入自定义 H1 标题和分隔线。

源码实现同样兼容旧签名 noteToMarkdownHtmlHook(note, html),并在使用旧签名时输出 qWarning 提示升级为 noteToMarkdownHtmlHook(note, html, forExport)(见 src/services/scriptingservice.cpp)。

五、加密 Hook:自定义加解密方案

encryptionHook

文本需要加密或解密时调用,允许用任意算法(或对接外部程序)实现自定义加密。

/**
 * This function is called when text has to be encrypted or decrypted
 *
 * @param text string the text to encrypt or decrypt
 * @param password string the password
 * @param decrypt bool if false encryption is demanded, if true decryption is demanded
 * @return the encrypted decrypted text
 */
function encryptionHook(text, password, decrypt);

三个官方示例覆盖了不同思路:

  • encryption-keybase.qml:调用 Keybase 外部程序完成加解密;
  • encryption-pgp.qml:对接 PGP/GPG 工具链;
  • encryption-rot13.qml:纯 JS 实现 ROT13 变换,并在 init() 中调用 script.encryptionDisablePassword() 禁用密码对话框——注意 ROT13 不依赖密码,所以 password 参数可以忽略。

六、标签系统 Hooks:自定义笔记标签机制

QOwnNotes 允许你用脚本完全接管标签的增删改查,例如在笔记文本中使用 @tag1、@tag2、@tag3 这样的特殊文本作为标签。

noteTaggingHook

在标签被添加、移除、重命名或需要列出笔记标签时调用。只要任意已激活脚本实现了该函数,笔记标签处理就会交给它接管。

/**
 * Handles note tagging for a note
 *
 * This function is called when tags are added to, removed from or renamed in
 * a note or the tags of a note should be listed
 *
 * @param note
 * @param action can be "add", "remove", "rename" or "list"
 * @param tagName tag name to be added, removed or renamed
 * @param newTagName tag name to be renamed to if action = "rename"
 * @return note text string or string-list of tag names (if action = "list")
 */
function noteTaggingHook(note, action, tagName, newTagName);

文档明确了接管后的 UI 行为细节:

  • 初次从笔记中导入 @tag 形式的标签并覆盖当前标签分配时,不会丢失标签树,只是失去旧的"标签↔笔记"关联;标签仍可移动进其他标签;若标签树中有多个同名标签,将分配第一个命中的标签;
  • 给笔记添加标签 = 向笔记文本追加该标签;从笔记移除标签 = 从文本中删除该标签;
  • 在标签列表中删除标签会从所有相关笔记中删除;在标签列表中重命名标签会同步更新笔记文本;
  • 笔记列表中的批量打标签 / 批量移除标签都会作用于笔记文本;
  • 在标签面板中移动标签时,应用会对所有选中标签及其子标签在所有笔记上触发一系列 add / remove 动作。

完整实现见 note-tagging.qml:list 动作用正则 \B@([^\s,]+) 提取标签名并去重;add 动作检查标签不存在后追加 \n@tagName;remove / rename 用转义后的正则替换文本。

::: warning 务必让 list 动作足够快——每次笔记文件夹或标签树重载时,它都会对每一条笔记执行一次!也就是说,每当你发现 mainwindow.cpp 中出现 reloadTagTree 调用(见 src/mainwindow.cpp 中 MainWindow::reloadTagTree() 及其调用点)时,list 都会被批量触发。 :::

noteTaggingByObjectHook

与 noteTaggingHook 类似,但不局限于标签树根部的标签名——你可以使用整棵标签树,而不是只能操作一个标签列表。区别在于:

  • 参数从标签名变为 TagApi 对象;
  • list 动作需要返回标签 ID 列表而非标签名列表;
  • 这意味着你需要在 list 中自行创建缺失的标签,才能返回已存在的标签 ID。
/**
 * Handles note tagging for a note
 *
 * This function is called when tags are added to, removed from or renamed in
 * a note or the tags of a note should be listed
 *
 * @param note
 * @param action can be "add", "remove", "rename" or "list"
 * @param tag to be added, removed or renamed
 * @param newTagName tag name to be renamed to if action = "rename"
 * @return note text string or string-list of tag ids (if action = "list")
 */
function noteTaggingByObjectHook(note, action, tag, newTagName);

示例 note-tagging-by-object.qml 展示了进阶用法:list 动作中通过 script.getTagByNameBreadcrumbList(["level1a", "level2b", "level3b"]) 按面包屑路径查找标签对象,再用 tag.fetchByName(tagName) 解析文本中的 @tag 并收集 tag.id(tagId == 0 表示标签不存在,示例中以 TODO: create tag 注释提示需要自行创建)。

源码中,ScriptingService::noteTaggingHookExists() 同时检测 noteTaggingHook 与 noteTaggingByObjectHook 任一存在(见 src/services/scriptingservice.cpp),而 src/managers/tagmanager.cpp 中大量分支(如第 477 行先尝试 noteTaggingByObjectHook)据此决定走脚本标签引擎还是内置标签引擎。

七、编辑器增强 Hooks

autocompletionHook

自动补全被触发时(例如按下 Ctrl + Space)调用,返回要追加到补全列表的字符串列表。

/**
 * Calls the autocompletionHook function for all script components
 * This function is called when autocompletion is invoked in a note
 *
 * @return QStringList of text for the autocomplete list
 */
function callAutocompletionHook();

示例 autocompletion.qml 实现了"输入 @ 前缀时补全标签"的实用功能:通过 script.noteTextEditCurrentWord(true) 获取当前单词(含 @),再调用 script.searchTagsByName(word.substr(1)) 做子串搜索并返回标签列表。

highlightingHook

语法高亮期间对编辑器的每个文本块调用,允许实现超越静态正则规则的、动态且上下文感知的高亮。

::: warning 此 Hook 调用极其频繁(每次按键都会对所有可见文本块执行),实现必须保持高效。如果只需要静态正则高亮,请优先使用 addHighlightingRule(见 webpage/src/scripting/methods-and-objects.md#adding-a-highlighting-rule-for-the-editor)或支持自定义颜色与样式的 addHighlightingRule 变体。 :::

/**
 * This function is called for each text block during syntax highlighting.
 * It allows context-aware, dynamic highlighting.
 *
 * @param text {QString} the text of the current block being highlighted
 * @param previousBlockState {int} the highlighter state of the previous block
 *     (-1 if this is the first block)
 * @return {Array} an array of highlight range objects, each with:
 *     start {int} - start position in the text
 *     length {int} - number of characters to highlight
 *     state {int} - the HighlighterState to use (optional, -1 for custom only)
 *     foregroundColor {string} - foreground color name or hex (optional)
 *     backgroundColor {string} - background color name or hex (optional)
 *     bold {bool} - bold font weight (optional)
 *     italic {bool} - italic font style (optional)
 *     underline {bool} - underline the text (optional)
 *     fontSize {int} - font point size (optional)
 */
function highlightingHook(text, previousBlockState) {
  var highlights = [];

  // Example: highlight all occurrences of "FIXME" with red bold underline
  var re = /FIXME/g;
  var match;
  while ((match = re.exec(text)) !== null) {
    highlights.push({
      start: match.index,
      length: match[0].length,
      foregroundColor: "#ff0000",
      underline: true,
      bold: true,
    });
  }

  return highlights;
}

返回数组中的每个对象描述一个高亮区间:start / length 定位区间,state 可复用内置高亮状态(-1 表示仅用自定义格式),其余字段为可选的自定义颜色、字重、斜体、下划线与字号。源码侧,src/helpers/qownnotesmarkdownhighlighter.cpp 在每个文本块高亮时调用该 Hook(仅当 highlightingHookExists() 为真),而该存在性标志在脚本引擎加载时被缓存以避免重复反射检测(见 src/services/scriptingservice.cpp 中 _highlightingHookExists 的初始化)。

两个示例:

  • highlighting.qml:同时演示静态规则(addHighlightingRule,包括自定义颜色映射 foregroundColor / backgroundColor / bold / italic)与动态 Hook(高亮所有 FIXME);
  • custom-highlighting.qml:进一步的自定义高亮示例。

八、外部集成与窗口/布局/进程 Hooks

websocketRawDataHook

浏览器扩展 QOwnNotes Web Companion 通过浏览器上下文菜单发送数据时调用。

/**
 * @param requestType can be "page" or "selection"
 * @param pageUrl the url of the webpage where the request was made
 * @param pageTitle the page title of the webpage where the request was made
 * @param rawData the data that was transmitted, html for requestType "page" or plain text for requestType "selection"
 * @param screenshotDataUrl the data url of the screenshot of the webpage where the request was made
 * @return true if data was handled by a hook
 */
function callHandleWebsocketRawDataHook(requestType, pageUrl, pageTitle, rawData, screenshotDataUrl);

返回 true 表示数据已被某个 Hook 处理。示例 websocket-raw-data-selection-in-note.qml 只处理 "selection" 请求,用 script.noteTextEditWrite(rawData) 把网页选中文本写入当前笔记后返回 true;layouts.qml 则演示了与窗口布局的联动。源码调用入口为 ScriptingService::callHandleWebsocketRawDataHook()(见 src/services/scriptingservice.cpp)。

onDetachedProcessCallback

通过 startDetachedProcess 启动的后台外部进程(脚本线程)执行完毕时回调。详见 webpage/src/scripting/methods-and-objects.md#starting-an-external-program-in-the-background。

/**
 * This function is called when a script thread is done executing.
 * Hint: thread[1]==0 helps to determine if a bulk of started processes for a certain identifier is done.
 *
 * @param {QString} callbackIdentifier - the provided id when calling startDetachedProcess()
 * @param {QString} resultSet - the result of the process
 * @param {QVariantList} cmd - the entire command array [0-executablePath, 1-parameters, 2-exitCode]
 * @param {QVariantList} thread - the thread information array [0-passed callbackParameter, 1-remaining threads for this identifier]
 */
function onDetachedProcessCallback(callbackIdentifier, resultSet, cmd, thread);

关键提示:thread<a href="https://link.gitcode.com/i/e31827e63d89836d52a447fcc5a8d8a3" target="_blank">1] 表示同一 callbackIdentifier 下剩余未完成的线程数,thread[1] == 0 可用来判断一批进程是否全部执行完毕。示例 [callback.qml 在 init() 中批量启动 100 个 sleep 进程(全部使用 "callback-example" 标识符),在回调中通过 thread[1] 追踪批次进度、thread[0] 取回传入的 callbackParameter、cmd[1] 读取进程参数。该 Hook 的调用位于 src/services/scriptingservice.cpp 中 onDetachedProcessCallback(QVariant,QVariant,QVariant,QVariant) 的分发分支。

windowStateChangedHook

窗口状态变化事件触发后调用。

/**
 * This function is called after a WindowStateChange event was triggered
 *
 * @param {QString} windowState - the new window state, parameter value can be "minimized", "maximized", "fullscreen", "active" or "nostate"
 */
function windowStateChangedHook(windowState);

windowState 可取 "minimized"、"maximized"、"fullscreen"、"active" 或 "nostate"。示例 window-state-changed.qml 仅打印状态字符串。

layoutSwitchedHook

窗口布局(Layout)切换时调用。

/**
 * This function is called when layouts are switched
 *
 * @param oldUuid old uuid of layout
 * @param newUuid new uuid of layout
 */
function layoutSwitchedHook(oldUuid, newUuid);

参数为切换前后的布局 UUID。示例见 layouts.qml。源码检测签名为 layoutSwitchedHook(QVariant,QVariant)(见 src/services/scriptingservice.cpp)。

openAiBackendsHook

OpenAI 服务配置被重载时调用(例如脚本引擎重载时也会触发)。它用于为自定义 OpenAI 后端提供配置——包括自建 OpenAI API 兼容的 LLM,如 ChatGPT、Claude,或 Ollama 这类本地推理系统。

/**
 * This function is called when the OpenAI service config is reloaded
 * It returns a list of objects with config parameters for new OpenAI backends
 */
function openAiBackendsHook() {
  return [
    {
      id: "my-custom-ai",
      name: "My Custom AI",
      baseUrl: "http://localhost:5000",
      apiKey: "kDFJkjk3asdm",
      models: ["gpt-3.5-turbo", "gpt-4.0-turbo"],
    },
    {
      id: "my-custom-ai2",
      name: "My Custom AI 2",
      baseUrl: "http://localhost:5001",
      apiKey: "lOikf7eNdb9",
      models: ["gpt-3.5-turbo2", "gpt-4.0-turbo2"],
    },
  ];
}

每个后端对象包含 id、name、baseUrl、apiKey 与 models 列表。文档提示:可以使用"注册脚本设置变量"机制把后端配置存入脚本设置,实现可配置化——参见 webpage/src/scripting/methods-and-objects.md#registering-script-settings-variables。完整示例 custom-openai-backends.qml 额外演示了对接 Groq 云服务的 https://api.groq.com/openai/v1/chat/completions 后端。源码检测签名为 openAiBackendsHook()(无参数),见 src/services/scriptingservice.cpp。

九、Hooks 使用约定与性能注意点

综合全部 22 个 Hook,可以总结出 QOwnNotes 脚本引擎的统一约定:

  1. 返回空字符串表示"不做任何修改/不接管":insertMediaHook、insertAttachmentHook、fetchUrlTitleHook、handleNoteTextFileNameHook、handleNoteNameHook、preNoteToMarkdownHtmlHook、noteToMarkdownHtmlHook、noteTaggingHook 等均遵循此约定;
  2. "第一个返回非空字符串者胜出"的竞争规则:insertMediaHook、insertAttachmentHook、fetchUrlTitleHook 在多脚本场景下由首个非空返回值决定结果;
  3. Hook 被激活即整体接管:如 noteTaggingHook 一旦有脚本实现,标签处理就切换到脚本引擎;websocketRawDataHook 返回 true 即表示数据已被处理;
  4. 性能敏感场景:noteTaggingHook 的 list 动作在每次笔记文件夹/标签树重载时对所有笔记执行,必须够快;highlightingHook 在每次按键时对所有可见文本块执行,应尽量精简逻辑,静态规则优先用 addHighlightingRule。

从源码实现(src/services/scriptingservice.cpp)看,所有 Hook 都经由统一的反射检测与分发框架处理,并记录每次调用的耗时——这既是脚本引擎稳定性的保证,也为脚本作者排查"哪个 Hook 拖慢了界面"提供了日志依据。

参考示例速查

Hook 官方示例(docs/scripting/examples/)
onNoteStored / noteOpenedHook on-note-opened.qml、example.qml
noteDoubleClickedHook external-note-open.qml
insertMediaHook / insertAttachmentHook / insertingFromMimeDataHook example.qml、insert-headline-with-link-from-github-url.qml、note-text-from-5pm-mail.qml
fetchUrlTitleHook fetch-url-title-with-kanboard-cli.qml
handleNoteTextFileNameHook use-tag-names-in-filename.qml
handleNewNoteHeadlineHook custom-new-note-headline.qml
preNoteToMarkdownHtmlHook / noteToMarkdownHtmlHook preview-styling.qml、example.qml
encryptionHook encryption-keybase.qml、encryption-pgp.qml、encryption-rot13.qml
noteTaggingHook note-tagging.qml
noteTaggingByObjectHook note-tagging-by-object.qml
autocompletionHook autocompletion.qml
highlightingHook highlighting.qml、custom-highlighting.qml
websocketRawDataHook websocket-raw-data-selection-in-note.qml、layouts.qml
onDetachedProcessCallback callback.qml
windowStateChangedHook window-state-changed.qml
layoutSwitchedHook layouts.qml
openAiBackendsHook custom-openai-backends.qml

Hook 之外的脚本方法(如 startDetachedProcess、addHighlightingRule、设置变量注册、NoteApi / TagApi 对象属性等)可进一步查阅 methods-and-objects.md 与 classes.md,配合本文的 Hook 清单即可组合出完整的自定义脚本功能。

登录后查看全文
QOwnNotes