我正在尝试查找某些文本的每个实例,并在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
带有此代码的问题:
答案 0 :(得分:1)
您可以像这样循环处理最前面的窗口中的所有标签:
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()
有关,我认为您不需要。