单击不使用YouTube按钮与Chrome Extension一起播放视频
我正在写一个扩展程序,用于在YouTube上观看视频。一个站点打开,YouTube Iframe位于该计时器中。我只需要播放视频。我点击做到这一点。问题是它不起作用。对iframe的访问已在清单中。点击发生,屏幕闪烁一秒钟,什么也没有发生。有趣的是,有时它没有问题。我不明白问题是什么。
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();
另外,如果我在任何地方单击(除了播放按钮之外)或在工作背景之前将东西写在控制台上,一切都将有效。 视频例如: https://youtu.be/rlf0evypf8w
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
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您需要在浏览器设置中打开YouTube的声音。这个问题是在我的勇敢浏览器上出现的:89.0.4389.72,我决定在Chromium 102.0.5005.63上在Google Chrome浏览器中检查它,没有这样的问题,您可以关闭声音
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