I'm writing an extension for watching videos on YouTube. A site opens with a timer in which the YouTube iframe is located. I just need to play the video. I do it with a click. The problem is that it doesn't work. Access to the iframe is in the manifest. The click happens, the screen blinks for a second, and nothing happens. It is interesting that sometimes it works without problems. I do not understand what the problem is.
manifest.json:
{
"manifest_version": 2,
"name": "test",
"description": "test",
"version": "1.0",
"permissions": ["tabs","storage","<all_urls>","activeTab"],
"content_scripts": [
{
"matches": ["<all_urls>"],
"js": ["background.js"],
"run_at": "document_end",
"all_frames": true
}
],
}
bacground.js:
const delay = millis => new Promise((resolve, reject) => {
setTimeout(_ => resolve(), millis)
});
async function test(){
var url = window.location.toString();
var ytframe = "www.youtube.com/embed";
var resultytframe = url.match(ytframe);
if(resultytframe==ytframe){
document.querySelectorAll("button[class='ytp-large-play-button ytp-button']")[0].click();
}
}
test();
Also, everything will work if I make a physical mouse click anywhere(besides the play button) or write something to the console before work background.js. Video for example: https://youtu.be/rlf0eVyPf8w
You need to turn on the sound for YouTube in the browser settings. This problem arose in my Brave browser on Chromium: 89.0.4389.72, I decided to check it in Google Chrome browser on Chromium 102.0.5005.63, there is no such problem and you can turn off the sound