Applescript使用Javascript突出显示Chrome中的文本


2

我无法弄清楚如何调整这个突出显示Safari文本的脚本,以使其与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

这是我的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

回复:

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

对于它的价值,由于'window.find()' command线路起点处的“未知令牌”,我无法让Safari工作。这条线是否意味着评论?你从哪里得到那个剧本?
grg

只是好奇,这个脚本的目的是什么?
2015年

很难解释,但基本上我得到变量这样的名称,IP和其他数据,我在不同的标签上寻找这些信息,所以突出它们真的很有助于快速发现它们
Kevin

Answers:


5

确保您已在Google Chrome中启用AppleScript中的JavaScript

通过AppleScript执行JavaScript已关闭。要打开它,请从菜单栏转到查看>开发人员>允许来自Apple Events的JavaScript。

我对您的脚本进行了一些更改以使其工作,更改以粗体突出显示。

  • 获取和设置窗口位置的scrollPos变量及其相关的滚动处理是多余的,因此所有对它的引用都已被删除。
  • 所有执行动词都需要tell window 1,而不仅仅是一个动词。我已添加to tell window 1到应用程序的附带说明中。
  • execute动词带有引用和JavaScript字符串,而不仅仅是JavaScript。您缺少所有执行动词的引用。
将myList设置为{“AppleScript”,“2018”,“CLOSED”}

告诉应用程序“谷歌浏览器” 告诉窗口1 
    执行活动选项卡 javascript“document.designMode ='on';”  - 删除了scrollPos和页面偏移量

    在myList中重复此文本
        执行活动选项卡 javascript“var sel = window.getSelection();
                    sel.collapse(document.body,0);
                    while(window.find('“&thisText&”',true)){document.execCommand('HiliteColor',false,'#5cdf64');}
                    sel.collapseToEnd()”
    结束重复
    执行活动选项卡 javascript“document.designMode ='off';” -  scrollPos和页面偏移量删除 
结束告诉

要在所有选项卡上运行它,请将其包装为repeat with atab in tabs

告诉应用程序“Google Chrome”告诉窗口1
     重复使用选项卡中的atab 
        执行atab javascript“document.designMode ='on';”  - 删除了scrollPos和页面偏移量

        在myList中重复此文本
            执行atab javascript“var sel = window.getSelection();
                    sel.collapse(document.body,0);
                    while(window.find('“&thisText&”',true)){document.execCommand('HiliteColor',false,'#5cdf64');}
                    sel.collapseToEnd()”
        结束重复
        执行atab javascript“document.designMode ='off';” -  scrollPos和页面偏移量删除
     结束重复 
结束告诉

那是完美的谢谢你!你知道我是否可以在每个标签上运行此突出显示?如果我运行重复模式,我可能会点击选项卡中的不同链接或可能打开新链接?
Kevin

@Kevin repeat with atab in tabs,请参阅编辑
grg
By using our site, you acknowledge that you have read and understand our Cookie Policy and Privacy Policy.
Licensed under cc by-sa 3.0 with attribution required.