0
votes

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.

2
Have you set "persistent": true?Haibara Ai

2 Answers

1
votes

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)

0
votes

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.