繁体   English   中英

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

[英]Create .CRX (chrome extension / webapp ) file on demand using PHP

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

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

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

如果您确实不想遵循该格式,则可以让您的PHP脚本执行以下操作之一:

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

希望有帮助!

这对我有用:DI仅从真实路径更改为null,否则更改将不适用于新的chrome:D

/ ** *类CrxGenerator * *从*文件夹和Pem私钥创建Chrome扩展CRX包* *基于CRX格式的文档: http : //developer.chrome.com/extensions/crx.html * * @作者:Tomasz Banasiak * @许可证:麻省理工学院* @日期: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();
}

}

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

看起来我已经找到了我想要的东西。 Chrome小组已选择此选项来创建无CRX的Web应用程序 ,只需使用简单的清单文件即可。

创建自己的webapp并将其发布在网站上进行安装要容易得多。 当我有许多具有很多域的网站,而不必为每个域创建自定义CRX文件时,这也解决了我的问题。 我只是创建一个小的PHP脚本,该脚本为每个域动态创建清单文件。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM