cleaning up the global javascript

This commit is contained in:
Gordon Pedersen 2024-07-16 10:42:43 +10:00
parent 905257789b
commit cee1c3dfd2
3 changed files with 33 additions and 48 deletions

View file

@ -18,7 +18,7 @@
<link href="/vendor/cm-material.css" rel="stylesheet" /> <link href="/vendor/cm-material.css" rel="stylesheet" />
<script src="/_content/PSC.Blazor.Components.MarkdownEditor/js/easymde.min.js"></script> <script src="/_content/PSC.Blazor.Components.MarkdownEditor/js/easymde.min.js"></script>
<script src="/_content/PSC.Blazor.Components.MarkdownEditor/js/markdownEditor.js"></script> <script src="/_content/PSC.Blazor.Components.MarkdownEditor/js/markdownEditor.js"></script>
<script src="/js/csharp.js"></script> <script src="/js/script.js"></script>
<link rel="icon" type="image/png" href="favicon.png" /> <link rel="icon" type="image/png" href="favicon.png" />
</head> </head>

View file

@ -1,47 +0,0 @@
window.injectCSharp = async function (helper) {
window.CSHARP = helper
}
async function delay(t) {
return new Promise((resolve) => {
setTimeout(resolve, t);
});
}
async function removeElementById(id) {
document.getElementById(id)?.remove()
}
function scrollToId(id) {
const element = document.getElementById(id);
if (element instanceof HTMLElement) {
element.scrollIntoView({
behavior: "smooth",
block: "start",
inline: "nearest"
});
}
}
function toggleDetails(id) {
const element = document.getElementById(id)
if (element instanceof HTMLDetailsElement)
element.open = !element.open
}
function cacheBust(url) {
fetch(new Request(url), {
headers: new Headers({
"pragma": "no-cache",
"cache-control": "no-cache"
}),
mode: 'no-cors',
cache: 'no-cache',
})
.finally(() => {
let els = document.querySelectorAll(`[src="${url}"]`)
els.forEach(el => el.removeAttribute('src'))
els.forEach(el => el.src = url)
})
}

32
wwwroot/js/script.js Normal file
View file

@ -0,0 +1,32 @@
window.injectCSharp = async function (helper) { window.CSHARP = helper }
async function delay(t) { return new Promise((resolve) => { setTimeout(resolve, t) }) }
async function removeElementById(id) { document.getElementById(id)?.remove() }
function scrollToId(id) {
const element = document.getElementById(id)
if (element instanceof HTMLElement)
element.scrollIntoView({behavior: "smooth",block: "start",inline: "nearest"})
}
function toggleDetails(id) {
const element = document.getElementById(id)
if (element instanceof HTMLDetailsElement) element.open = !element.open
}
function cacheBust(url) {
fetch(new Request(url), {
headers: new Headers({
"pragma": "no-cache",
"cache-control": "no-cache"
}),
mode: 'no-cors',
cache: 'no-cache',
})
.finally(() => {
let els = document.querySelectorAll(`[src="${url}"]`)
els.forEach(el => el.removeAttribute('src'))
els.forEach(el => el.src = url)
})
}