javascript - 在 Mac OS X 中控制 iTunes 的 Chrome 扩展程序

标签 javascript macos google-chrome-extension itunes messaging

我正在为 Google Chrome 创建一个扩展,它可以在 iTunes( native 应用程序)中开始和停止播放、获取有关其媒体库的信息等。操作发生在 Mac OS X 10.9.3 Mavericks 中。

我已经找到了 Chrome Runtime API,它有一些 native 方法:

chrome.runtime.sendNativeMessage(.., .., ..)
chrome.runtime.connectNative(.., ..)

它们的描述见 developer.chrome.com 。它们允许向 native 应用程序发送某种“消息”。我已经成功地为 iTunes 创建了一个 native 消息传递主机。首先,我尝试connectNative() 连接到它,但 iTunes 立即断开连接。然后我向它发送了一条随机消息(我在后台页面内执行此操作):

chrome.runtime.sendNativeMessage(
  host,
  {text: "foo bar"},
  function(response){
    console.log(response);
        // > Undefined
    console.log(chrome.runtime.lastError.message);
        // > "Native host has exited."
});

现在我遇到两个问题:

  1. 我走的路对吗?我想做的事情到底有可能吗?
  2. 这些“消息”中到底应该发送什么内容?

通常,我需要在媒体库中执行搜索、开始播放某首歌曲、打开某个流的能力 - 所有这些都是由不同的用户操作触发的。基本上,我可以创建辅助 cocoa 应用程序作为此扩展程序和 iTunes 之间的门户,并且它可以完美地解决 iTunes 的所有困惑问题,但我很确定这是一种矫枉过正,并且存在某种方法可以在扩展程序中完成所有操作。

最佳答案

native 消息传递要求 native 可执行文件支持 Chrome native 消息传递。具体来说:

Chrome starts each native messaging host in a separate process and communicates with it using standard input (stdin) and standard output (stdout). The same format is used to send messages in both directions: each message is serialized using JSON, UTF-8 encoded and is preceded with 32-bit message length in native byte order.

虽然对这个主题进行了零研究,但我仍然确信 iTunes 不是为支持 Chrome native 消息传递而编写的(这就是它“立即断开连接”的原因,因为它不知道您要做什么),并且由于您没有 iTunes 的来源,因此您将无法更改它。

如果您知道如何使用您编写的不同 native 可执行文件来操作 iTunes,那么您可以教可执行文件通过 Chrome native 消息传递,从而充当翻译您自己的可执行文件的桥梁iTunes 操作的音乐协议(protocol)。

关于javascript - 在 Mac OS X 中控制 iTunes 的 Chrome 扩展程序,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24471178/

相关文章:

javascript - 在 Chrome 中下载后访问文件数据...?

javascript - 基于正则表达式匹配启用 javascript/jquery 中的按钮

javascript - 当 JavaScript 对象字面量嵌套时,this 是什么?

xcode - 无法通过 xcode4 提交 Mac 应用程序 - 签名无效

macos - 为什么“Key Down 事件掩码”不起作用,但“全部掩码”却起作用?

javascript - 在 chrome 扩展程序中显示通知

javascript - 带有图表的数据表查询

javascript - 统一码转二进制?

c - 检索正在运行的进程的名称

javascript - 有没有办法请求 Chrome 扩展弹出窗口的麦克风访问权限?