获取 Chrome 扩展 ID 进行开发 [英] Obtaining Chrome Extension ID for development

查看:120
本文介绍了获取 Chrome 扩展 ID 进行开发的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

虽然类似于 这个问题,我不是在问:

Although similar to this SO question, I am not asking:

在什么情况下扩展的 ID 会发生变化?

Under what conditions does an extension's ID change?

也没有

如何将我的 zip 存档上传到 Chrome 仪表板?

How can I upload my zip archive to the Chrome Dashboard?

不过,我想问的是,如何在不使用 Chrome 仪表板的情况下获取扩展程序的密钥.因此,我不认为它是 这个问题.

I am however asking, how I can obtain an extension's key without using the Chrome Dashboard. Therefore, I do not consider it a duplicate of this SO question.

Chrome 扩展程序中的使用 Google Identity 的文档声明需要复制扩展程序对其清单文件的键.

The documentation for using Google Identity inside a Chrome extension states the need to copy an extensions's key to its manifest file.

为了保持您的应用程序 ID 不变,您需要复制应用程序中的密钥将 manifest.json 安装到您的源清单中.

但是,当导航到推荐目录(...Google/Chrome/Default/Extensions) 我没有看到我解压的扩展程序的 ID.我意识到这是因为扩展程序没有安装为 .crx 文件.但是,文档 显然是为了开发而编写的:

However, when navigating to the recommended directory (...Google/Chrome/Default/Extensions) I do not see the ID of my unpacked extension. I realise this is because the extension was not installed as .crx file. However, the documentation is clearly written for the purposes of development:

将已安装的 manifest.json 中的密钥复制到您的源清单中,以便您的应用程序 ID 在开发过程中将保持不变.

如何避免每次更改时打包扩展程序并重新安装?如果我的开发扩展没有安装清单文件,我可以从中获取扩展的密钥,我可以从哪里获取?

How can I avoid packaging my extension and reinstalling each time I make a change? If my development extension has no installed manifest file from which I can obtain the extension's key, where can I get it from?

推荐答案

简单的方法

获取扩展 ID 的最简单方法是生成 .pem 文件并使用我的 其他答案(阅读图片下方的部分).

The easy way

The easiest way to get an extension ID is to generate the .pem file and extract the extension ID using the steps described in my other answer (read the part below the image).

此答案的其余部分适用于那些希望仅使用命令行工具生成扩展程序 ID 的人.我将使用 OpenSSL,因为它是跨平台的.

The rest of this answer is for those who want to generate the extension ID with command-line tools only. I'm going to use OpenSSL because it is cross-platform.

首先,我们生成一个私钥.将此私钥保密,不要丢失.否则您将无法创建具有相同扩展 ID 的 CRX 文件.在撰写本文时,Chrome 生成的私钥为 2048 位 RSA 密钥,采用 PKCS #8 格式(1024 位直到 2013 年).在整个答案中,我将把这个私钥文件称为 key.pem,因为 Chrome 网上应用店期望私钥名为 key.pem.

First, we generate a private key. Keep this private key secret and do not lose it. Otherwise you will not be able to create a CRX file with the same extension ID. As of writing, the private keys generated by Chrome are 2048-bit RSA keys in PKCS #8 format (1024-bit until 2013). Throughout the answer, I will refer to this private key file as key.pem, because the Chrome Web Store expects that the private key is called key.pem.

其次,我展示了如何为 "key" 清单文件的字段.这只是公钥,以 base64 格式编码.

Second, I show how to generate the value for the "key" field of the manifest file. This is just the public key, encoded in base64 format.

我的答案中的第三个命令显示了如何计算给定公钥(从私钥派生而来)的扩展 ID.

The third command in my answer shows how to calculate the extension ID given a public key (derived from a private key).

OpenSSL 安装在大多数 Linux 发行版上.如果没有,只需通过您喜欢的包管理器安装 openssl.

OpenSSL is installed on most Linux distros. If not, just install openssl via your favorite package manager.

# Create private key called key.pem
2>/dev/null openssl genrsa 2048 | openssl pkcs8 -topk8 -nocrypt -out key.pem

# Generate string to be used as "key" in manifest.json (outputs to stdout)
2>/dev/null openssl rsa -in key.pem -pubout -outform DER | openssl base64 -A

# Calculate extension ID (outputs to stdout)
2>/dev/null openssl rsa -in key.pem -pubout -outform DER |  shasum -a 256 | head -c32 | tr 0-9a-f a-p

我已将 2>/dev/null 放在每行的开头,以防止写入 RSA 密钥"输出到控制台.

I've put 2>/dev/null at the start of each line to prevent "writing RSA key" from being output to the console.

如果您没有 OpenSSL,您可以从 此镜像 获取预编译的二进制文件.

If you don't have OpenSSL, you can get a precompiled binary from this mirror.

@echo off
:: Assuming that you have installed OpenSSL in this directory
SET PATH=%PATH%;C:OpenSSL-Win32in

:: Create private key called key.pem
2>NUL openssl genrsa -out priv.tmp 2048
2>NUL openssl pkcs8 -topk8 -in priv.tmp -nocrypt -out key.pem
del priv.tmp

