5

我希望能够用 PHP 生成 crx 文件。

crx 文件是带有附加标头的 zip 文件,我不知道如何创建此标头。如果我使用预生成的 pem 文件,我可以创建一个 crx 文件,但这会导致所有 crx 文件具有相同的扩展名 id,这并不好。这是我到目前为止所获得的链接......
http://valorsolo.com/index.php?page=Viewing%20Message&id=1472&pagenum=2#1500

如果它有助于这已在 Python 中完成,这里有一篇关于更详细信息的优秀博客文章......
http://blog.roomanna.com/12-12-2010/packaging-chrome-extensions
和这里的一些链接关于该主题的其他代码.....
http://code.google.com/chrome/extensions/crx.html
http://code.google.com/p/crx-packaging/source/browse/trunk/ packer.py
https://github.com/bellbind/crxmake-python/blob/master/crxmake.py
http://www.curetheitch.com/projects/buildcrx/

4

3 回答 3

3

This ruby code was helpful.

Your public key must be in DER format, and unfortunately PHP's OpenSSL extension can't do that, so far as I can tell. I had to generate it from my private key at the command line:

openssl rsa -pubout -outform DER < extension_private_key.pem > extension_public_key.pub

UPDATE: there is a PHP der2pem() function available here, thanks to tutuDajuju for pointing it out.

Once that's done, building the .crx file is quite easy:

# make a SHA1 signature using our private key
$pk = openssl_pkey_get_private(file_get_contents('extension_private_key.pem'));
openssl_sign(file_get_contents('extension.zip'), $signature, $pk, 'sha1');
openssl_free_key($pk);

# decode the public key
$key = base64_decode(file_get_contents('extension_public_key.pub'));

# .crx package format:
#
#   magic number               char(4)
#   crx format ver             byte(4)
#   pub key lenth              byte(4)
#   signature length           byte(4)
#   public key                 string
#   signature                  string
#   package contents, zipped   string
#
# see http://code.google.com/chrome/extensions/crx.html
#
$fh = fopen('extension.crx', 'wb');
fwrite($fh, 'Cr24');                             // extension file magic number
fwrite($fh, pack('V', 2));                       // crx format version
fwrite($fh, pack('V', strlen($key)));            // public key length
fwrite($fh, pack('V', strlen($signature)));      // signature length
fwrite($fh, $key);                               // public key
fwrite($fh, $signature);                         // signature
fwrite($fh, file_get_contents('extension.zip')); // package contents, zipped
fclose($fh);
于 2011-04-07T19:17:02.997 回答
2

您可以使用有效的 PHP 解决方案:https ://github.com/andyps/crxbuild 您可以在项目和命令行脚本中包含一个 PHP 类。

于 2013-11-16T11:44:19.650 回答
2

CRX 格式在文档页面上有详细描述: http ://code.google.com/chrome/extensions/crx.html

该文件的末尾有 Ruby 和 Bash 的示例。遵循您的语言 (PHP) 中的格式。

于 2011-02-17T03:33:06.927 回答