Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
92 views
in Technique[技术] by (71.8m points)

How can I generate "Chrome Extensions" in Java code?

can you tell me, how can I generate Chrome Extensions in Java code, that is how can I create .crx file in Java code?

Firstly, I created the simplest Chrome Extensions manually. In short, it was done like this:

(1) I created a folder, for example "extension". And created inside 2 files: manifest.json and webrequest.js.

(2) The content of manifest.json is:

{
  "manifest_version": 2,
  
  "name": "Webrequest",
  "version": "1.0",
  "description": "Extension",
  
  "permissions": [
    // ...
  ],
  
  "background": {
    "scripts": [
      "webrequest.js"
    ]
  }
}

(3) The content of webrequest.js is:

chrome.webRequest.onAuthRequired.addListener(
    function (details) {
        console.log("onAuthRequired event has fired", details);
        return {
            authCredentials: {
                username: "user1",
                password: "pswd1"
            }
        };
    },

    {
        urls: [
           // ...            
        ]
    },

    [
        "blocking"
    ]
);

Here, in the authCredentials section, specify the required username / password.

(4) Then in the Chrome browser I wrote "chrome://extensions". Then go into the extensions. Turn on "Developer mode". Next, "Pack extension", and specify my "extension" folder. As a result, Chrome created 2 files: extension.crx and extension.pem.

In the Java project, in the resources folder, I created the chrome_extensions directory and copied the extension.crx file there.

(5) Next in the code, I added my extension:

final String fileString = classLoader.getResource("chrome_extensions/extension.crx").getFile();
final File extensionFile = new File(fileString);        
options.addExtensions(extensionFile);

In the end, everything works for me. But the problem is: as you can see in paragraph (3), I use the login and password of a specific user. But I actually have a lot of users, about 10. So I have 2 options: Either create 10 .crx files with different login / passwords, or generate a .crx file directly in the Java code. I want to try second way.

I already tried to find in Google my question. But I only find the case of how to create a function generateCrxHeader(byte [] extensionContents) from the zip archive. It seems to me, that this is not my situation.

I need to generate the final .crx file, having 2 files at the very beginning: manifest.json and webrequest.js. And I must first substitute the current username and password in webrequest.js.

Can you help me, please, how can I do this?

question from:https://stackoverflow.com/questions/65841814/how-can-i-generate-chrome-extensions-in-java-code

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)
Waitting for answers

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...