I am new to chrome extension writing, I tried to make a simple extension which try to control the speed of YouTube videos, but at the first phase of coding it i want to see the communication of this extension between background script(which send a message) and content script(which receive message).background successfully send message but content script doesn't receive it. why?
manifest.json:
{
"manifest_version":2,
"version":"1",
"name":"youtube playback speed controller",
"description":"this extention controll the playback speed of youtube vidz",
"icons": {
"128":"image/x.png",
"64":"image/x.png",
"16":"image/x.png"},
"background":{
"scripts":["background.js"],
"persistent":false
},
"content_scripts": [
{
"matches": ["*://www.youtube.com/*"],
"js" : ["content.js"]
}
],
"page_action": {
"default_icon": {
"16": "image/x.png",
"24": "image/x.png",
"32": "image/x.png",
"128": "image/x.png"
},
"default_title": "Youtube playback speed",
"default_popup": "popup.html"
},
"commands": {
"speedUp": {
"suggested_key": {
"windows": "Ctrl+Right",
"linux": "Ctrl+Right"
},
"description": "speedup"
},
"slowDown": {
"suggested_key": {
"windows": "Ctrl+Left",
"linux": "Ctrl+Left"
},
"description": "slow down"
}
},
"permissions":[
"tabs",
"https://www.youtube.com/*"
]
}
background.js:
var playbackspeed = 0.5;
function speedhandler(command) {
if(command=='speedUp')
{
playbackspeed +=0.1;
} else if (command=='slowDown') {
playbackspeed -=0.1;
}
}
chrome.commands.onCommand.addListener(function(command) {
speedhandler(command)
if (playbackspeed > 1 | playbackspeed < 0)
{
alert('your desired playback speed is not in normal range');
playbackspeed = 0.5;
}
chrome.tabs.query({ active: true, currentWindow: true }, function(tabs) {
chrome.tabs.sendMessage(tabs[0].id, { x: 'changeit' },function() {
alert('message sent')
})
})
});
content.js:
chrome.runtime.onMessage.addListener(function(responsew, call, sendresponse) {
alert('on message fired')
if(responsew.x == 'changeit')
{
alert('message matches')
}
})