Skip to content

Commit

Permalink
Clicking a DOI link in the preview pane no longer crashes (#8255)
Browse files Browse the repository at this point in the history
* Fixed DOI link causing exception; now opens in browser

* Added to changelog

* Fixed checkstyle issue

* Removed awt package references

* Browser opens with JabrefDesktop

* Switched to using LOGGER
  • Loading branch information
evanpickett committed Nov 20, 2021
1 parent 578c106 commit d0d3d5e
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 0 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,7 @@ Note that this project **does not** adhere to [Semantic Versioning](http://semve
- The default directory of the "LaTeX Citations" tab is now the directory of the currently opened database (and not the directory chosen at the last open file dialog or the last database save) [koppor#538](https://github.com/koppor/jabref/issues/538)
- We fixed an issue where right-clicking on a tab and selecting close will close the focused tab even if it is not the tab we right-clicked [#8193](https://github.com/JabRef/jabref/pull/8193)
- We fixed an issue where selecting a citation style in the preferences would sometimes produce an exception [#7860](https://github.com/JabRef/jabref/issues/7860)
- We fixed an issue where an exception would occur when clicking on a DOI link in the preview pane [#7706](https://github.com/JabRef/jabref/issues/7706)

### Removed

Expand Down
27 changes: 27 additions & 0 deletions src/main/java/org/jabref/gui/preview/PreviewViewer.java
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
package org.jabref.gui.preview;

import java.io.IOException;
import java.net.MalformedURLException;
import java.util.Objects;
import java.util.Optional;
import java.util.regex.Pattern;
Expand All @@ -17,6 +19,7 @@
import org.jabref.gui.DialogService;
import org.jabref.gui.Globals;
import org.jabref.gui.StateManager;
import org.jabref.gui.desktop.JabRefDesktop;
import org.jabref.gui.util.BackgroundTask;
import org.jabref.gui.util.TaskExecutor;
import org.jabref.gui.util.Theme;
Expand All @@ -30,6 +33,10 @@
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.w3c.dom.Document;
import org.w3c.dom.Node;
import org.w3c.dom.NodeList;
import org.w3c.dom.events.EventTarget;
import org.w3c.dom.html.HTMLAnchorElement;

/**
* Displays an BibEntry using the given layout format.
Expand Down Expand Up @@ -137,6 +144,26 @@ public PreviewViewer(BibDatabaseContext database, DialogService dialogService, S
registered = true;
}
highlightSearchPattern();

// https://stackoverflow.com/questions/15555510/javafx-stop-opening-url-in-webview-open-in-browser-instead
NodeList anchorList = previewView.getEngine().getDocument().getElementsByTagName("a");
for (int i = 0; i < anchorList.getLength(); i++) {
Node node = anchorList.item(i);
EventTarget eventTarget = (EventTarget) node;
eventTarget.addEventListener("click", evt -> {
EventTarget target = evt.getCurrentTarget();
HTMLAnchorElement anchorElement = (HTMLAnchorElement) target;
String href = anchorElement.getHref();
try {
JabRefDesktop.openBrowser(href);
} catch (MalformedURLException exception) {
LOGGER.error("Invalid URL", exception);
} catch (IOException exception) {
LOGGER.error("Invalid URL Input", exception);
}
evt.preventDefault();
}, false);
}
});
}

Expand Down

0 comments on commit d0d3d5e

Please sign in to comment.