Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Playing the beep in SO chat

I am trying to play the notification beep (or mention beep) in the SO chat using a Chrome extension, but I cannot get it right (if it is even possible). I have tried the following code:

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

But I get the following error:

Uncaught TypeError: Object [object Object] has no method 'jPlayer'

Is there a way to use the SO chat sound 'module' / player to play the @mention beep?

UPDATE

I know I can setup my own 'audio player', but I want to use the audio player that is used in the chat here on SO and I want to to use the notification beep.

I've uploaded my full code in a GitHub gist which is part of this project. The line where I am trying to call the audio player is 224.

like image 478
PeeHaa Avatar asked Mar 06 '12 23:03

PeeHaa


1 Answers

Its a sandbox thing I guess, your not allowed to execute scripts from the page so i guess plugins count to.
That known its just a matter of playing outside the sandbox....

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/*"
  ]
}

You can see some stuff on communicating with the page here... http://code.google.com/chrome/extensions/content_scripts.html

like image 169
PAEz Avatar answered Sep 19 '22 07:09

PAEz