0

Developing a chrome extension that can play music. The problem is chrome extensions when clicked start playing but as soon as we navigate away or minimize the browser or click somewhere else the extension goes away. I've tries background pages but no much luck there.

Is there any other way to develop a chrome extension that once starts on the browser will persist to continue work until the browser is closed or a switch off button on that extension is clicked.

Chetan Raikwal
  • 138
  • 1
  • 1
  • 9

2 Answers2

1

There is no current way to do this, unless you create an actual popup for your extension, or you expect your users to always be in developer mode (unlikely). Source: https://stackoverflow.com/a/30570093/6614294

The chrome docs do state:

No, popups automatically close when the user focuses on some portion of the browser outside of the popup. There is no way to keep the popup open after the user has clicked away.

(Source)

Community
  • 1
  • 1
0

No, there is no current way to do this, a lough work arounds such as, creating an actual popup window, do, exist. This is explicitly stated in the extension FAQ anyway, as you should have researched before. This question has arisen multiple times, such as here

Inject music into webpage using Manifest scripts property.

Example Manifest File

{
  "manifest_version": 2,
  "name": "My Cool Extension",
  "version": "0.1",

}
"content_scripts": [{
    "matches": [
         "<all_urls>"
    ],
    "js": ["script.js"]
}]

script.js example

//script.js
var audio = document.appendChild(document.createElement("audio"));
audio.setAttribute("src","song.mp3");
audio.setAttribute("autoplay","true");
//Set attributes -^

script.js example with jQuery

$(function(){
    var audio = new Audio();
    audio.autoplay = true;
    audio.src = "song.mp3";
    $("body").append(audio);
});

Sources:

  1. Robots Thoughtbot tutorial on making Chrome extension
  2. Building a Chrome Extension - Inject code in a page using a Content script (SO Question)

Lastly, I apologize if my JS may be incorrect, if there are any errors, please say so, as I am slightly inexperienced.

Community
  • 1
  • 1
Arnav Borborah
  • 11,357
  • 8
  • 43
  • 88
  • yes I agree but I was wondering if implementing a background page would do the work. As every-time the popup is opened the state is retrieved from the background page and rendered on the popup in this way the effect would be as if the extension was never closed. – Chetan Raikwal Jul 26 '16 at 14:55
  • It actually, is possible, but you don't necessarily need a background page. What you DO need is to inject a script into the page, by declaring the script in your scripts property in the manifest file. In the manifest file, use JS to inject music into the DOM of the webpage. I will update answer for example. – Arnav Borborah Jul 26 '16 at 20:34