在SO聊天中播放哔哔声

时间:2012-03-06 23:42:34

标签: javascript google-chrome-extension chat

我正在尝试使用Chrome扩展程序在SO聊天中播放通知提示音(或提及哔哔声),但我无法做到正确(如果可能的话)。我尝试了以下代码:

this.notify = function () {
  $("#jplayer").jPlayer('play', 0);
}

但是我收到以下错误:

  

未捕获的TypeError:对象[object Object]没有方法'jPlayer'

有没有办法使用SO聊天声音'模块'/播放器播放@mention哔声?

更新

我知道我可以设置自己的'音频播放器',但我想在这里使用聊天中使用的音频播放器,我想使用通知哔声。

我已将GitHub gist的完整代码上传到this project224。我试图呼叫音频播放器的行是{{3}}。

2 个答案:

答案 0 :(得分:3)

为什么不呢:

new Audio('beep.wav').play();

Chrome有音频支持(无论如何最近的版本),所以这应该没问题。这是我在扩展中使用的内容。

答案 1 :(得分:3)

我认为这是一个沙盒的东西,你不允许从页面执行脚本,所以我猜插件数到了。
知道这只是在沙箱外面玩的问题....

<强>的script.js

var customEvent = document.createEvent('Event');
customEvent.initEvent('JPlayerNotify', true, true);

function notify() {
    document.getElementById('communicationDIV').innerText='notify';
    document.getElementById('communicationDIV').dispatchEvent(customEvent);
}

// Utitlity function to append some js into the page, so it runs in the context of the page
function appendScript(file) {
    var script = document.createElement('script');
    script.setAttribute("type", "application/javascript");
    script.setAttribute("src", chrome.extension.getURL(file));
    document.head.appendChild(script);
}

appendScript("JPlayer.js");

// had to wait for a bit for the page to be ready (dialup and all), you wont need to do the setTimeout
setTimeout("notify()",3500);

<强> JPlayer.js

var notify_node = document.createElement('div');
notify_node.id = 'communicationDIV';
document.documentElement.appendChild(notify_node);

notify_node.addEventListener('JPlayerNotify', function() {
  var eventData = notify_node.innerText;
  if (eventData=='notify'){
    $("#jplayer").jPlayer('play', 0);
  }
});

<强>的manifest.json

{
  "name": "JPlayerNotify",
  "version": "0.5.0",
  "description": "JPlayerNotify",
  "content_scripts" : [
    {
      "matches": ["http://chat.stackoverflow.com/rooms/*"],
      "js" : ["script.js"],
      "run_at" : "document_idle",
      "all_frames" : false
    }
  ],
  "permissions": [
    "http://stackoverflow.com/*",
    "https://stackoverflow.com/*",
    "http://*.stackoverflow.com/*",
    "https://*.stackoverflow.com/*"
  ]
}

您可以在此处看到与该页面进行通信的一些内容...... http://code.google.com/chrome/extensions/content_scripts.html