I'm developing chrome extension.
I want to send message from content.js
to background.js
but it's not working.
If I click on the plugin icon and click on the button I added to YouTube with the popup open, the message is sent to background.js. But I have to click the plugin icon again for the message to be sent every time.
content.js
if(location.hostname.includes("youtube.com")){
document.addEventListener('yt-navigate-finish', process);
document.addEventListener('DOMContentLoaded', process);
function process() {
if (!location.pathname.startsWith('/watch')) {
return;
}
if(document.getElementById("yt-btn-music-12atv") == null) {
var yt_btn_music = document.createElement("button");
yt_btn_music.innerHTML = "Keşif Kuyruğu";
yt_btn_music.id = "yt-btn-music-12atv";
} else {yt_btn_music = document.getElementById("yt-btn-music-12atv");}
document.getElementById("below").insertBefore(yt_btn_music, document.getElementById("below").firstChild);
document.getElementById("yt-btn-music-12atv").addEventListener("click", function() {
chrome.runtime.sendMessage({request: "call-func_openMusic"});
});
}
}
background.js
function openMusic() {
openTab("https://www.youtube.com/" + music, "Şarkı Köşesi", "www.youtube.com");
if(lastMusics.length >= 10) {
lastMusics.shift();
}
if(lastMusics.includes(music) == false) {
lastMusics.push(music);
}
setStorage();
loadStorage();
}
chrome.runtime.onMessage.addListener(
function(response, sender, sendResponse) {
if (response.request === "call-func_openMusic")
openMusic();
sendResponse();
}
);
manifest.json
{
"name": "Extension",
"description": "Şimdiye kadar yapılmış en özel akıllı tahta eklentisi...",
"version": "3.0.0",
"manifest_version": 3,
"options_page": "popup.html#settings",
"background.service_worker": {
"scripts": ["/js/background.js"],
"persistent": false
},
"content_scripts": [{
"matches": ["<all_urls>"],
"js": [ "/js/content.js" ],
"css": [ "/css/content.css" ],
"run_at": "document_end"
}],
"permissions": [
"storage",
"unlimitedStorage",
"tabs",
"activeTab"
],
"icons": {
"16": "/img/16.png",
"48": "/img/48.png",
"128": "/img/128.png"
},
"action": {
"default_title": "Extension",
"default_popup": "popup.html",
"default_icon": {
"16": "/img/16.png",
"48": "/img/48.png",
"128": "/img/128.png"
}
}
}