Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/xml/14.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript 在SO chat中播放嘟嘟声_Javascript_Google Chrome Extension_Chat - Fatal编程技术网

Javascript 在SO chat中播放嘟嘟声

Javascript 在SO chat中播放嘟嘟声,javascript,google-chrome-extension,chat,Javascript,Google Chrome Extension,Chat,我正在尝试使用Chrome扩展在SO聊天中播放通知嘟嘟声(或提及嘟嘟声),但我无法正确播放(如果可能的话)。我尝试了以下代码: this.notify = function () { $("#jplayer").jPlayer('play', 0); } 但我得到了以下错误: 未捕获的TypeError:对象[Object Object]没有方法“jPlayer” 有没有办法使用SO聊天声音“模块”/播放器来播放@提及嘟嘟声 更新 我知道我可以设置自己的“音频播放器”,但我想使用聊天中使用

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

this.notify = function () {
  $("#jplayer").jPlayer('play', 0);
}
但我得到了以下错误:

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

有没有办法使用SO聊天声音“模块”/播放器来播放@提及嘟嘟声

更新

我知道我可以设置自己的“音频播放器”,但我想使用聊天中使用的音频播放器,所以我想使用通知蜂鸣音

我已经上传了我的完整代码,它是的一部分。我试图呼叫音频播放器的线路是。

为什么不:

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

Chrome有音频支持(不管怎么说是最新版本),所以这应该没问题。这就是我在扩展中使用的内容。

我猜这是一个沙箱,不允许您从页面执行脚本,所以我猜插件的作用是。
我知道这只是在沙箱外面玩的问题

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);
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);
  }
});
JPlayer.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);
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/*"
  ]
}

你可以在这里看到一些关于与页面交流的内容

你把这个代码放在什么文件里了?我猜代码不是在页面上运行的,而是在扩展的背景页面上运行的。@Walkereno我在一个“content\u scripts”JS文件中运行它。@Walkereno我添加了一个指向项目的链接,以便您进行验证。我给了您一个符合条件的工作示例,为什么没有勾号?您的个人资料页面说您,但不管怎样,我很高兴这对你有好处。很公平,这是你的特权。但这种方法是可行的,而且很简单。您是否有指向此SO聊天模块文档的链接,以便我尝试帮助回答您的问题?我已将问题中的链接更新为完整代码。该功能没有文档,因为它是如此内部。若要扩展此功能。。。那么<代码>新音频(chrome.extension.getURL(“so.mp3”)).play()和so.mp3来自@PAEz well。。。现在我们越来越近了。也许我应该忘掉它,这样做。但我仍然想知道为什么它不起作用。这是一个沙盒安全的东西…检查我的答案,如果你想的话,你可以使用JPlayer。