簡體   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