📄 browser.java
字号:
args = (String[])argsVector.toArray(args); // the windows url protocol handler doesn't work well with file URLs. // Correct those problems here before continuing // Java File.toURL() gives only one / following file: bu // we need two. // If there are escaped characters in the url, we will have // to create an Internet shortcut and open that, as the command // line version of the rundll doesn't like them. boolean useShortCut = false; if (args[0].equals("rundll32") && args[1].equals("url.dll,FileProtocolHandler")){ if (args[2].startsWith("file:/")){ if (args[2].charAt(6) != '/'){ args[2] = "file://" + args[2].substring(6); } if (args[2].charAt(7) != '/'){ args[2] = "file:///" + args[2].substring(7); } useShortCut = true; } else if (args[2].toLowerCase().endsWith("html") || args[2].toLowerCase().endsWith("htm")){ useShortCut = true; } } if (useShortCut){ File shortcut = File.createTempFile("OpenInBrowser", ".url"); shortcut = shortcut.getCanonicalFile(); shortcut.deleteOnExit(); PrintWriter out = new PrintWriter(new FileWriter(shortcut)); out.println("[InternetShortcut]"); out.println("URL=" + args[2]); out.close(); args[2] = shortcut.getCanonicalPath(); } // start the browser Process p = Runtime.getRuntime().exec(args); // give the browser a bit of time to fail. // I have found that sometimes sleep doesn't work // the first time, so do it twice. My tests // seem to show that 1000 milliseconds is enough // time for the browsers I'm using. for (int j=0; j<2; j++){ try{ Thread.currentThread().sleep(1000); } catch (InterruptedException inte){ } } if (p.exitValue() == 0){ // this is a weird case. The browser exited after // a couple seconds saying that it successfully // displayed the url. Either the browser is lying // or the user closed it *really* quickly. Oh well. found = true; } } catch (IOException x){ // the command was not a valid command. System.err.println(labels.getString("warning") + " " + x.getMessage()); } } if (!found){ // we never found a command that didn't terminate with an error. throw new IOException(labels.getString("failed")); } } catch (IllegalThreadStateException e){ // the browser is still running. This is a good sign. // lets just say that it is displaying the url right now! } } } /** * Display the URLs, each in their own window, in the system browser. * * Browser.init() should be called before calling this function or * Browser.exec should be set explicitly. * * If more than one URL is given an HTML page containing JavaScript will * be written to the local drive, that page will be opened, and it will * open the rest of the URLs. * * @param urls the list of urls to display * @throws IOException if the url is not valid or the browser fails to star * * @since ostermillerutils 1.00.00 */ public static void displayURLs(String[] urls) throws IOException { if (urls == null || urls.length == 0){ return; } if (urls.length == 1){ displayURL(urls[0]); return; } File shortcut = File.createTempFile("DisplayURLs", ".html"); shortcut = shortcut.getCanonicalFile(); shortcut.deleteOnExit(); PrintWriter out = new PrintWriter(new FileWriter(shortcut)); out.println("<html>"); out.println("<head>"); out.println("<title>" + labels.getString("html.openurls") + "</title>"); out.println("<script language=\"javascript\" type=\"text/javascript\">"); out.println("function displayURLs(){"); for (int i=1; i<urls.length; i++){ out.println("window.open(\"" + urls[i] + "\", \"_blank\", \"toolbar=yes,location=yes,directories=yes,status=yes,menubar=yes,scrollbars=yes,resizable=yes\");"); } out.println("location.href=\"" + urls[0] + "\";"); out.println("}"); out.println("</script>"); out.println("</head>"); out.println("<body onload=\"javascript:displayURLs()\">"); out.println("<noscript>"); for (int i=0; i<urls.length; i++){ out.println("<a target=\"_blank\" href=\"" + urls[i] + "\">" + urls[i] + "</a><br>"); } out.println("</noscript>"); out.println("</body>"); out.println("</html>"); out.close(); displayURL(shortcut.toURL().toString()); } /** * Display the URL in a new window. * * Uses javascript to check history.length to determine if the browser opened a * new window already. If it did, the url is shown in that window, if not, it is * shown in new window. * * Some browsers do not allow the length of history to be viewed by a web page. In that * case, the url will be displayed in the current window. * * Browser.init() should be called before calling this function or * Browser.exec should be set explicitly. * * @param url the url to display in a new window. * @throws IOException if the url is not valid or the browser fails to star * * @since ostermillerutils 1.00.00 */ public static void displayURLinNew(String url) throws IOException { displayURLsinNew (new String[] {url}); } /** * Display the URLs, each in their own window, in the system browser and the first in * the named window. * * The first URL will only be opened in the named window if the browser did no * open it in a new window to begin with. * * Browser.init() should be called before calling this function or * Browser.exec should be set explicitly. * * An html page containing javascript will * be written to the local drive, that page will be opened, and it will * open all the urls. * * @param urls the list of urls to display * @throws IOException if the url is not valid or the browser fails to star * * @since ostermillerutils 1.00.00 */ public static void displayURLsinNew(String[] urls) throws IOException { if (urls == null || urls.length == 0){ return; } File shortcut = File.createTempFile("DisplayURLs", ".html"); shortcut.deleteOnExit(); shortcut = shortcut.getCanonicalFile(); PrintWriter out = new PrintWriter(new FileWriter(shortcut)); out.println("<html>"); out.println("<head>"); out.println("<title>" + labels.getString("html.openurls") + "</title>"); out.println("<script language=\"javascript\" type=\"text/javascript\">"); out.println("function displayURLs(){"); out.println("var hlength = 0;"); out.println("try {"); out.println("hlength = history.length;"); out.println("} catch (e) {}"); out.println("if (hlength>0) {"); out.println("window.open(\"" + urls[0] + "\", \"_blank\", \"toolbar=yes,location=yes,directories=yes,status=yes,menubar=yes,scrollbars=yes,resizable=yes\");"); out.println("}"); for (int i=1; i<urls.length; i++){ out.println("window.open(\"" + urls[i] + "\", \"_blank\", \"toolbar=yes,location=yes,directories=yes,status=yes,menubar=yes,scrollbars=yes,resizable=yes\");"); } out.println("if (hlength==0) {"); out.println("location.href=\"" + urls[0] + "\";"); out.println("} else {"); out.println("history.back()"); out.println("}"); out.println("}"); out.println("</script>"); out.println("</head>"); out.println("<body onload=\"javascript:displayURLs()\">"); out.println("<noscript>"); for (int i=0; i<urls.length; i++){ out.println("<a target=\"_blank\" href=\"" + urls[i] + "\">" + urls[i] + "</a><br>"); } out.println("</noscript>"); out.println("</body>"); out.println("</html>"); out.close(); displayURL(shortcut.toURL().toString()); } /** * Display the URL in the named window. * * If the browser opens a new window by default, this will likely cause a duplicate window * to be opened. * * Browser.init() should be called before calling this function or * Browser.exec should be set explicitly. * * @param url the url to display * @param namedWindow the name of the desired window. * @throws IOException if the url is not valid or the browser fails to star * * @since ostermillerutils 1.00.00 */ public static void displayURL(String url, String namedWindow) throws IOException { displayURLs (new String[] {url}, new String[] {namedWindow}); } /** * Display the URLs in the named windows. * * If the browser opens a new window by default, this will likely cause a duplicate window * to be opened. This method relies on the browser to support javascript. * * Browser.init() should be called before calling this function or * Browser.exec should be set explicitly. * * Extra names for windows will be ignored, and if there are too few names, the remaining * windows will be named "_blank". * * @param urls the list of urls to display * @param namedWindows the list of names for the windows. * @throws IOException if the url is not valid or the browser fails to star * * @since ostermillerutils 1.00.00 */ public static void displayURLs(String[] urls, String[] namedWindows) throws IOException { if (urls == null || urls.length == 0){ return; } File shortcut = File.createTempFile("DisplayURLs", ".html"); shortcut.deleteOnExit(); shortcut = shortcut.getCanonicalFile(); PrintWriter out = new PrintWriter(new FileWriter(shortcut)); out.println("<html>"); out.println("<head>"); out.println("<title>" + labels.getString("html.openurls") + "</title>"); out.println("<base target=\"" + ((namedWindows==null||namedWindows.length==0||namedWindows[0]==null)?"_blank":namedWindows[0]) + "\">"); out.println("<script language=\"javascript\" type=\"text/javascript\">"); for (int i=1; i<urls.length; i++){ out.println("window.open(\"" + urls[i] + "\", \"" + ((namedWindows==null||namedWindows.length<=i||namedWindows[i]==null)?"_blank":namedWindows[i]) + "\", \"toolbar=yes,location=yes,directories=yes,status=yes,menubar=yes,scrollbars=yes,resizable=yes\");"); } out.println("location.href=\"" + urls[0] + "\";"); out.println("</script>"); out.println("</head>"); out.println("<body onload=\"javascript:displayURLs()\">"); out.println("<noscript>"); for (int i=0; i<urls.length; i++){ out.println("<a target=\"" + ((namedWindows==null||namedWindows.length==0||namedWindows[0]==null)?"_blank":namedWindows[0]) + "\" href=\"" + urls[i] + "\">" + urls[i] + "</a><br>"); } out.println("</noscript>"); out.println("</body>"); out.println("</html>"); out.close(); displayURL(shortcut.toURL().toString()); } /** * Display the URLs the first in the given named window. * * If the browser opens a new window by default, this will likely cause a duplicate window * to be opened. This method relies on the browser to support javascript. * * Browser.init() should be called before calling this function or * Browser.exec should be set explicitly. * * @param urls the list of urls to display * @param namedWindow the name of the first window to use. * @throws IOException if the url is not valid or the browser fails to star * * @since ostermillerutils 1.00.00 */ public static void displayURLs(String[] urls, String namedWindow) throws IOException { displayURLs(urls, new String[] {namedWindow}); } /** * Open the url(s) specified on the command line in your browser. * * @param args Command line arguments (URLs) */ public static void main(String[] args){ try { Browser.init(); if (Browser.dialogConfiguration(null)){ if (args.length == 0){ Browser.displayURLs(new String[]{ "http://www.google.com/", "http://dmoz.org/", "http://ostermiller.org", }, "fun"); } else if (args.length == 1){ Browser.displayURL(args[0], "fun"); } else { Browser.displayURLs(args, "fun"); } } try { Thread.sleep(10000); } catch (InterruptedException x){ } } catch (IOException e){ System.err.println(e.getMessage()); } System.exit(0); } /** * Show a dialog that allows the user to configure the * command lines used for starting a browser on their system. * * @param owner The frame that owns the dialog. * * @since ostermillerutils 1.00.00 */ public static boolean dialogConfiguration(Frame owner){ dialogConfiguration(owner, null); return Browser.dialog.changed(); } /** * Show a dialog that allows the user to configure the * command lines used for starting a browser on their system. * String used in the dialog are taken from the given * properties. This dialog can be customized or displayed in * multiple languages. * <P> * Properties that are used: * com.Ostermiller.util.BrowserDialog.title<br> * com.Ostermiller.util.BrowserDialog.description<br> * com.Ostermiller.util.BrowserDialog.label<br> * com.Ostermiller.util.BrowserDialog.defaults<br> * com.Ostermiller.util.BrowserDialog.browse<br> * com.Ostermiller.util.BrowserDialog.ok<br> * com.Ostermiller.util.BrowserDialog.cancel<br> * * @param owner The frame that owns this dialog. * @param props contains the strings used in the dialog. * @deprecated Use the com.Ostermiller.util.Browser resource bundle to set strings for the given locale. * * @since ostermillerutils 1.00.00 */ public static boolean dialogConfiguration(Frame owner, Properties props){ if (Browser.dialog == null){
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -