使用 PHP 按需创建 .CRX(chrome 扩展 / webapp)文件

发布于 2024-10-15 08:28:28 字数 182 浏览 4 评论 0原文

我需要即时创建 CRX 文件。它适用于我的 CMS 后端,因此它仅适用于经过身份验证的用户,他们可以将 CMS 后端安装为 Web 应用程序并为 Web 应用程序提供更多权限。问题是,后端用于许多域,因此为每个域创建 CRX 文件是一项相当艰巨的工作。所以我认为按需创建 CRX 文件会更容易,该文件将由 PHP 使用自己的域和可能的自定义图标生成。

I need to create CRX file on the fly. It's for my CMS backend, so it will be just for authenticated users who can install CMS backend as webapp and offer some more privileges to the web app. The problem is, that the backend is used for many domains so creating CRX file for each of them is quite a work. So I figured that it would be easier to just create CRX file on demand which would be generated by PHP using its own domain and probably custom icon.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

且行且努力 2024-10-22 08:28:28

在文档页面上,他们解释了 CRX 包格式。有许多第三方库实现了该格式。在接下来的页面中,您可以了解该格式并下载 Ruby / Bash 脚本(您也可以在网上找到其他脚本),如果您想实现自己的打包器,您可以遵循此处描述的格式。

https://developer.chrome.com/extensions/crx

如果您确实不想按照格式,您可以让 PHP 脚本执行以下操作之一:

  1. 使用 Chrome 二进制文件 chrome.exe --pack-extension=c:\myext --pack-extension-key=c:\myext.pem< /code>
  2. 使用 PHP 中的 Ruby 或 Bash 脚本(您可以调用系统命令)

希望有帮助!

On the documentation page, they explain the CRX package format. There are many third party libraries that implemented that format. In the following page, you can learn the format and either download a Ruby / Bash script (you can find others too online), and if you want to implement your own packager, you can follow the format described there.

https://developer.chrome.com/extensions/crx

If you really don't want to follow the format, you can let your PHP script do one of the following:

  1. Use Chrome binary chrome.exe --pack-extension=c:\myext --pack-extension-key=c:\myext.pem
  2. Use the Ruby or Bash script from PHP (you can call system commands)

Hope that helps!

再可℃爱ぅ一点好了 2024-10-22 08:28:28

另外,对于仍在寻找在 PHP 中创建 CTX 的方法的任何人,请查看以下问题:使用 PHP 创建 Google Chrome Crx 文件

Also, for anyone still looking for a way to create a CTX in PHP, look at this question: Create Google Chrome Crx file with PHP

笔落惊风雨 2024-10-22 08:28:28

这对我有用:DI 只是从真实路径更改为 null,否则更改将无法在新的 chrome 上工作:D

/**
* 类Crx生成器
*
* 从以下位置创建 Chrome 扩展 CRX 包
* 文件夹& pem 私钥
*
* 基于 CRX 格式文档:http://developer.chrome.com/extensions/crx.html< /a>
*
* @作者:托马斯·巴纳西亚克
* @许可证:麻省理工学院
* @日期:2013-11-03
*/

类 CrxGenerator {
const TEMP_ARCHIVE_EXT = '.zip';

private $sourceDir = null;
private $cacheDir = '';

private $privateKeyContents = null;
private $publicKeyContents = null;

private $privateKey = null;
private $publicKey = null;

/**
 * @param $file Path to PEM key
 * @throws Exception
 */
public function setPrivateKey($file) {
    if (!file_exists($file)) {
        throw new Exception('Private key file does not exist');
    }

    $this->privateKeyContents = file_get_contents($file);
    $this->privateKey = $file;
}

/**
 * @param $file Path to PUB key
 * @throws Exception
 */
public function setPublicKey($file) {
    if (!file_exists($file)) {
        throw new Exception('Private key file does not exist');
    }

    $this->publicKeyContents = file_get_contents($file);
    $this->publicKey = $file;
}

/**
 * @param $cacheDir dir specified for caching temporary archives
 * @throws Exception
 */
public function setCacheDir($cacheDir) {
    if (!is_dir($cacheDir)) {
        throw new Exception('Cache dir does not exist!');
    }

    $this->cacheDir = $cacheDir;
}

/**
 * @param $sourceDir Extension source directory
 */
public function setSourceDir($sourceDir) {
    $this->sourceDir = $sourceDir;
}

/**
 * @param $outputFile path to output file
 * @throws Exception
 */
public function generateCrx($outputFile) {
    $basename = basename($outputFile);
    // First step - create ZIP archive
    $zipArchive = $this->cacheDir . DIRECTORY_SEPARATOR . $basename . self::TEMP_ARCHIVE_EXT;

    $result = $this->createZipArchive(
        $this->sourceDir,
        $zipArchive
    );

    if (!$result) {
        throw new Exception('ZIP creation failed');
    }

    $zipContents = file_get_contents($zipArchive);

    // Second step - create file signature
    $privateKey = openssl_pkey_get_private($this->privateKeyContents);
    openssl_sign($zipContents, $signature, $privateKey, 'sha1');
    openssl_free_key($privateKey);

    // Create output file

    $crx = fopen($outputFile, 'wb');
    fwrite($crx, 'Cr24');
    fwrite($crx, pack('V', 2));
    fwrite($crx, pack('V', strlen($this->publicKeyContents)));
    fwrite($crx, pack('V', strlen($signature)));
    fwrite($crx, $this->publicKeyContents);
    fwrite($crx, $signature);
    fwrite($crx, $zipContents);
    fclose($crx);

    // Clear cache
    unset($zipArchive);
}

/**
 * @param $source - source dir
 * @param $outputFile - output file
 * @return bool - success?
 */
private function createZipArchive($source, $outputFile) {
    if (!extension_loaded('zip') || !file_exists($source)) {
        return false;
    }

    $zip = new ZipArchive();
    if (!$zip->open($outputFile, ZIPARCHIVE::CREATE)) {
        return false;
    }

     $source = str_replace('\\', '/', realpath($source));

    if (is_dir($source) === true) {
        $files = new RecursiveIteratorIterator(new RecursiveDirectoryIterator($source), RecursiveIteratorIterator::SELF_FIRST);
        foreach ($files as $file) {
            $file = str_replace('\\', '/', $file);

            // Exclude "." and ".." folders
            if( in_array(substr($file, strrpos($file, '/') + 1), array('.', '..')) ) {
                continue;
            }

            $file = $file;

            if (is_dir($file) === true) {
                $zip->addEmptyDir(str_replace($source . '/', '', $file . '/'));
            }
            else if (is_file($file) === true) {
                $zip->addFromString(str_replace($source . '/', '', $file), file_get_contents($file));
            }
        }
    }
    else if (is_file($source) === true) {
        $zip->file_get_contents($source);
    echo  $source;
    }

    return $zip->close();
}

}

This works for me :D I just change from real path to null without that changes won't work on new chrome :D

/**
* Class CrxGenerator
*
* Create Chrome Extension CRX packages from
* folder & pem private key
*
* Based on CRX format documentation: http://developer.chrome.com/extensions/crx.html
*
* @author: Tomasz Banasiak
* @license: MIT
* @date: 2013-11-03
*/

