开发者

SWT browser, change content before showing

I want to change content of SWT Browser (some webpage deefined by user), before it will'be shown to the user. When I say change, I mean add few css/js files to html code.

I tried to use ProgressListener for this purposes (I get all code with getText(), do some changes and apply with setText() method), but found that in this case, all other resources, defined without absolute pathes are no more available.

It happens, because after setText() browser is setting page to about:blank. So, I have all code, but all css/js/img defined with relative pathes are no mor开发者_运维技巧e available.

Is it possible to add few lines of css/js definitions and not to loos all resource defined without relative pathes?

Thanks in advance!


Using getText and modifying it is not the advisable way of changing HTML content. I will suggest you to use execute() method of org.eclipse.swt.browser.Browser. It allows you fire javascripts on the DOM object of the page.

>> Example

Here in this code I am allowing the page to fully load and then looking for all the links item and then creating a red border around them.

>>Output

SWT browser, change content before showing

>>Code

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());
            }
        });     
    }
}

>> Further Reading

  1. Eclipse Article
  2. SWT Snippets

Hope this helps.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