Reputation: 121
I need to launch Chrome from command line with custom parameter, which contains path to some js-file. Further this path will be used in extension.
I browsed carefully all related documentation and clicked all nodes in Chrome debugger, but found nothing which can resemble on command line parameters. Is it possible anyway to get these parameters or it's need to write more complex npapi-extension? (theoretically in such npapi- extension we able to get self process through win-api, command line of self process and so on).
Upvotes: 12
Views: 12103
Reputation: 317
Further to the answers above about using the URL to pass parameters in, note that only Extensions, not Apps, can do this. I've published a Chrome Extension that just intercepts the URL and makes it available to another App.
https://chrome.google.com/webstore/detail/gafgnggdglmjplpklcfhcgfaeehecepg/
The source code is available at:
https://github.com/appazur/kiosk-launcher
for Wi-Fi Public Display Screens
Upvotes: 0
Reputation: 1054
Perhaps pass the path to your extension in a custom user agent string set via 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])
Upvotes: 6
Reputation: 28678
Basically I use the technique given in @dfrankow's answer, but I open 127.0.0.1:0
instead of a fake URL. This approach has two advantages:
0
. Using simply 127.0.0.1
is not enough, since it is possible that a web server runs on the client machine, and I don't want the extension to connect to it accidentally. So I have to specify a port number, but which one? Port 0
is the perfect choice: according to RFC 1700, this port number is "reserved", that is, servers are not allowed to use it. Example command line to pass 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 this way:
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 background page of the extension):
{"abc":"42","xyz":"hello"}
Upvotes: 3
Reputation: 1326
You could 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);
});
This assumes you are using jQuery in your loading sequence, you could always substitute with any other AJAX method
Upvotes: 2