0

I'm developing a Chrome Extension which blocks the ads & get the Ad Video ID. But I'm facing a problem that to do all of that I've to right click on video for menu & click on "stats for nerds". By doing so the HTML Elements for Ad Video ID & Video ID appear on the source code which then I can use for all my work.

The main problem is how to automatically right click for the menu. As I'm building a chrome extension the code needs to be in JavaScript I think, if there is an other way please let me know.

I've tried to use selenium to right click but selenium initiates it's own browser not what we're working on. And I also don't know if selenium works in Chrome extension.

My code:- (Before skipping we are getting stats for nerds html class & clicking on it. In JS, I'm able to click but not right click. )

function handleSkipBtn() {

    if (skipBtn.length > 0) {

        //This Lines of code is used to get ad video ID. I just want to open stats of nerds here and then close it.

        var a = document.getElementsByClassName("ytp-sfn-cpn");
        if (a.length > 0) {
            var b = a[0].innerText;
            alert(b);
        }

        skipBtn[0].click();
    }
}

I jus need a way to right click automatically in JavaScript file for chrome extension.

  • You need to wait for the element to appear in DOM. You can use MutationObserver. – wOxxOm Aug 23 '22 at 12:04
  • @wOxxOm It does not appear unless right click event is triggered on the video content window. That's why I want to right click automatically. – Zain Khalid Aug 23 '22 at 19:32
  • Use MutationObserver to wait for `document.querySelector('.ytp-menuitem:last-child')` then click() it. – wOxxOm Aug 23 '22 at 20:08
  • @wOxxOm I don't understand. Waiting won't bring the element in DOM but right clicking on video for options does bring elements in DOM. I've tried alot to right click automatically in JavaScript but js has no inbuilt function to right click. It only has .click function. – Zain Khalid Aug 27 '22 at 12:28
  • Hmm... `movie_player.dispatchEvent(new Event('contextmenu'))` – wOxxOm Aug 27 '22 at 12:52

1 Answers1

0

This will open the right-click menu for you:

driver.execute_script('document.getElementsByClassName("ytp-contextmenu")[0].style.display = "block";');

And this will click the stats for nerds button:

driver.execute_script('document.getElementsByClassName("ytp-menuitem")[6].click();');
Thorium
  • 191
  • 1
  • 14
  • Thanks for your response! My problem remains that this class "ytp-contextmenu" does not appear unless i manually right click. It is hidden in a sense. Further, how do I initiate 'driver' so that it works on already open browser & YouTube video. Solutions on the internet tell us to intiate driver by assigning specific port etc... But I am building a chrome extension that everyone can use. So assigning port solution doesn't work. Waiting for your response! – Zain Khalid Aug 23 '22 at 11:56
  • I've to right click on "video-stream html5-main-video" html element so that "ytp-contextmenu" becomes visible. Currently i can not perform operations on it. – Zain Khalid Aug 23 '22 at 12:04
  • You can initialize 'driver' with `driver = webdriver.Chrome(service=s, options=options)`. Once that is set, you can make the right-click menu visible with `driver.execute_script('document.getElementsByClassName("ytp-contextmenu")[0].style.display = "block";');`. – Thorium Aug 23 '22 at 12:12
  • If you need more help, I can help tomorrow – Thorium Aug 23 '22 at 12:13