AppleScript - 以编程方式添加Google Chrome扩展程序

时间:2017-01-08 11:15:46

标签: javascript shell google-chrome google-chrome-extension applescript

我正在尝试通过AppleScript 为Google Chrome浏览器安装Adblock Plus扩展程序(显然是在Mac上)! 下面是我到目前为止的代码,但它有时会出错并且不可靠,因为如果用户点击其他内容时它会使用tell application "System Events" to keystroke ...,或者Chrome会根据>标签点击次数的变化而变化#39; 所需按钮位于默认位置,然后故障开始,错误正在向左,向右和向中流动。

set abpLink to "https://chrome.google.com/webstore/detail/adblock-plus/cfhdojbkjhnklbpkdaibdccddilifddb"
set finalTab to "chrome-extension://cfhdojbkjhnklbpkdaibdccddilifddb/firstRun.html"

try
    tell application "Google Chrome"
        activate
        open location abpLink
        my waitToLoad()
        repeat 3 times
            tell application "Google Chrome" to activate
            tell application "System Events" to keystroke tab -- Navigate to 'Add to Chrome'
        end repeat
        tell application "System Events" to keystroke return -- Click 'Add to Chrome'
        delay 4 -- This could cause unecessary delays
        tell application "System Events" to tell process "Google Chrome"
            repeat until exists button 1 of window 1
                delay 0.2
            end repeat
            if exists (button 1 of window 1) then
                click (button 1 of window 1)
            end if
        end tell
    end tell
end try
on waitToLoad()
    try
        tell application "Google Chrome" to set chromeLoading to loading of active tab of window 1
        repeat while chromeLoading = true
            delay 1
            activate
            tell application "Google Chrome" to set chromeLoading to loading of active tab of window 1
        end repeat
    end try
end waitToLoad

是否有更好的方法可以完成此任务(在Mac上安装适用于Chrome的ABP),这是最可靠的?也许JavaScript可以为所有框执行按钮点击事件弹出并需要确认(目前由'标签处理'然后'返回'击键)?

1)在打开abpLink时,请处理此按钮: First button to deal with

2)点击上面的按钮后,处理这个: Second button to deal with

我欢迎任何帮助。欢呼声。

0 个答案:

没有答案