I am trying to write a firefox extension that will inject html and js next to the thumbs up button on a youtube video. However, my current way of doing it isn't reliable. Here is my code
manifest.json
{
"manifest_version": 2,
"name": "test",
"version": "1.0.0",
"description": "A notetaking tool for YouTube videos",
"icons": {
"48": "icons/border-48.png"
},
"background": {
"scripts": ["background.js"]
},
"permissions": [
"tabs"
],
"content_scripts": [
{
"matches": ["*://*.youtube.com/watch*"],
"js": ["watchingVideo.js"],
"run_at": "document_start"
}
],
"web_accessible_resources":[
"icons/my_icon.svg"
]
}
background.js
browser.tabs.onUpdated.addListener((tabId, changeInfo, tab) => {
let isYoutubeVid = tab.url.includes('https://www.youtube.com/watch?');
if (isYoutubeVid){
browser.tabs.sendMessage(tabId, {message: 'hello from background.js'});
}
});
watchingVideo.js
browser.runtime.onMessage.addListener((message) => {
alert(message.message);
});
If you follow these steps:
- Open a tab
- go to youtube
- watch a video
The alert message won't pop up. It will only work if you refresh the page while watching a video.
What is a more reliable way to determine if a user is watching a youtube video?