Applescript per evidenziare il testo in Chrome utilizzando Javascript

2

Non riesco a capire come adattare questo script, che evidenzia il testo in Safari, per farlo funzionare 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

Ecco la mia versione di 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

Rispondi:

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
    
posta Kevin 27.05.2018 - 15:17
fonte

1 risposta

5

Assicurati di aver abilitato JavaScript da 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.

Ho apportato alcune modifiche allo script perché funzioni, con le modifiche evidenziate in grassetto.

  • La variabile scrollPos e la relativa gestione dello scrolling ottenendo e impostando la posizione della finestra erano superflue, quindi tutti i riferimenti ad esso sono stati rimossi.
  • Tutti i verbi eseguiti devono essere tell window 1 , non solo un verbo. Ho aggiunto to tell window 1 alla descrizione allegata dell'applicazione.
  • Il verbo execute prende un riferimento e una stringa JavaScript, non solo JavaScript. Ti manca il riferimento su tutti i verbi di esecuzione.
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

Per eseguirlo su tutte le schede, avvolgilo 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
    
risposta data 10.06.2018 - 13:12
fonte

Leggi altre domande sui tag