class CrxGenerator {
const TEMP_ARCHIVE_EXT = '.zip';

private $sourceDir = null;
private $cacheDir = '';

private $privateKeyContents = null;
private $publicKeyContents = null;

private $privateKey = null;
private $publicKey = null;

/**
 * @param $file Path to PEM key
 * @throws Exception
 */
public function setPrivateKey($file) {
    if (!file_exists($file)) {
        throw new Exception('Private key file does not exist');
    }

    $this->privateKeyContents = file_get_contents($file);
    $this->privateKey = $file;
}

/**
 * @param $file Path to PUB key
 * @throws Exception
 */
public function setPublicKey($file) {
    if (!file_exists($file)) {
        throw new Exception('Private key file does not exist');
    }

    $this->publicKeyContents = file_get_contents($file);
    $this->publicKey = $file;
}

/**
 * @param $cacheDir dir specified for caching temporary archives
 * @throws Exception
 */
public function setCacheDir($cacheDir) {
    if (!is_dir($cacheDir)) {
        throw new Exception('Cache dir does not exist!');
    }

    $this->cacheDir = $cacheDir;
}

/**
 * @param $sourceDir Extension source directory
 */
public function setSourceDir($sourceDir) {
    $this->sourceDir = $sourceDir;
}

/**
 * @param $outputFile path to output file
 * @throws Exception
 */
public function generateCrx($outputFile) {
    $basename = basename($outputFile);
    // First step - create ZIP archive
    $zipArchive = $this->cacheDir . DIRECTORY_SEPARATOR . $basename . self::TEMP_ARCHIVE_EXT;

    $result = $this->createZipArchive(
        $this->sourceDir,
        $zipArchive
    );

    if (!$result) {
        throw new Exception('ZIP creation failed');
    }

    $zipContents = file_get_contents($zipArchive);

    // Second step - create file signature
    $privateKey = openssl_pkey_get_private($this->privateKeyContents);
    openssl_sign($zipContents, $signature, $privateKey, 'sha1');
    openssl_free_key($privateKey);

    // Create output file

    $crx = fopen($outputFile, 'wb');
    fwrite($crx, 'Cr24');
    fwrite($crx, pack('V', 2));
    fwrite($crx, pack('V', strlen($this->publicKeyContents)));
    fwrite($crx, pack('V', strlen($signature)));
    fwrite($crx, $this->publicKeyContents);
    fwrite($crx, $signature);
    fwrite($crx, $zipContents);
    fclose($crx);

    // Clear cache
    unset($zipArchive);
}

/**
 * @param $source - source dir
 * @param $outputFile - output file
 * @return bool - success?
 */
private function createZipArchive($source, $outputFile) {
    if (!extension_loaded('zip') || !file_exists($source)) {
        return false;
    }

    $zip = new ZipArchive();
    if (!$zip->open($outputFile, ZIPARCHIVE::CREATE)) {
        return false;
    }

     $source = str_replace('\\', '/', realpath($source));

    if (is_dir($source) === true) {
        $files = new RecursiveIteratorIterator(new RecursiveDirectoryIterator($source), RecursiveIteratorIterator::SELF_FIRST);
        foreach ($files as $file) {
            $file = str_replace('\\', '/', $file);

            // Exclude "." and ".." folders
            if( in_array(substr($file, strrpos($file, '/') + 1), array('.', '..')) ) {
                continue;
            }

            $file = $file;

            if (is_dir($file) === true) {
                $zip->addEmptyDir(str_replace($source . '/', '', $file . '/'));
            }
            else if (is_file($file) === true) {
                $zip->addFromString(str_replace($source . '/', '', $file), file_get_contents($file));
            }
        }
    }
    else if (is_file($source) === true) {
        $zip->file_get_contents($source);
    echo  $source;
    }

    return $zip->close();
}

}

野心澎湃 2024-10-22 08:28:28

看起来我已经找到了我正在寻找的东西。 Chrome 团队提供此选项是为了创建无 CRX 的网络应用,只需通过使用简单的清单文件。

创建自己的 Web 应用程序并将其发布到网站上进行安装要容易得多。当我有很多网站有很多域并且我不必为每个域创建自定义 CRX 文件时,它也解决了我的问题。我只是创建一个小的 PHP 脚本,它为每个域动态创建清单文件。

Looks like I have found exactly what I was looking for. Chrome team has made this option to create CRX-less web apps, just by using simple manifest file.

Much easier to create own webapp and publish it on website for installation. And it also solves my problem when I have many websites with a lot of domains and I don't have to create custom CRX file for each domain. I just create small PHP script which creates manifest files on the fly for each domain.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文