Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to create chrome crx file programmatically (preferably in java)?

I want to create chrome extension crx file programatically (not using chrome.exe, because it opens new chrome window). So what are the alternatives for same ? My preference is java, but if its possible in other language then also I am okay.

like image 1000
Silent Warrior Avatar asked Jul 07 '10 16:07

Silent Warrior


People also ask

Can you code a Chrome extension in Java?

Chrome extensions must run JavaScript. You can however use Google Web Toolkit to compile Java to JavaScript. And you can write Chrome extensions in Kotlin.

What is CRX file format?

CRX files are ZIP files with a special header and the . crx file extension.

How do I edit a CRX file?

CRX files are like . ZIP files, just change the extension and right click > Extract Files and you are done. Once you have extracted files --> modify them and add to zip and change extension back to . crx.


3 Answers

As kylehuff stated, there are external tools that you could use. But you can always use the command line from Google Chrome to do that which is cross platform (Linux / Windows / Mac).

chrome.exe --pack-extension=[extension_path] --pack-extension-key=[extension_key]

--pack-extension is:

Package an extension to a .crx installable file from a given directory.

--pack-extension-key is:

Optional PEM private key is to use in signing packaged .crx.

The above does not run Google Chrome, it is just command line packing using Chromium's core crx algorithm that they use internally.

like image 108
Mohamed Mansour Avatar answered Oct 14 '22 08:10

Mohamed Mansour


There is a variety of utilities to do this, in various languages (albeit; they are mostly shell/scripting languages)

I cannot post the links to all of them, because I am a new stackoverflow user - I can only post 1 link, so I created a page which lists them all - including the one C one I speak about below - http://curetheitch.com/projects/buildcrx/6/

Anyway, I spent a few hours and put together a version in C which runs on Windows or Linux, as the other solutions require installation of a scripting language or shell (i.e. python, ruby, bash, etc.) and OpenSSL. The utility I wrote has OpenSSL statically linked so there are no interpreter or library requirements.

The repository is hosted on github, but the link above has a list of my utility and other peoples solutions.

Nothing listed for Java, which was your preference, but hopefully that helps!

like image 44
kylehuff Avatar answered Oct 14 '22 09:10

kylehuff


//Method to generate .crx. signature


import java.nio.ByteBuffer;
import java.nio.ByteOrder;
import java.security.KeyPair;
import java.security.KeyPairGenerator;
import java.security.SecureRandom;
import java.security.Signature;
    //@param : extenstionContents  is your zip file , 
    //@returns :  byte[] of the signature , use ByteBuffer to merge them and you have your   
    // .crx
    public static byte[] generateCrxHeader(byte[] extensionContents) throws Exception {
        KeyPairGenerator keyGen = KeyPairGenerator.getInstance("RSA");      
        SecureRandom random = new SecureRandom();
        keyGen.initialize(1024, random);        

        KeyPair pair = keyGen.generateKeyPair();

        Signature sigInstance = Signature.getInstance("SHA1withRSA");
        sigInstance.initSign(pair.getPrivate());
        sigInstance.update(extensionContents);
        byte [] signature = sigInstance.sign();
        byte [] subjectPublicKeyInfo = pair.getPublic().getEncoded();
        final int headerLength = 4 + 4 + 4 + 4 + subjectPublicKeyInfo.length + signature.length;
        ByteBuffer headerBuf = ByteBuffer.allocate(headerLength);
        headerBuf.order(ByteOrder.LITTLE_ENDIAN);
        headerBuf.put(new byte[]{0x43,0x72,0x32,0x34}); // Magic number
        headerBuf.putInt(2); // Version
        headerBuf.putInt(subjectPublicKeyInfo.length); // public key length
        headerBuf.putInt(signature.length); // signature length
        headerBuf.put(subjectPublicKeyInfo);
        headerBuf.put(signature);
        final byte [] header = headerBuf.array();
        return header;
    }
like image 3
JavaHead Avatar answered Oct 14 '22 08:10

JavaHead