:: Generate string to be used as "key" in manifest.json
2>NUL openssl rsa -in key.pem -pubout -outform DER -out pub.tmp
2>NUL openssl base64 -A -in pub.tmp
del pub.tmp

:: Calculate extension ID
2>NUL openssl rsa -in key.pem -pubout -outform DER -out pub.tmp
2>NUL openssl dgst -sha256 -out checksum.tmp pub.tmp
SET /p EXTID=<checksum.tmp
SET EXTID=%EXTID:* =%
SET EXTID=%EXTID:~0,32%
SET EXTID=%EXTID:f=p%
SET EXTID=%EXTID:e=o%
SET EXTID=%EXTID:d=n%
SET EXTID=%EXTID:c=m%
SET EXTID=%EXTID:b=l%
SET EXTID=%EXTID:a=k%
SET EXTID=%EXTID:9=j%
SET EXTID=%EXTID:8=i%
SET EXTID=%EXTID:7=h%
SET EXTID=%EXTID:6=g%
SET EXTID=%EXTID:5=f%
SET EXTID=%EXTID:4=e%
SET EXTID=%EXTID:3=d%
SET EXTID=%EXTID:2=c%
SET EXTID=%EXTID:1=b%
SET EXTID=%EXTID:0=a%
echo %EXTID%
del checksum.tmp pub.tmp

@echo on

我用 openssl 命令将 2>NUL 放在每一行的开头,以隐藏有关缺少配置文件的无害警告.

I've put 2>NUL at the start of each line with the openssl command to hide a harmless warning about a missing config file.

这是在 Linux 上运行前面命令的示例.命令的相关输出以粗体显示.第一个命令创建一个文件,因此在 shell 中没有可见的输出.请注意,第二条和第三条命令的输出没有以换行符结尾,因此行尾有一个$"(不应复制).

Here is an example of running the previous commands on Linux. The relevant output of the commands are boldfaced. The first command creates a file, so there is no visible output in the shell. Note that the output of the second and third command do not end with a line break, so there is a "$" at the end of the line (which should not be copied).

$ # Create private key called key.pem
$ 2>/dev/null openssl genrsa 2048 | openssl pkcs8 -topk8 -nocrypt -out key.pem
$ # Generate string to be used as "key" in manifest.json (outputs to stdout)
$ 2>/dev/null openssl rsa -in key.pem -pubout -outform DER | openssl base64 -A
MIIBIjANBgkqhkiG9w0BAQEFAAOCAQ8AMIIBCgKCAQEA8vj7SK0NZ6ak7K6m6KEAkfGaNfKUahqFFms6W8rq+voaW7nETrpsMqNyhmBQ+ea0KkyI/S5XIrDQPqDcNpvesYlg9lsmi7CQBZjJw7zNqKkvn0oYaP4SNtWZfZopBumqFbzFi5cst2PT+XU9CBitxXNtocRtcjOsa44W1gPA5xanmtlF258N6Nann+rSOAdhIWqSo/J6fj72cxTNfmqLkwAvhdS4Zyux4F87vxp4YTSwElfYXFsHZWi7h66uuuMzqyOyJz5grhCJ24rtTshMQUCxQWyhO2XT2J1tVfUN1YVw6xdKUz3aGyKZeXCuql5klHmlqE9PTlbKj/1VMiIgCQIDAQAB$ 
$ # Calculate extension ID (outputs to stdout)
$ 2>/dev/null openssl rsa -in key.pem -pubout -outform DER | sha256sum | head -c32 | tr 0-9a-f a-p
mfabfdnimhipcapcioneheloaehhoggk$ 
$ cat key.pem   # Show content of key.pem for completeness of this demo
-----BEGIN PRIVATE KEY-----
MIIEvQIBADANBgkqhkiG9w0BAQEFAASCBKcwggSjAgEAAoIBAQDy+PtIrQ1npqTs
rqbooQCR8Zo18pRqGoUWazpbyur6+hpbucROumwyo3KGYFD55rQqTIj9LlcisNA+
oNw2m96xiWD2WyaLsJAFmMnDvM2oqS+fShho/hI21Zl9mikG6aoVvMWLlyy3Y9P5
dT0IGK3Fc22hxG1yM6xrjhbWA8DnFqea2UXbnw3o1qef6tI4B2EhapKj8np+PvZz
FM1+aouTAC+F1LhnK7HgXzu/GnhhNLASV9hcWwdlaLuHrq664zOrI7InPmCuEInb
iu1OyExBQLFBbKE7ZdPYnW1V9Q3VhXDrF0pTPdobIpl5cK6qXmSUeaWoT09OVsqP
/VUyIiAJAgMBAAECggEAIztFPKmTOwdn/MXqf+rwqTjuUopFSQllaPXNdYf8AL6J
Wema9IuFquYWcjO/Ki1wzH1ik8vHaMlYuOwcYnLBnN69x5s6AKFukNEx2IclDyLR
O/jDh13oCDl600KqVk1Fk3dW8cHPAxyfn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btE=
-----END PRIVATE KEY-----

这篇关于获取 Chrome 扩展 ID 进行开发的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