简体   繁体   English

JxBrowser - 如何在多个组件中获取相同的浏览器 session

[英]JxBrowser - How obtain same browser session in multiple components

I have successfully integrated JxBrowser into a Java Swing application.我已成功将 JxBrowser 集成到 Java Swing 应用程序中。 One of the views (BrowserView) is inside a JTabbedPane + JPanel.其中一个视图 (BrowserView) 在 JTabbedPane + JPanel 内。 I would like to give users the option to switch to full screen in a separate JFrame.我想为用户提供在单独的 JFrame 中切换到全屏的选项。 Note that it would be very difficult in our project to have only one JFrame .请注意,在我们的项目中,只有一个 JFrame 是非常困难的

How, for this second view, resume the already open session?对于第二个视图,如何恢复已经打开的 session? And, conversely, how to resume the full screen session when returning to the integrated view.以及,反过来,返回集成视图时如何恢复全屏 session。 Or, in other words, how can I have a single browsing session with two views?或者,换句话说,我怎样才能有两个视图的单个浏览 session?

I tried to retake the Browser instance and / or the BrowserView instance to no avail.我试图重新获取 Browser 实例和/或 BrowserView 实例无济于事。 The best I got is by taking the same 2 instances in the JFrame.我得到的最好的结果是在 JFrame 中采用相同的 2 个实例。 The context is taken over, but the integrated browser is crashed.上下文被接管,但集成浏览器崩溃。

Here is an extract of my source code:这是我的源代码的摘录:

package testFiles;

import com.teamdev.jxbrowser.browser.Browser;
import com.teamdev.jxbrowser.engine.Engine;
import com.teamdev.jxbrowser.engine.EngineOptions;
import com.teamdev.jxbrowser.engine.RenderingMode;
import com.teamdev.jxbrowser.view.swing.BrowserView;

import javax.swing.*;
import java.awt.*;
import java.nio.file.Paths;

public class PanelChromium extends JPanel
{
    private static final String LICENCE_KEY = "xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx";
    
    private static Engine engine;
    
    private final Browser     browser;
    private final BrowserView browserView;
    
    
    public static void init()
    {
        String userDataDir = System.getProperty("user.home") + "\\ChromiumData";
        EngineOptions options = EngineOptions.newBuilder(RenderingMode.OFF_SCREEN).
            licenseKey(LICENCE_KEY).diskCacheSize(100_000_000).userDataDir(Paths.get(userDataDir)).build();
        
        engine = Engine.newInstance(options);
    }
    
    /** Integrated browser */
    public PanelChromium()
    {
        this(null);
    }
    
    /**
     * Second browser in separated JFrame
     * @param org First browser instance
     */
    public PanelChromium(PanelChromium org)
    {
        setLayout(new BorderLayout(0, 0));
        
        browser = (org == null) ? engine.newBrowser() : org.browser;
        browserView = (org == null) ? BrowserView.newInstance(browser) : org.browserView;
        
        add(browserView, BorderLayout.CENTER);
    }
}

I can instantiate many "new PanelChromium()" but "new PanelChromium(panelOne)" crash the "panelOne" instance.我可以实例化许多“new PanelChromium()”,但“new PanelChromium(panelOne)”会使“panelOne”实例崩溃。

I have found a solution.我找到了解决方案。 Well, not alone... JxBrowser support is monitoring the posts on StackOverflow and has sent me a private email to give me the solution: contrary to what I thought, it's entirely possible to move a Swing component from one container to another - even a component as large as a BrowserView.好吧,并不孤单...... JxBrowser 支持正在监视 StackOverflow 上的帖子,并给我发送了一个私有 email 来给我解决方案:与我的想法相反,完全有可能将 Swing 组件从一个容器移动到另一个容器- 甚至是与 BrowserView 一样大的组件。 So I just instantiate one Browser and one BrowserView and move the BrowserView instance from the JTabbedPane + JPanel to the JFrame:所以我只实例化一个 Browser 和一个 BrowserView 并将 BrowserView 实例从 JTabbedPane + JPanel 移动到 JFrame:

  • JTabbedPane / JPanel.remove() JTabbedPane / JPanel.remove()
  • JFrame / JPanel.add() JFrame / JPanel.add()
  • (and conversely when the JFrame is closed) (反之,当 JFrame 关闭时)

In my case, it works well because the JFrame size and position are controlled and always overlap the JTabbedPane: when the JFrame is displayed, no one can see the "dead" BrowserView in the JTabbedPane.就我而言,它运行良好,因为 JFrame 大小和 position 受到控制并且始终与 JTabbedPane 重叠:当 JFrame 显示在 JTabbedPane 中时,在 JTabbedPane 中看不到“.deadPaneView” But if you follow my example, you should be warned that as only one BrowserView has been created, only one is active at a time.但是,如果您按照我的示例进行操作,则应该警告您,由于只创建了一个 BrowserView,因此一次只有一个处于活动状态。

Finally, I want to thanks TeamDevs for JxBrowser and their quick and efficient support.最后,我要感谢 TeamDevs 对 JxBrowser 及其快速高效的支持。

On googling a bit, it seems you can have 2 Browsers share a session:在谷歌上搜索一下,似乎你可以让 2 个浏览器共享一个 session:

Two Browser instances with the same BrowserContext instances will use the same user data directory.具有相同 BrowserContext 实例的两个 Browser 实例将使用相同的用户数据目录。 As result they will share cookies and cache files.结果,他们将共享 cookies 和缓存文件。 For example: Java例如:Java

BrowserContext context = new BrowserContext(
    new BrowserContextParams("C:\\my-data1"));
Browser browser1 = new Browser(context);
Browser browser2 = new Browser(context);

https://jxbrowser.support.teamdev.com/support/solutions/articles/9000012878-creating-browser https://jxbrowser.support.teamdev.com/support/solutions/articles/9000012878-creating-browser

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM