查找 SWT 浏览器的浏览器 type/version
Find browser type/version of SWT Browser
我遇到 Eclipse SWT 浏览器无法在一台机器上加载某些样式的问题。我希望机器使用 IE 10 作为本机浏览器,但我不确定如何确认这一点。有没有办法确定 SWT 决定使用哪个浏览器加载页面?
基于这个问题的公认答案:
How can you detect the version of a browser?
你可以这样做:
public static void main(String[] args)
{
final Display display = new Display();
final Shell shell = new Shell(display);
shell.setText("Whosebug");
shell.setLayout(new FillLayout());
Browser browser = new Browser(shell, SWT.NONE);
browser.addProgressListener(new ProgressListener()
{
@Override
public void changed(ProgressEvent progressEvent)
{
}
@Override
public void completed(ProgressEvent progressEvent)
{
System.out.println(browser.evaluate("var ua = navigator.userAgent, tem, M = ua.match(/(opera|chrome|safari|firefox|msie|trident(?=\/))\/?\s*(\d+)/i) || []; if (/trident/i.test(M[1])) { tem = /\brv[ :]+(\d+)/g.exec(ua) || []; return 'IE ' + (tem[1] || ''); } if (M[1] === 'Chrome') { tem = ua.match(/\b(OPR|Edge)\/(\d+)/); if (tem != null) return tem.slice(1).join(' ').replace('OPR', 'Opera'); } M = M[2] ? [M[1], M[2]] : [navigator.appName, navigator.appVersion, '-?']; if ((tem = ua.match(/version\/(\d+)/i)) != null) M.splice(1, 1, tem[1]); return M.join(' ');"));
}
});
browser.setUrl("https://www.google.co.uk");
shell.pack();
shell.open();
while (!shell.isDisposed())
{
if (!display.readAndDispatch())
display.sleep();
}
display.dispose();
}
在我的例子中输出 IE 11
。
我遇到 Eclipse SWT 浏览器无法在一台机器上加载某些样式的问题。我希望机器使用 IE 10 作为本机浏览器,但我不确定如何确认这一点。有没有办法确定 SWT 决定使用哪个浏览器加载页面?
基于这个问题的公认答案:
How can you detect the version of a browser?
你可以这样做:
public static void main(String[] args)
{
final Display display = new Display();
final Shell shell = new Shell(display);
shell.setText("Whosebug");
shell.setLayout(new FillLayout());
Browser browser = new Browser(shell, SWT.NONE);
browser.addProgressListener(new ProgressListener()
{
@Override
public void changed(ProgressEvent progressEvent)
{
}
@Override
public void completed(ProgressEvent progressEvent)
{
System.out.println(browser.evaluate("var ua = navigator.userAgent, tem, M = ua.match(/(opera|chrome|safari|firefox|msie|trident(?=\/))\/?\s*(\d+)/i) || []; if (/trident/i.test(M[1])) { tem = /\brv[ :]+(\d+)/g.exec(ua) || []; return 'IE ' + (tem[1] || ''); } if (M[1] === 'Chrome') { tem = ua.match(/\b(OPR|Edge)\/(\d+)/); if (tem != null) return tem.slice(1).join(' ').replace('OPR', 'Opera'); } M = M[2] ? [M[1], M[2]] : [navigator.appName, navigator.appVersion, '-?']; if ((tem = ua.match(/version\/(\d+)/i)) != null) M.splice(1, 1, tem[1]); return M.join(' ');"));
}
});
browser.setUrl("https://www.google.co.uk");
shell.pack();
shell.open();
while (!shell.isDisposed())
{
if (!display.readAndDispatch())
display.sleep();
}
display.dispose();
}
在我的例子中输出 IE 11
。