java – SWT浏览器,在显示之前更改内容

我希望在向用户显示之前更改SWT浏览器的内容(一些由用户定义的网页).
当我说改变时,我的意思是添加几个css / js文件到 HTML代码.

我尝试使用ProgressListener用于此目的(我使用getText()获取所有代码,进行一些更改并应用setText()方法),但发现在这种情况下,没有绝对路径定义的所有其他资源都不再可用.

它发生了,因为在setText()浏览器将页面设置为about:blank之后.所以,我有所有代码,但所有使用相对路径定义的css / js / img都不再可用.

是否可以添加几行css / js定义,而不是松散所有没有相对路径的资源?

提前致谢!

使用getText并修改它不是更改HTML内容的可取方法.我建议你使用org.eclipse.swt.browser.Browser的execute()方法.它允许您在页面的DOM对象上触发 javascripts.

>>例

在这段代码中,我允许页面完全加载,然后查找所有链接项,然后在它们周围创建一个红色边框.

>>输出

>>代码

import org.eclipse.swt.SWT;
import org.eclipse.swt.SWTError;
import org.eclipse.swt.browser.Browser;
import org.eclipse.swt.browser.LocationEvent;
import org.eclipse.swt.browser.LocationListener;
import org.eclipse.swt.browser.ProgressEvent;
import org.eclipse.swt.browser.ProgressListener;
import org.eclipse.swt.layout.GridData;
import org.eclipse.swt.layout.GridLayout;
import org.eclipse.swt.widgets.Display;
import org.eclipse.swt.widgets.Event;
import org.eclipse.swt.widgets.Label;
import org.eclipse.swt.widgets.Listener;
import org.eclipse.swt.widgets.Shell;
import org.eclipse.swt.widgets.Text;
import org.eclipse.swt.widgets.ToolBar;
import org.eclipse.swt.widgets.ToolItem;

public class BrowserTest 
{
    private static Browser browser;

    public static void main(String [] args) 
    {
        Display display = new Display();
        final Shell shell = new Shell(display);
        GridLayout gridLayout = new GridLayout();
        gridLayout.numColumns = 3;
        shell.setLayout(gridLayout);

        createBrowser(shell);

        browser.addProgressListener(new ProgressListener() 
        {
            public void changed(ProgressEvent event) {
            }
            public void completed(ProgressEvent event) {
                changeSomething();
            }
        });

        shell.open();
        browser.setUrl("http://google.com");

        while (!shell.isDisposed()) {
            if (!display.readAndDispatch())
                display.sleep();
        }
        display.dispose();
    }

    protected static void changeSomething() 
    {
        String s = "var allLinks = document.getElementsByTagName('a'); " +
                "for (var i=0, il=allLinks.length; i<il; i++) { " +
                    "elm = allLinks[i]; elm.style.border = 'thin solid red';" +
                "}";

        System.out.println(browser.execute(s));
    }

    private static void createBrowser(Shell shell) 
    {
        ToolBar toolbar = new ToolBar(shell, SWT.NONE);
        ToolItem itemGo = new ToolItem(toolbar, SWT.PUSH);
        itemGo.setText("Go");

        GridData data = new GridData();
        data.horizontalSpan = 3;
        toolbar.setLayoutData(data);

        Label labelAddress = new Label(shell, SWT.NONE);
        labelAddress.setText("Address");

        final Text location = new Text(shell, SWT.BORDER);
        data = new GridData();
        data.horizontalAlignment = GridData.FILL;
        data.horizontalSpan = 2;
        data.grabExcessHorizontalSpace = true;
        location.setLayoutData(data);

        try {
            browser = new Browser(shell, SWT.NONE);
        } catch (SWTError e) {
            System.out.println("Could not instantiate Browser: " + e.getMessage());
            //display.dispose();
            return;
        }
        data = new GridData(SWT.FILL, SWT.FILL, true, true);
        data.horizontalSpan = 3;
        browser.setLayoutData(data);

        /* event handling */
        Listener listener = new Listener() 
        {
            public void handleEvent(Event event) 
            {
                ToolItem item = (ToolItem)event.widget;
                String string = item.getText();
                if (string.equals("Go")) browser.setUrl(location.getText());
            }
        };

        browser.addLocationListener(new LocationListener() {
            public void changed(LocationEvent event) {
                if (event.top) location.setText(event.location);
            }
            public void changing(LocationEvent event) {
            }
        });


        itemGo.addListener(SWT.Selection, listener);
        location.addListener(SWT.DefaultSelection, new Listener() {
            public void handleEvent(Event e) {
                browser.setUrl(location.getText());
            }
        });     
    }
}

>>进一步阅读

> Eclipse Article
> SWT Snippets

希望这可以帮助.

相关文章
相关标签/搜索