From b6e2299c93d62681bd2f052de320cba13149c231 Mon Sep 17 00:00:00 2001 From: Helena Rasche Date: Wed, 3 Feb 2021 19:22:41 +0100 Subject: [PATCH] Persist scroll/url information, in both chrome & firefox --- config/plugins/webhooks/gtn/script.js | 65 ++++++++++++++++++++++++++- 1 file changed, 63 insertions(+), 2 deletions(-) diff --git a/config/plugins/webhooks/gtn/script.js b/config/plugins/webhooks/gtn/script.js index 0d98fdaac03..0d395ab2052 100644 --- a/config/plugins/webhooks/gtn/script.js +++ b/config/plugins/webhooks/gtn/script.js @@ -1,4 +1,5 @@ var gtnWebhookLoaded = false; +var lastUpdate = 0; function removeOverlay() { document.getElementById("gtn-container").style.visibility = "hidden"; @@ -8,9 +9,44 @@ function showOverlay() { document.getElementById("gtn-container").style.visibility = "visible"; } +function getIframeUrl(){ + var loc; + try { + loc = document.getElementById("gtn-embed").contentWindow.location.pathname; + } catch (e) { + loc = null; + } + return loc +} + +function getIframeScroll() { + var loc; + try { + loc = parseInt(document.getElementById("gtn-embed").contentWindow.scrollY); + } catch (e) { + loc = 0; + } + return loc +} + +function restoreLocation() { +} + +function persistLocation() { + // Don't save every scroll event. + var time = new Date().getTime(); + if ( time - lastUpdate < 1000 ) { + return; + } + lastUpdate = time; + window.localStorage.setItem('gtn-in-galaxy', `${getIframeScroll()} ${getIframeUrl()}`); +} + function addIframe() { - let url, message; + let url, message, onloadscroll; gtnWebhookLoaded = true; + let storedData = false; + let safe = false; // Test for the presence of /training-material/. If that is available we // can opt in the fancy click-to-run features. Otherwise we fallback to @@ -24,7 +60,15 @@ function addIframe() { Click to run unavailable. `; } else { - url = "/training-material/"; + safe = true; + + var storedLocation = window.localStorage.getItem('gtn-in-galaxy'); + if(storedLocation !== null && storedLocation.split(' ')[1] !== undefined && storedLocation.split(' ')[1].startsWith('/training-material/')) { + onloadscroll = storedLocation.split(' ')[0]; + url = storedLocation.split(' ')[1]; + } else { + url = "/training-material/"; + } message = ""; } }) @@ -48,8 +92,25 @@ function addIframe() { removeOverlay(); }); + // Only setup the listener if it won't crash things. + if(safe) { + // Listen to the scroll position + document.getElementById("gtn-embed").contentWindow.addEventListener('scroll', () => { + persistLocation(); + }); + } + // Depends on the iframe being present document.getElementById("gtn-embed").addEventListener("load", () => { + // Save our current location when possible + if(onloadscroll !== undefined){ + document.getElementById('gtn-embed').contentWindow.scrollTo(0, parseInt(onloadscroll)); + onloadscroll = undefined; + } + + if(safe) { + persistLocation(); + } var gtn_tools = $("#gtn-embed").contents().find("span[data-tool]"); // Buttonify gtn_tools.addClass("galaxy-proxy-active");