1

Basically I want my javafx WebEngine to display the youtube homepage. My code to start up the browser is shown below.

        browserWebView.getEngine().setJavaScriptEnabled(true);
        WebConsoleListener.setDefaultListener(new WebConsoleListener() {
            @Override
            public void messageAdded(WebView webView, String message, int lineNumber, String sourceId) {
                System.out.println("Console: [" + sourceId + ":" + lineNumber + "] " + message);
            }
        });
        browserWebView.getEngine().load("https://www.youtube.com/");

What I end up with is a youtube homepage which looks like this unloadedYoutubeHomePage

The WebConsoleListener is a debugger which helps me find out why the youtube homepage isn't loading and it returned this.

Console: [null:0] Unrecognized Content-Security-Policy directive 'require-trusted-types-for'.

Console: [null:0] Refused to display 'https://accounts.google.com/ServiceLogin?service=youtube&uilel=3&passive=true&continue=https%3A%2F%2Fwww.youtube.com%2Fsignin%3Faction_handle_signin%3Dtrue%26app%3Ddesktop%26hl%3Den%26next%3D%252Fsignin_passive%26feature%3Dpassive&hl=en' in a frame because it set 'X-Frame-Options' to 'DENY'.

I've been searching for solutions to this for quite a while now and it would be great if someone could help me out with my problem. Also, using embedded YouTube video URLs really isn't a solution for me as I would like to load the YouTube homepage itself, not the videos it displays. Additional info: System type: 64-bit operating system, x64-based processor Java platform: Jdk 11 Using javafx version: javafx-sdk-17.0.1

  • 1
    I really wish that askers would not [delete old related posts](https://stackoverflow.com/questions/70604776/how-do-i-properly-load-youtube-com-with-the-javafx-webview). Instead, keep the posts there, and edit them to get them reopened if the become worthy. Or, if you have new improved info like this case and prefer to ask a new question, then fine, ask a new question like you have done, highlight the new info like you have done, but link back to the prior question for reference and context, otherwise it is very confusing, repeats work for potential answerers and useful comments are lost. – jewelsea Jan 07 '22 at 02:43
  • Edit your question, add *full* version info for your OS, jdk and JavaFX versions as well as the user agent string returned by WebView. – jewelsea Jan 07 '22 at 02:47
  • “I've been searching for solutions to this for quite a while now” -> if you located relevant info you should link to the info you found in the question. – jewelsea Jan 07 '22 at 02:56
  • Can you play an embedded YouTube video and it is only loading the home page which does not work? – jewelsea Jan 07 '22 at 02:58
  • It can play embedded youtube videos perfectly fine. Its the youtube home page which does not load. It seems to only load the videos and not the rest of the YouTube. For an example look here: [link](https://imgur.com/c3n4mwl) – harharprogrammer Jan 07 '22 at 03:07
  • It produces the same error when I load youtube urls such as [link](https://www.youtube.com/watch?v=JslxPjrMzqY)```Console: [null:0] The source list for Content Security Policy directive 'script-src' contains an invalid source: ''report-sample''. It will be ignored. Console: [null:0] Refused to display 'https://accounts.google.com/ServiceLogin?service=youtube&uilel=3&passive=true&continue=https%3A%2F%2Fwww.youtube.com%2Fsignin%3Faction_handle_signin%3Dtrue%26app%3Ddesktop%26hl%3Den%26next%3D%252Fsignin_passive%26feature%3Dpassive&hl=en' in a frame because it set 'X-Frame-Options' to 'DENY'.``` – harharprogrammer Jan 07 '22 at 03:10
  • Info on [require-trusted-types-for](https://developer.mozilla.org/en-US/docs/Web/HTTP/Headers/Content-Security-Policy/require-trusted-types-for), it is a relatively new browser feature not supported by all browsers. Probably it is not supported by whatever version of WebEngine you are using. You should try the latest JavaFX early access release to see if it still gives the same message, if it does you can create an issue report for the openjfx project. – jewelsea Jan 07 '22 at 03:18
  • Don’t put error messages in comments, they aren’t readable, instead edit the question, if responding to a comment, you can quote the comment in your edit. – jewelsea Jan 07 '22 at 03:20
  • Okay sorry, I will make sure to do that next time! – harharprogrammer Jan 07 '22 at 03:24
  • See this info on [embed vs watch urls](https://stackoverflow.com/questions/9934944/embedding-youtube-video-refused-to-display-document-because-display-forbidden-b) in reference to `'X-Frame-Options' to 'DENY'`. Not related to home page display but related to your comment with the watch link. – jewelsea Jan 07 '22 at 03:28
  • Okay I will check out the link, thank you! – harharprogrammer Jan 07 '22 at 03:40
  • FWIW, I tried this on Windows 11 + JDK 17 + JavaFX 17.0.1 & 18-ea+9 and the issue replicated. I also got an additional error in the console `RangeError: Maximum call stack size exceeded.` as well as a log about preloading failed. The preloading failed log I get if I access the page in MS Edge (which loads youtube fine), so can be ignored. What is causing the failed display I cannot say, could be the CSP error, but my guess is it is due to the RangeError and whatever is causing that. – jewelsea Jan 07 '22 at 09:55
  • Perhaps you could track down the issue further using a [webview debugger](https://github.com/vsch/Javafx-WebView-Debugger), though that would be difficult I believe. My advice, unless you are very skilled and dedicated, is to give up on the idea of displaying the youtube home page in WebView and try a different approach. Also log a [bug report about it](https://wiki.openjdk.java.net/display/OpenJFX/Submitting+a+Bug+Report) in case the JavaFX devs want to fix it. – jewelsea Jan 07 '22 at 09:58
  • Also note WebView in 17.0.1 and 18ea was [updated reasonably recently](https://bugs.openjdk.java.net/browse/JDK-8268849), so I don't think the issues are around the WebView being out of date but are instead due to some other set of causes. – jewelsea Jan 07 '22 at 09:59
  • If anybody else wants to spend time on this, ***be careful***, `com.sun.javafx.webkit.WebConsoleListener` is not public API, so getting it to work in a modular environment in something like Idea is a pain (you need to export the com.sun.javafx.webkit package to your module in to both the compile settings as well as the VM runtime options in order for it to work at all. – jewelsea Jan 07 '22 at 10:03
  • Okay, thank you! – harharprogrammer Jan 07 '22 at 20:23

0 Answers0