Since 6.14 version JxBrowser provides API that allows injecting custom style sheet (CSS) into every web page loaded in the Browser instance. The following example demonstrates how to use this functionality:(从6.14版本开始,JxBrowser提供了API,该API允许将自定义样式表(CSS)注入到Browser实例中加载的每个网页中。下面的示例演示如何使用此功能:)

import com.teamdev.jxbrowser.chromium.Browser;
import com.teamdev.jxbrowser.chromium.swing.BrowserView;

import javax.swing.*;
import java.awt.*;

public class CustomCSS {
    public static void main(String[] args) {
        Browser browser = new Browser();
        BrowserView view = new BrowserView(browser);

        JFrame frame = new JFrame();
        frame.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
        frame.add(view, BorderLayout.CENTER);
        frame.setSize(800, 600);
        frame.setLocationRelativeTo(null);
        frame.setVisible(true);

        browser.setCustomStyleSheet("body { background-color: orange; }");
        browser.loadURL("about:blank");
    }
}

Note: the injected CSS won't override already defined CSS properties on the loaded web page.(注意:注入的CSS不会覆盖已加载网页上已定义的CSS属性。)


To reset injected CSS call the browser.setCustomStyleSheet("") method.(要重置注入的CSS,请调用browser.setCustomStyleSheet(“”)方法。)