[Fix] Ketcher submission now recognized (#213)

This will hack the ketcher submission to work again (see #207).
The problem seems to be that the iframe loads slower than the script tag so the reference is not available on page load.

Registering from within the code to poll until ketcher is ready is a bit messy.
Tracked the introduced dept in #212.

Reviewed-on: enviPath/enviPy#213
Co-authored-by: Tobias O <tobias.olenyi@envipath.com>
Co-committed-by: Tobias O <tobias.olenyi@envipath.com>
This commit is contained in:
2025-11-13 23:27:29 +13:00
committed by jebus
parent e60052b05c
commit d584791ee8
2 changed files with 90 additions and 10 deletions

View File

@ -138,6 +138,25 @@
</div>
{# prettier-ignore-start #}
<script>
// Helper function to safely get Ketcher instance from iframe
function getKetcherInstance(iframeId) {
const ketcherFrame = document.getElementById(iframeId);
if (!ketcherFrame) {
console.error("Ketcher iframe not found:", iframeId);
return null;
}
try {
if ('contentWindow' in ketcherFrame && ketcherFrame.contentWindow.ketcher) {
return ketcherFrame.contentWindow.ketcher;
}
} catch (err) {
console.error("Cannot access Ketcher iframe - possible CORS issue:", err);
}
return null;
}
function predictKetcherToTextInput() {
$("#predict-smiles").val(this.ketcher.getSmiles());
}
@ -172,10 +191,21 @@
let smiles = $("#predict-smiles").val().trim();
// If SMILES input is empty, try to get from Ketcher
if (!smiles && window.predictKetcher) {
smiles = window.predictKetcher.getSmiles().trim();
if (smiles) {
$("#predict-smiles").val(smiles);
if (!smiles) {
const ketcher = getKetcherInstance('predict-ketcher');
if (ketcher && ketcher.getSmiles) {
try {
smiles = ketcher.getSmiles().trim();
if (smiles) {
$("#predict-smiles").val(smiles);
}
} catch (err) {
console.error("Failed to get SMILES from Ketcher:", err);
alert("Unable to extract structure from the drawing editor. Please enter a SMILES string instead.");
button.prop("disabled", false);
button.text("Predict");
return;
}
}
}