Is there a way to get command line options in a Google Chrome extension?

I need to start Chrome from the command line using a custom parameter that contains the path to some js file. Further this path will be used in the extension.

I carefully looked through all the related documentation and clicked all the nodes in the Chrome Debugger, but I didn’t find anything that might resemble parameters on the command line. Is it possible in any case to get these parameters or is it necessary? write a more complex npapi extension? (theoretically, in such npapi- extensions, which we can get independently through win-api, the command line, the process itself, etc.).

+8
command-line google-chrome google-chrome-extension
source share
5 answers

Hack alert: this post suggests passing a fake url that will have all command line parameters as query string parameters, e.g.

chrome.exe http://fakeurl.com/?param1=val1&param2=val2 
+4
source share

Perhaps skip the path to your extension in the user agent user line installed through the command line. For example:

 chrome.exe --user-agent='Chrome 43. My path is:/path/to/file' 

Then in your extension:

 var path = navigator.userAgent.split(":"); console.log(path[1]) 
+4
source share

I mainly use the technique provided in @dfrankow's answer , but I open 127.0.0.1:0 instead of a fake URL. This approach has two advantages:

  • Name resolution error. OK, if I correctly chose the fake URL to avoid opening the existing URL, name resolution will fail. But this is not necessary, so why not just skip this step?
  • The server is not listening on TCP port 0 . Using just 127.0.0.1 not enough, because it is possible that the web server is running on the client machine, and I do not want to connect to it by accident. Therefore, I must indicate the port number, but which one? Port 0 is an ideal choice: according to RFC 1700 , this port number is "reserved", that is, servers are not allowed to use this.

An example command line to pass the arguments abc and xyz to your extension:

 chrome "http://127.0.0.1:0/?abc=42&xyz=hello" 

You can read these arguments in background.js as follows:

 chrome.windows.onCreated.addListener(function (window) { chrome.tabs.query({}, function (tabs) { var args = { abc: null, xyz: null }, argName, regExp, match; for (argName in args) { regExp = new RegExp(argName + "=([^\&]+)") match = regExp.exec(tabs[0].url); if (!match) return; args[argName] = match[1]; } console.log(JSON.stringify(args)); }); }); 

Console output (in the console of the extension base page):

 {"abc":"42","xyz":"hello"} 
+3
source share

You can try:

 var versionPage = "chrome://version/strings.js"; $.post(versionPage, function(data){ data = data.replace("var templateData = ", ""); data = data.slice(0, -1); var jsonOb = $.parseJSON(data); alert(jsonOb.command_line); }); 

Assuming you use jQuery in your loading sequence, you can always replace any other AJAX method

+1
source share

In addition to the answers above about using a URL to pass parameters, note that only extensions, not applications, can do this. I published a Chrome extension that simply intercepts a URL and makes it available to another application.

https://chrome.google.com/webstore/detail/gafgnggdglmjplpklcfhcgfaeehecepg/

Source code is available at:

https://github.com/appazur/kiosk-launcher

for Wi-Fi virtual screens

0
source share

All Articles