Note: Advice in this article will only work for JxBrowser 6. See the corresponding article for JxBrowser 7 here.(注意:本文中的建议仅适用于JxBrowser6,JxBrowser7相应文章请点击这里。)


JxBrowser provides API for working with navigation history. Using this API you can get information about navigation entries, got to entry at specified index, remove navigation entries, etc.(JxBrowser提供用于处理导航历史记录的API。使用此API,您可以获取有关导航条目的信息,到达指定索引的条目,删除导航条目等。)


Note: When you create Browser instance it navigates to the about:blank web page by default, so there's always one entry in navigation history.(注意:创建浏览器实例时,默认情况下它会导航到about:blank网页,因此导航历史记录中始终只有一个条目。)


Receiving navigation entry count(接收导航条目计数)

Returns the number of entries in the back/forward list. (返回后退/前进列表中的条目数。)

int entryCount = browser.getNavigationEntryCount();

 

Index of the current navigation entry(当前导航条目的索引)

Returns index of the current navigation entry in the back/forward list. (返回后退/前进列表中当前导航条目的索引。)

int index = browser.getCurrentNavigationEntryIndex();

 

Navigating to the entry at index(导航到索引处的条目)

Navigates to the entry at a specific index in the back/forward list. (导航到后退/前进列表中特定索引处的条目。)

browser.goToIndex(index);

 

Removing the entry at index(删除索引处的条目)

Removes navigation entry from the back/forward list at a specific index. (从特定索引的后退/前进列表中删除导航条目。)

boolean success = browser.removeNavigationEntryAtIndex(index);

 

Getting information about navigation entry(获取有关导航条目的信息)

Prints information about the navigation entry at specific index. (在特定索引处打印有关导航条目的信息。)

NavigationEntry navigationEntry = browser.getNavigationEntryAtIndex(index);
System.out.println("URL = " + navigationEntry.getURL());
System.out.println("Original URL = " + navigationEntry.getOriginalURL());
System.out.println("Title = " + navigationEntry.getTitle());


Clearing entire navigation history(清除整个导航记录)

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

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

/**
 * The sample demonstrates how to clear entire navigation history.
 */
public class ClearHistorySample {
    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);

        loadURLAndWaitReady(browser, "http://www.google.com");
        loadURLAndWaitReady(browser, "http://www.teamdev.com");

        // Returns the number of entries in the back/forward list.
        int entryCount = browser.getNavigationEntryCount();
        // Remove navigation entries at index.
        for (int i = entryCount - 2; i >= 0; i--) {
            boolean success = browser.removeNavigationEntryAtIndex(i);
            System.out.println("Navigation entry at index " + i +
                    " has been removed successfully? " + success);
        }
    }

    private static void loadURLAndWaitReady(Browser browser, final String url) {
        Browser.invokeAndWaitFinishLoadingMainFrame(browser, new Callback<Browser>() {
            @Override
            public void invoke(Browser value) {
                value.loadURL(url);
            }
        });
    }
}