2012-03-06 86 views
5

我試圖在使用Chrome擴展程序的SO聊天中播放通知嗶聲(或提到嘟嘟聲),但我無法正確(如果它甚至可能)。我曾嘗試下面的代碼:在SO聊天中播放嘟嘟聲

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

,但我得到了以下錯誤:

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

是否有使用SO聊天聲「模塊」 /播放器來播放@mention嘟嘟的方法嗎?

UPDATE

我知道我可以設置我自己的「音頻播放器」,但我想使用在聊天中使用這裏SO音頻播放器,我想使用的通知提示音。

我上傳了我的完整代碼在GitHub gist,它是this project的一部分。我嘗試撥打音頻播放器的線路是224

+0

在哪些文件你把這個代碼?我猜測代碼沒有在頁面上運行,而是擴展的背景頁面。 – mowwwalker 2012-03-07 00:16:46

+0

@Walkerneo我在其中一個「content_scripts」JS文件中運行這個。 – PeeHaa 2012-03-07 00:19:19

+0

@Walkerneo我添加了一個鏈接到項目,所以你可以驗證。 – PeeHaa 2012-03-08 22:07:14

回答

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

3

爲什麼不乾脆:

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

Chrome提供了音頻支持(最新版本反正),所以這應該是蠻好的。這是我在我的擴展中使用的。

+0

夠公平的,這是你的特權。但這種方式很有效,而且很簡單。你有鏈接到這個SO聊天模塊的任何文檔,所以我可以嘗試幫助回答你的問題? – 2012-03-07 15:04:03

+0

我已將我的問題中的鏈接更新爲完整的代碼。沒有關於功能的文檔,因爲它是SO內部聊天。 – PeeHaa 2012-03-08 22:07:44

+2

爲了擴展這個......如何......'new Audio(chrome.extension.getURL(「so.mp3」))。play();'和so.mp3來自http://或cdn。 sstatic.net/chat/so.mp3 – PAEz 2012-03-09 16:55:25