Search code examples
c#geckofx

How do I get a youtube video's upload date using GeckoFX 60's evaluate script method?


The GeckoFX 60 browser I'm using in WPF has an evaluate script method that takes in javascript code (in a form of a string).

What I did:

  1. Looked for a YouTube video to test my javascript code
  2. Placing document.getElementById('date').innerText on the console gave me the information I needed
  3. Went back to my WPF app and placed this:

(C#)

string videoDate = "";
using (Gecko.AutoJSContext js = new Gecko.AutoJSContext(YouTubeBrowser.Window))
{
    js.EvaluateScript("document.getElementById('date').innerText", out videoDate);
}
NewProject.VideoDate = DateTime.Parse(videoDate);

Problem:

It was catching an error so I placed a break before parsing the string and found out that the videoDate string is null

What I expected:

I expected it to return the •Jan 30, 2008 the console showed when I entered the js code on the browser.

So far these lines of code are working for me (both on the console and my wpf app's GeckoBrowser) when getting other information from the YouTube video:

js.EvaluateScript("document.title", out videoTitle); = gets the Video Title

js.EvaluateScript("document.URL", out videoId); = gets the Video Url (which I then filter out to only get the video id in c#)

A few more things I've tried that didn't work:

A. Using GeckoElement and retrieving the browser's Document and its textContent

GeckoElement elem = YouTubeBrowser.Document.GetElementById("date");
videoDate = elem.textContent;

B. Using GeckoElement and retrieving the browser's DomDocument and its textContent

GeckoElement elem = YouTubeBrowser.DOMDocument.GetElementById("date");
videoDate = elem.textContent;

C. Changed innerText to textContent (based on another SO answer I saw that said firefox didn't understand innerText (which is weird since it worked on the console but I guess they added support for that later on) but rather uses textContent to retrieve the value)

string videoDate = "";
using (Gecko.AutoJSContext js = new Gecko.AutoJSContext(YouTubeBrowser.Window))
{
    js.EvaluateScript("document.getElementById('date').textContent", out videoDate);
}
NewProject.VideoDate = DateTime.Parse(videoDate);

Solution

  • I think this has to do with the dynamic DOM on YouTube.
    While I could not retrieve the value by element Id I found the same information on another Tag by class name:

    _gfxBrowser.Document.GetElementsByClassName("watch-time-text")[0].TextContent
    

    Returns: "Published on Jan 25, 2019"