2 votos

Applescript para resaltar texto en Chrome usando Javascript

No consigo averiguar cómo adaptar este script, que resalta texto en Safari, para que funcione con 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('" & 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

5voto

siva Puntos 23

Asegúrate de haber activado JavaScript de AppleScript en Google Chrome

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

He hecho algunos cambios en tu script para que funcione, con los cambios resaltados en negrita.

  • La variable scrollPos y su gestión asociada del desplazamiento obteniendo y estableciendo la posición de la ventana era superflua, por lo que se han eliminado todas las referencias a ella.
  • Todos los verbos de ejecución deben tell window 1 no sólo un verbo. He añadido to tell window 1 al relato adjunto de la solicitud.
  • El verbo ejecutar toma una referencia y una cadena JavaScript, no sólo JavaScript. Te falta la referencia en todos tus 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

AppleAyuda.com

AppleAyuda es una comunidad de usuarios de los productos de Apple en la que puedes resolver tus problemas y dudas.
Puedes consultar las preguntas de otros usuarios, hacer tus propias preguntas o resolver las de los demás.

Powered by:

X