2

I need to highlight every instance of some keywords in Safari.

I have this script which work :

set myList to {"demo", "demo1"}


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

How can I do to run in every tabs instead only one tab?

I try this but this is not working because of the way safari is handing tabs

try
    tell application "Safari"
        repeat with atab in tabs
            do JavaScript "document.designMode = 'on';" in atab 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 atab of window 1
            end repeat
            do JavaScript "document.designMode = 'off';" in atab of window 1
        end repeat
    end tell
on error
    --
end try

tell application "Safari" count every tab --> error number -1728 from every tab end tell

update, the only issue, it's make the page scroll up and down every time the repeat take place :

repeat 50 times
    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
end repeat
  • count every tab in window 1, surely. And, similarly, repeat with atab in tabs of window 1. – CJK Nov 29 '18 at 10:18

You must log in to answer this question.

Browse other questions tagged .