Applescript to highlight text in Chrome using Javascript I can't figure out how to adapt this script, which highlights text in Safari, to get it to work with Google Chrome:   

set myList to {"AppleScript", "2018", "demo@apple.com"}

tell application "Safari"
    'window.find()' command change the scroll position when it select the founded string
    set scrollPos to do JavaScript "document.designMode = 'on'; [window.pageXOffset.toString(),window.pageYOffset.toString()]" in document 1
    repeat with thisText in myList
        do JavaScript "var sel = window.getSelection();\n                sel.collapse(document.body, 0);//------    To start at the beginning of the document, not after the selectioned text\n                while (window.find('" &amp; thisText &amp; "', true)) {document.execCommand('HiliteColor', false, '#5cdf64');}\n                sel.collapseToEnd()" in document 1
    end repeat

    -- restore the scroll position
    do JavaScript "document.designMode = 'off';  window.scrollTo(" &amp; (item 1 of scrollPos) &amp; ", " &amp; (item 2 of scrollPos) &amp; ")" in document 1
end tell


Here is my Google Chrome version : 

set myList to {"AppleScript", "2018", "CLOSED"}

tell application "Google Chrome"
    tell tab 3 of window 1 to set scrollPos to execute javascript "document.designMode = 'on'; [window.pageXOffset.toString(),window.pageYOffset.toString()]"

    repeat with thisText in myList
        execute javascript "var sel = window.getSelection();\n                sel.collapse(document.body, 0);//------    To start at the beginning of the document, not after the selectioned text\n                while (window.find('" &amp; thisText &amp; "', true)) {document.execCommand('HiliteColor', false, '#5cdf64');}\n                sel.collapseToEnd()"
    end repeat
    execute javascript "document.designMode = 'off';  window.scrollTo(" &amp; (item 1 of scrollPos) &amp; ", " &amp; (item 2 of scrollPos) &amp; ")"
end tell


Reply :

tell application "Google Chrome"
    execute tab 3 of window 1 javascript "document.designMode = 'on'; [window.pageXOffset.toString(),window.pageYOffset.toString()]"
        --&gt; {"0", "0"}
    execute current application javascript "var sel = window.getSelection();\n                sel.collapse(document.body, 0);//------    To start at the beginning of the document, not after the selectioned text\n                while (window.find('AppleScript', true)) {document.execCommand('HiliteColor', false, '#5cdf64');}\n                sel.collapseToEnd()"
        --&gt; missing value
    execute current application javascript "var sel = window.getSelection();\n                sel.collapse(document.body, 0);//------    To start at the beginning of the document, not after the selectioned text\n                while (window.find('2018', true)) {document.execCommand('HiliteColor', false, '#5cdf64');}\n                sel.collapseToEnd()"
        --&gt; missing value
    execute current application javascript "var sel = window.getSelection();\n                sel.collapse(document.body, 0);//------    To start at the beginning of the document, not after the selectioned text\n                while (window.find('CLOSED', true)) {document.execCommand('HiliteColor', false, '#5cdf64');}\n                sel.collapseToEnd()"
        --&gt; missing value
    execute current application javascript "document.designMode = 'off';  window.scrollTo(0, 0)"
        --&gt; missing value
end tell
Result:
missing value
Make sure you've enabled JavaScript from AppleScript in Google Chrome


  Executing JavaScript through AppleScript is turned off. To turn it on, from the menu bar, go to View > Developer > Allow JavaScript from Apple Events.


I've made a few changes to your script for it to work, with the changes highlighted in bold.


The scrollPos variable and its associated handling of scrolling by getting and setting window position was superfluous, so all references to it have been removed.
All execute verbs need to tell window 1, not just one verb. I've added to tell window 1 to the enclosing tell of the application.
The execute verb takes a reference and a JavaScript string, not just JavaScript. You are missing the reference on all your execute verbs.



set myList to {"AppleScript", "2018", "CLOSED"}

tell application "Google Chrome" to tell window 1
    execute active tab javascript "document.designMode = 'on';" -- scrollPos and page offset removed

    repeat with thisText in myList
        execute active tab javascript "var sel = window.getSelection();
                    sel.collapse(document.body, 0);
                    while (window.find('" & thisText & "', true)) {document.execCommand('HiliteColor', false, '#5cdf64');}
                    sel.collapseToEnd()"
    end repeat
    execute active tab javascript "document.designMode = 'off';" -- scrollPos and page offset removed
end tell

To run it on all tabs, wrap it with repeat with atab in tabs:


tell application "Google Chrome" to tell window 1
    repeat with atab in tabs
        execute atab javascript "document.designMode = 'on';" -- scrollPos and page offset removed

        repeat with thisText in myList
            execute atab javascript "var sel = window.getSelection();
                    sel.collapse(document.body, 0);
                    while (window.find('" & thisText & "', true)) {document.execCommand('HiliteColor', false, '#5cdf64');}
                    sel.collapseToEnd()"
        end repeat
        execute atab javascript "document.designMode = 'off';" -- scrollPos and page offset removed
    end repeat
end tell