Applescript para resaltar texto en Chrome usando Javascript

2

No puedo descubrir cómo adaptar este script, que resalta el texto en Safari, para que funcione con Google Chrome:

set myList to {"AppleScript", "2018", "[email protected]"}

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('" & thisText & "', 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(" & (item 1 of scrollPos) & ", " & (item 2 of scrollPos) & ")" in document 1
end tell

Aquí está mi versión de Google Chrome:

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('" & thisText & "', true)) {document.execCommand('HiliteColor', false, '#5cdf64');}\n                sel.collapseToEnd()"
    end repeat
    execute javascript "document.designMode = 'off';  window.scrollTo(" & (item 1 of scrollPos) & ", " & (item 2 of scrollPos) & ")"
end tell

Responder:

tell application "Google Chrome"
    execute tab 3 of window 1 javascript "document.designMode = 'on'; [window.pageXOffset.toString(),window.pageYOffset.toString()]"
        --> {"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()"
        --> 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()"
        --> 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()"
        --> missing value
    execute current application javascript "document.designMode = 'off';  window.scrollTo(0, 0)"
        --> missing value
end tell
Result:
missing value
    
pregunta Kevin 27.05.2018 - 15:17

1 respuesta

5

Asegúrese de haber habilitado JavaScript desde AppleScript en Google Chrome

  

La ejecución de JavaScript a través de AppleScript está desactivada. Para activarlo, desde la barra de menú, vaya a Ver > Desarrollador > Permitir JavaScript desde Apple Events.

He realizado algunos cambios en su script para que funcione, con los cambios resaltados en negrita.

  • La variable scrollPos y su manejo asociado del desplazamiento al obtener y establecer la posición de la ventana eran superfluos, por lo que todas las referencias a ella se han eliminado.
  • Todos los verbos ejecutados necesitan tell window 1 , no solo un verbo. He agregado to tell window 1 a la información adjunta de la aplicación.
  • El verbo de ejecución toma una referencia y una cadena de JavaScript, no solo JavaScript. Le falta la referencia en todos sus verbos de ejecución.
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

Para ejecutarlo en todas las pestañas, envuélvalo con 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
    
respondido por el grg 10.06.2018 - 13:12

Lea otras preguntas en las etiquetas