Mac 命令行安装Chrome插件

本文介绍了两种在Mac上安装Chrome插件的方法:一是通过终端命令行进行安装;二是使用Objective-C代码实现自动化安装。文中详细解释了每种方法的具体步骤,并强调了在安装前需确保Chrome浏览器未处于运行状态。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

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];
    }
}


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值