From 4a3b25ce20695ba2caa1b79e232a5e7570f3cf43 Mon Sep 17 00:00:00 2001 From: Matthias Bussonnier Date: Tue, 5 Mar 2024 11:26:55 +0100 Subject: [PATCH] Draft: Think of dialog to confirm whether to load JupyterLight This is not perfect, in particular the `confirm(...)` is a blocking call, which is not supposed to be used in a OnClik handler as an onclick handler need to respond fast. I think this need to be refactored into utilities functions, and setTimeout(0, callback), and/or Promises. That way the onClick finishes immediately and does not violate the timeout limit. As noted in the comment, the navigator api is experimental, so it woudl be good to test properly. In Web Inspector (Firefox and Chome at least), it is possible to artificially throttle the connection to test the logic. --- jupyterlite_sphinx/jupyterlite_sphinx.js | 25 ++++++++++++++++++++++++ 1 file changed, 25 insertions(+) diff --git a/jupyterlite_sphinx/jupyterlite_sphinx.js b/jupyterlite_sphinx/jupyterlite_sphinx.js index 4ae8c96..d963b47 100644 --- a/jupyterlite_sphinx/jupyterlite_sphinx.js +++ b/jupyterlite_sphinx/jupyterlite_sphinx.js @@ -52,6 +52,11 @@ window.jupyterliteConcatSearchParams = (iframeSrc, params) => { } }; +/* + * This is the callback that will be triggered when a user press the button + * to show this iframe. + * + */ window.tryExamplesShowIframe = ( examplesContainerId, iframeContainerId, @@ -59,6 +64,26 @@ window.tryExamplesShowIframe = ( iframeSrc, iframeHeight, ) => { + // this is an experimental API + // https://developer.mozilla.org/en-US/docs/Web/API/Navigator/connection + const networkinfo = navigator.connection || {}; + const connectiontype = networkinfo.effectiveType || ""; + if ( + networkinfo.saveData == true || + connectiontype.indexOf("2g") !== -1 || + connectiontype.indexOf("3g") !== -1 + ) { + console.log( + "We detected either a mettered or slow connection. Asking user if they are sure.", + ); + const res = confirm( + "Opening JupyterLite may download several hundreds Mb of data. Do you wish to continue ?", + ); + if (res === false) { + return; + } + } + const examplesContainer = document.getElementById(examplesContainerId); const iframeParentContainer = document.getElementById( iframeParentContainerId,