Playing sound from source Chrome Extension

时间:2015-07-28 22:13:34

标签: javascript json google-chrome google-chrome-extension

I'm an amateur programmer and I'm trying to create a button on Chrome as an extension that will play a clip when pressed. I'm not entirely convinced I'm doing this correctly. Any help would be much appreciated. Even a point in the right direction would be great. I am loading the .json through Chrome using the unpacked extension loader.

The problem that Chrome is telling me is that the "Manifest is not valid JSON. Line: 10, column: 2, Unexpected data after root element."

{
  "name": "EzWinEzLyfe",
  "version":"1.0",
  "manifest_version": 2,
  "description": "DUH DA NA NAH",
  "browser_action": {
    "default_icon": "icon.png"
  }
}
var myAudio = new Audio();
myAudio.src = "C:\Users\Eduardo\Desktop\Hello World/win.mp3";
myAudio.play();

2 个答案:

答案 0 :(得分:1)

您正在尝试将代码放入清单中。您需要创建一个背景部分,如下所示:

"background": { "scripts": ["background.js"] }

然后创建一个background.js文件并将其放在与清单相同的文件夹中。由于您希望在单击扩展图标时播放声音,请将声音代码放在图标单击侦听器中,如下所示:

chrome.browserAction.onClicked.addListener(function() {
  new Audio("win.mp3").play()
})

Chrome扩展程序无法直接访问用户的文件系统,因此请复制音频并将其放入扩展程序文件夹。

以下是一些开发文档:OverviewManifest File FormatBackground PagesHTMLAudioElement

答案 1 :(得分:-2)

It doesn't appear that your browser_action property is formatted properly, at least according to this Chrome browserAction

      "browser_action": {
          "default_icon": {                    // optional
            "19": "images/icon19.png",           // optional
            "38": "images/icon38.png"            // optional
          }