@@ -14,14 +14,13 @@ import javax.swing.JPanel
1414fun DesktopWebView (
1515 modifier : Modifier ,
1616 url : String ,
17- onLoadingChange : (Boolean ) -> Unit ={}
1817) {
1918 val jPanel: JPanel = remember { JPanel () }
2019 val jfxPanel = JFXPanel ()
2120
2221 SwingPanel (
2322 factory = {
24- jfxPanel.apply { buildWebView(url, onLoadingChange ) }
23+ jfxPanel.apply { buildWebView(url) }
2524 jPanel.add(jfxPanel)
2625 },
2726 modifier = modifier,
@@ -30,45 +29,39 @@ fun DesktopWebView(
3029 DisposableEffect (url) { onDispose { jPanel.remove(jfxPanel) } }
3130}
3231
33- private fun JFXPanel.buildWebView (url : String , onLoadingChange : ( Boolean ) -> Unit ) {
32+ private fun JFXPanel.buildWebView (url : String ) {
3433 Platform .runLater {
3534 val webView = WebView ()
3635 val webEngine = webView.engine
3736
38- // Notify loading start
39- onLoadingChange(true )
40-
41- webEngine.loadWorker.stateProperty().addListener { _, _, newState ->
42- if (newState == Worker .State .SUCCEEDED ) {
43- // Notify loading end
44- onLoadingChange(false )
45- }
46- }
47-
48- // Set the user agent to simulate a browser for YouTube
4937 webEngine.userAgent =
5038 " Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/58.0.3029.110 Safari/537.3"
5139
52- val script = """
53- setTimeout(function() {
54- var element = document.querySelector('.ytp-chrome-top.ytp-show-cards-title');
55- if (element !== null) {
56- element.style.display = "none";
57- }
58- // Repeat the above pattern for other elements...
59- }, 1000); // Adjust the timeout value as needed
60- """ .trimIndent()
61-
62- webEngine.executeScript(script)
63-
64- // Enable JavaScript support for YouTube embed player
6540 webEngine.isJavaScriptEnabled = true
6641
67- // Enable full-screen mode support
68- webEngine.executeScript(" document.webkitExitFullscreen = function() {};" )
69- // Load the YouTube video using the embed URL
7042 webEngine.load(url)
43+
7144 val scene = Scene (webView)
7245 setScene(scene)
46+
47+ webEngine.loadWorker.stateProperty().addListener { _, _, newState ->
48+ if (newState == Worker .State .SUCCEEDED ) {
49+ val script = """
50+ setTimeout(function() {
51+ var overlaySelectors = [
52+ '.ytp-gradient-top',
53+ '.ytp-gradient-bottom'
54+ ];
55+ overlaySelectors.forEach(function(selector) {
56+ var element = document.querySelector(selector);
57+ if (element !== null) {
58+ element.style.display = 'none';
59+ }
60+ });
61+ }, 1000); // Adjust the timeout value as needed
62+ """ .trimIndent()
63+ webEngine.executeScript(script)
64+ }
65+ }
7366 }
7467}
0 commit comments