Comments added via plugin disappear after page reload

Hi! I’m facing an issue while developing a plugin. I wrote a plugin that should add comments to the text of a DOCX document. Everything works successfully and comments appear, but after refreshing the editor page, the comments disappear.

Here’s the plugin code snippet responsible for saving comments:

function handleReviewButtonClick(plugin) {
        plugin.callCommand(function () {
            // Получаем весь текст документа
            let document = Api.GetDocument();
            let props = {
                Numbering: false,      // не хочу нумерацию
                Math: false,           // не хочу математические формулы
                ParaSeparator: "\n",  // разделитель параграфов
                TabSymbol: "\t"       // символ табуляции
            };
            let text = document.GetText(props);

            fetch("https://llm-agent-proxy/ai/review", {
                method: "POST",
                headers: {
                    "Content-Type": "application/json"
                },
                body: JSON.stringify({ content: text })
            })
                .then(response => {
                    if (!response.ok) throw new Error("Ошибка сети");
                    return response.json();
                })
                .then(data => {
                    console.log("Получены комментарии:", data);

                    // Вставляем комментарии в документ
                    for (let i = 0; i < data.length; i++) {
                        let comment = data[i];
                        let targetList = document.Search(comment.target);

                        for (let j = 0; j < targetList.length; j++) {
                            Api.AddComment(targetList[j], comment.text, "AI Reviewer");
                        }
                    }
                })
                .catch(err => {
                    console.error("Ошибка при проверке документа:", err);
                });
        });
    }

I`m using OnlyOffice in docker with version 9.1.0
Could you please advise what might be causing this?

Okay, as soon as I wrote about the problem, I got an idea on how to solve it. I managed to do it using the plugin API instead of the editor API.​

function handleReviewButtonClick(plugin) {
        plugin.callCommand(function () {
            // Получаем весь текст документа
            let document = Api.GetDocument();
            let props = {
                Numbering: false,      // не хочу нумерацию
                Math: false,           // не хочу математические формулы
                ParaSeparator: "\n",  // разделитель параграфов
                TabSymbol: "\t"       // символ табуляции
            };
            return document.GetText(props);
        }, false, false, function(text) {

            return fetch("https://llm-review-proxy/ai/review", {
                method: "POST",
                headers: {
                    "Content-Type": "application/json"
                },
                body: JSON.stringify({ content: text })
            })
                .then(response => {
                    if (!response.ok) throw new Error("Ошибка сети");
                    return response.json();
                })
                .then(data => {
                    console.log("Получены комментарии:", data);

                    for (let i = 0; i < data.length; i++) {
                        let comment = data[i];
                        plugin.executeMethod ("SearchNext", [
                            {
                                "searchString": comment.target,
                                "matchCase": false
                            },
                            true
                        ]);

                        plugin.executeMethod ("AddComment", [
                            {
                                "UserName": "AI Reviewer",
                                "Text": comment.text,
                                "Solved": false
                            }
                        ]);
                    }
                })
                .catch(err => {
                    console.error("Ошибка при проверке документа:", err);
                });
        });
    }
1 Like