微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

Mac 命令行安装Chrome插件

1. 在终端调用命令行实现Chrome插件安装

/Applications/Google\ Chrome.app/Contents/MacOS/Google\ Chrome --enable-easy-off-store-extension-install extensionPath

其中extensionPath是指的插件文件路径(Chrome插件文件的后缀名是crx),例如~/Desktop/chromeExtension.crx

注意:安装插件之前要确保Chrome浏览器不在运行

2. 通过代码实现Chrome插件安装

- (void)startInstallChromeExtension {
    NSURL *url = [[NSWorkspace sharedWorkspace] URLForApplicationWithBundleIdentifier:@"com.google.Chrome"];
    if (url) {
        Nsstring *chromeExtensionPath = [[NSBundle mainBundle] pathForResource:EXTENSIONSOURCENAME ofType:@"crx" inDirectory:EXTENSIONDIRNAME];
        NSDictionary *configuration = [NSDictionary dictionaryWithObjectsAndKeys:
                                       [NSArray arrayWithObjects:@"--enable-easy-off-store-extension-install",chromeExtensionPath,nil],NSWorkspaceLaunchConfigurationArguments,nil];
        
        [[NSWorkspace sharedWorkspace] launchApplicationAtURL:url
                                                      options:NSWorkspacelaunchdefault
                                                configuration:configuration error:nil];
        
    }
}

- (IBAction)installChromeExtension:(id)sender {
    NSArray *runningChromes = [NSRunningApplication runningApplicationsWithBundleIdentifier:@"com.google.Chrome"];
    if ([runningChromes count] > 0) {
        NSAlert * alert = [NSAlert alertWithMessageText:NSLocalizedString(@"安装Chrome插件,需要关闭正在运行的Chrome浏览器,是否允许?",@"")
                                          defaultButton:NSLocalizedString(@"OK",@"")
                                        alternateButton:NSLocalizedString(@"Cancel",@"")
                                            otherButton:nil
                              informativeTextWithFormat:@""];
        
        NSInteger result = [alert runModal];
        
        if (result == NSAlertDefaultReturn ) {
            for (int i = 0; i < [runningChromes count]; i++) {
                [(NSRunningApplication *)[runningChromes objectAtIndex:i] forceTerminate];
            }
            [self performSelector:@selector(startInstallChromeExtension) withObject:nil afterDelay:0.5];
        }
    }
    else {
        [self performSelector:@selector(startInstallChromeExtension) withObject:nil afterDelay:0.5];
    }
}

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。

相关推荐