gpt4 book ai didi

javascript(苹果脚本): find text and highlight

转载 作者:行者123 更新时间:2023-11-30 19:59:50 25 4
gpt4 key购买 nike

我试图找到某些文本的每个实例并在 Safari 中突出显示它们,无论它们是什么选项卡。

这是目前为止的代码:

set myList to {"ask", "yesterday", "more random e.g"}
try
tell application "Safari"
do JavaScript "document.designMode = 'on';" in tab 1 of window 1


repeat with thisText in myList
do JavaScript "var sel = window.getSelection();
sel.collapse(document.body, 0);
while (window.find('" & thisText & "', true)) {document.execCommand('HiliteColor', false, '#5cdf64');}
sel.collapseToEnd()" in tab 1 of window 1
end repeat
do JavaScript "document.designMode = 'off';" in tab 1 of window 1

end tell
on error
--
end try

此代码的问题:

  • 页面将随着脚本的运行上下滚动
  • 这不会搜索每个标签
  • 如果我点击一个链接/页面刷新无效(我想我可以使用重复,但我遇到了烦人的滚动问题)
  • 如果可能的话,我希望一个变量的每个实例都具有相同的颜色,然后另一个变量的颜色不同...

最佳答案

你可以像这样创建一个循环来处理最前面窗口中的所有选项卡:

set myList to {{"ask","#5cdf64"}, {"yesterday", "#FFFF00"}, {"more random e.g", "#FF0000"}}
tell application "Safari"
activate
set theWindow to front window
tell theWindow
set tabCount to count of tabs
repeat with tabIndex from 1 to tabCount
set current tab to tab tabIndex
tell current tab
repeat with colourPair in myList
do JavaScript "document.designMode = 'on'"
do JavaScript "var sel = window.getSelection(); sel.collapse(document.body, 0); while (window.find('" & (item 1 of colourPair) & "', true)) {document.execCommand('HiliteColor', false, '" & (item 2 of colourPair) & "');}"
do JavaScript "document.designMode = 'off'"
end repeat
end tell
end repeat
end tell
end tell

然后您的 JavaScript 将指向正确的选项卡,而无需对窗口和选项卡的引用进行硬编码。

您要求为 myList 中的每个实例分配不同的颜色,这是通过“列表列表”处理的,其中包含成对的搜索词和颜色代码。

我认为滚动与 sel.collapseToEnd() 有关,我认为您不需要。

关于javascript(苹果脚本): find text and highlight,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53509990/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com