用 Applescript 替换在线文本

Replacing Text Online with Applescript

这里是 Applescript 的新手。我正在尝试制作一个在线文本编辑器中扫描文本并将单词替换为其他单词的脚本。

在我的具体情况下,它将一些 html 片段替换为没有任何内容的“”——即删除它们。

我当前的脚本收到以下错误消息… "Brave Browser got an error: window 1 doesn’t understand the “execute” message." number -1708 from window 1

下面是脚本。有问题的行似乎是 EditorField 变量出现的地方。

有什么建议或想法可以使它正常工作吗?谢谢

    tell application "Brave Browser"
        activate
        my replaceWordWithStringInBodyText(" ", "")
        my replaceWordWithStringInBodyText("<span class=\"Apple-converted-space\">", "")
        my replaceWordWithStringInBodyText("<b>", "<strong>")
    end tell

    on replaceWordWithStringInBodyText(searchWord, replacementString)
        tell application "Brave Browser"
            activate
            tell window 1
                set EditorField to (execute javascript "document.getElementById('\"wp-content-editor-container\"')")
                tell EditorField
                    -- start at the end and go to the beginning
                    repeat with i from the (count of paragraphs) to 1 by -1
                        tell paragraph i
                            repeat
                                try
                                    if exists searchWord then
                                        set (last word where it is searchWord) to replacementString
                                    else
                                        exit repeat
                                    end if
                                on error errorMessage
                                    exit repeat
                                end try
                            end repeat
                        end tell
                    end repeat
                end tell
            end tell
            return true
        end tell
    end replaceWordWithStringInBodyText

我假设您在 Brave 浏览器中单击了 允许 JavaScript 来自 Apple Events,以获取它选中,在 View > Developer 菜单上。

话虽如此,但由于您没有包含 URL 来测试您的 code,让我提出来例如,如果我在 Brave 中遇到这个问题,我将如何单击右上角的 Ask Question 按钮浏览器:

tell application "Brave Browser" to tell active tab of front window to execute javascript ¬
    "document.getElementsByClassName('ws-nowrap s-btn s-btn__primary')[0].click();"

如您所见,还必须与 目标选项卡 以及 目标 window 进行通信。因此,您需要相应地调整 代码