I'm working on a Chrome extension that needs to automatically add comments to a YouTube video. The extension works by opening a new window and navigating to the YouTube URL. Here's how I open the window:
await chrome.windows.create({
url,
focused: false,
type: "panel",
state: 'minimized'
});
After that, I attempt to scroll down to the comment section and insert a comment using JavaScript like so:
setTimeout(() => {
document.body.style.zoom = '0.25';
document.querySelector('[section-identifier="comment-item-section"]').scrollIntoView();
setTimeout(() => {
document.getElementById('placeholder-area').click();
setTimeout(() => {
dispatchPaste(document.querySelector('ytd-commentbox #contenteditable-root'), comment_data);
setTimeout(() => {
document.getElementsByClassName('yt-spec-button-shape-next yt-spec-button-shape-next--filled yt-spec-button-shape-next--call-to-action')[0].click();
}, 1000);
}, 1000);
}, 1000);
}, 3000);
The issue is that the comment section doesn't seem to load unless the window is focused or maximized manually by the user.
Things I've tried:
Zooming out and scrolling to the bottom programmatically, thinking that the comments section needs to be in the viewport.
Programmatically firing focus events to emulate user interaction.
Using document.hidden
and document.visibilityState
to manipulate visibility.
None of these have worked so far. So I'll be grateful for any help or ideas! Thanks in advance!