有没有办法获取 Google Chrome 扩展中的命令行参数?

发布于 2024-12-24 21:47:52 字数 205 浏览 1 评论 0原文

我需要使用自定义参数从命令行启动 Chrome,这 包含一些 js 文件的路径。此外,该路径将用于 扩大。

我仔细浏览了所有相关文档并单击了中的所有节点 Chrome 调试器,但没有发现任何与命令行类似的内容 参数。是否有可能获得这些参数或者需要 编写更复杂的 npapi 扩展? (理论上在这样的 npapi- 扩展我们可以通过win-api、命令行获取自我进程 自我过程等)。

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).

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(5

痕至 2024-12-31 21:47:52

黑客警报:此帖子建议传递虚假信息要打开的 URL,其中包含所有命令行参数作为查询字符串参数,例如,

chrome.exe http://fakeurl.com/?param1=val1¶m2=val2

Hack alert: this post suggests passing a fake URL to open that has all the command-line parameters as query string parameters, e.g.,

chrome.exe http://fakeurl.com/?param1=val1¶m2=val2
等往事风中吹 2024-12-31 21:47:52

也许通过命令行将路径传递到自定义用户代理字符串中的扩展程序。例如:

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

然后,在您的扩展中:

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

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])
琉璃梦幻 2024-12-31 21:47:52

基本上我使用 @dfrankow 的回答中给出的技术,但我打开 127.0.0.1:0而不是假网址。这种方法有两个优点:

  1. 跳过名称解析尝试。好吧,如果我仔细选择假 URL 以避免打开现有 URL,则名称解析肯定会失败。但没有必要,那么为什么不跳过这一步呢?
  2. 没有服务器侦听 TCP 端口 0 仅使用 127.0.0.1 是不够的,因为 Web 服务器可能在客户端上运行机,并且我不希望扩展程序意外连接到它。所以我必须指定一个端口号,但是哪个呢?端口 0 是完美的选择:根据 RFC 1700 ,这个端口号是“保留”的,即不允许服务器使用它。

将参数 abcxyz 传递给您的扩展的示例命令行:

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

您可以通过以下方式在 background.js 中读取这些参数:

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));
    });
});

控制台输出(在后台页面的控制台中)扩展名):

{"abc":"42","xyz":"hello"} 

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:

  1. The name resolution attempt is skipped. OK, if I've chosen the fake URL carefully to avoid opening an existing URL, the name resolution would fail for sure. But there is no need for it, so why not just skip this step?
  2. No server listens on TCP port 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"} 
生死何惧 2024-12-31 21:47:52

你可以尝试:

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);
});

这假设你在加载序列中使用 jQuery,你总是可以用任何其他 AJAX 方法替换

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

颜漓半夏 2024-12-31 21:47:52

除了上面关于使用 URL 传递参数的答案之外,请注意,只有扩展程序(而不是应用程序)可以执行此操作。我发布了一个 Chrome 扩展程序,它只拦截 URL 并使其可供其他应用程序使用。

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

来源代码可在:

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

Wi-Fi 公共显示屏

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

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文