|
| 1 | +/* inspired by https://tobiasahlin.com/blog/move-from-jquery-to-vanilla-javascript/ */ |
| 2 | + |
| 3 | +function documentReady(callback) { |
| 4 | + if (document.readyState != "loading") callback(); |
| 5 | + else document.addEventListener("DOMContentLoaded", callback); |
| 6 | +} |
| 7 | + |
| 8 | +async function getRelease() { |
| 9 | + result = await fetch("https://api.github.com/repos/mne-tools/mne-installers/releases/latest"); |
| 10 | + data = await result.json(); |
| 11 | + return data; |
| 12 | +} |
| 13 | +async function warnVersion() { |
| 14 | + data = await getRelease(); |
| 15 | + // Take v1.5.1 for example and change to 1.5 |
| 16 | + ids = ["linux-installers", "macos-intel-installers", "macos-apple-installers", "windows-installers"]; |
| 17 | + warn = false; |
| 18 | + ids.forEach((id) => { |
| 19 | + label_id = document.getElementById(id); |
| 20 | + // tab is immediately after label |
| 21 | + children = [].slice.call(label_id.parentNode.children); |
| 22 | + div = children[children.indexOf(label_id) + 1]; |
| 23 | + a = div.children[0].children[0]; // div->p->a |
| 24 | + ending = a.href.split("-").slice(-1)[0]; // Should be one of: ["macOS_Intel.pkg", "macOS_M1.pkg", "Linux.sh", "Windows.exe"] |
| 25 | + data["assets"].every((asset) => { |
| 26 | + // find the matching asset |
| 27 | + if (!asset["browser_download_url"].endsWith(ending)) { |
| 28 | + return true; // continue |
| 29 | + } |
| 30 | + old_stem = a.href.split("/").slice(-1)[0]; |
| 31 | + new_stem = asset["browser_download_url"].split("/").slice(-1)[0]; |
| 32 | + a.href = asset["browser_download_url"]; |
| 33 | + // also replace the command on Linux |
| 34 | + if (ending === "Linux.sh") { |
| 35 | + code = document.getElementById("codecell0"); |
| 36 | + } |
| 37 | + if (!warn) { |
| 38 | + // MNE-Python-1.5.1_0-Linux.sh to 1.5 for example |
| 39 | + old_ver = old_stem.split("-").slice(2)[0].split("_")[0].split(".").slice(0, 2).join("."); |
| 40 | + new_ver = new_stem.split("-").slice(2)[0].split("_")[0].split(".").slice(0, 2).join("."); |
| 41 | + if (old_ver !== new_ver) { |
| 42 | + warn = `The installers below are for version ${new_ver} as ${old_ver} is no longer supported`; |
| 43 | + } |
| 44 | + } |
| 45 | + return false; // do not continue |
| 46 | + }); |
| 47 | + }); |
| 48 | + if (warn) { |
| 49 | + let outer = document.createElement("div"); |
| 50 | + let title = document.createElement("p"); |
| 51 | + let inner = document.createElement("p"); |
| 52 | + outer.setAttribute("class", "admonition warning"); |
| 53 | + title.setAttribute("class", "admonition-title"); |
| 54 | + title.innerText = "Warning"; |
| 55 | + inner.innerText = warn; |
| 56 | + outer.append(title, inner); |
| 57 | + document.querySelectorAll('.platform-selector-tabset')[0].before(outer); |
| 58 | + } |
| 59 | +} |
| 60 | + |
| 61 | +documentReady(warnVersion); |
0 commit comments