From 8130ce48453868e974667cab41b7f3cca2fb205c Mon Sep 17 00:00:00 2001 From: Noah Petherbridge Date: Mon, 27 Nov 2023 21:23:31 -0800 Subject: [PATCH] Right-click dissuasion --- pkg/templates/templates.go | 27 ++++++++++++++++++++------- web/static/js/bulma.js | 34 ++++++++++++++++++---------------- web/static/js/right-click.js | 17 +++++++++++++++++ web/templates/base.html | 36 ++++++++++++++++++++++++++++++++++++ 4 files changed, 91 insertions(+), 23 deletions(-) create mode 100644 web/static/js/right-click.js diff --git a/pkg/templates/templates.go b/pkg/templates/templates.go index 64131e8..5816436 100644 --- a/pkg/templates/templates.go +++ b/pkg/templates/templates.go @@ -75,13 +75,9 @@ func (t *Template) Execute(w http.ResponseWriter, r *http.Request, vars map[stri } // Reload the template from disk? - if stat, err := os.Stat(t.filepath); err == nil { - if stat.ModTime().After(t.modified) { - log.Info("Template(%s).Execute: file updated on disk, reloading", t.filename) - err = t.Reload() - if err != nil { - log.Error("Reloading error: %s", err) - } + if t.IsModifiedLocally() { + if err := t.Reload(); err != nil { + log.Error("Reloading error: %s", err) } } @@ -98,6 +94,23 @@ func (t *Template) Execute(w http.ResponseWriter, r *http.Request, vars map[stri return nil } +// IsModifiedLocally checks if any of the template partials of your Template have +// had their files locally on disk modified, so to know to reload them. +func (t *Template) IsModifiedLocally() bool { + // Check all the template files from base.html, to partials, to our filepath. + var files = templates(t.filepath) + for _, filename := range files { + if stat, err := os.Stat(filename); err == nil { + if stat.ModTime().After(t.modified) { + log.Info("Template(%s).Execute: file %s updated on disk, reloading", t.filename, filename) + return true + } + } + } + + return false +} + // Reload the template from disk. func (t *Template) Reload() error { stat, err := os.Stat(t.filepath) diff --git a/web/static/js/bulma.js b/web/static/js/bulma.js index a3e41f5..b954066 100644 --- a/web/static/js/bulma.js +++ b/web/static/js/bulma.js @@ -12,12 +12,12 @@ document.addEventListener('DOMContentLoaded', () => { }); // Hamburger menu script. - (function() { + (function () { // Get all "navbar-burger" elements const $navbarBurgers = Array.prototype.slice.call(document.querySelectorAll('.navbar-burger'), 0); // Add a click event on each of them - $navbarBurgers.forEach( el => { + $navbarBurgers.forEach(el => { el.addEventListener('click', () => { // Get the target from the "data-target" attribute @@ -33,7 +33,7 @@ document.addEventListener('DOMContentLoaded', () => { })(); // Allow the "More" drop-down to work on mobile (toggle is-active on click instead of requiring mouseover) - (function() { + (function () { const menu = document.querySelector("#navbar-more"), userMenu = document.querySelector("#navbar-user"), activeClass = "is-active"; @@ -69,9 +69,9 @@ document.addEventListener('DOMContentLoaded', () => { // Touching a link from the user menu lets it click thru. (document.querySelectorAll(".navbar-dropdown") || []).forEach(node => { - node.addEventListener("touchstart", (e) => { - e.stopPropagation(); - }); + node.addEventListener("touchstart", (e) => { + e.stopPropagation(); + }); }); // Clicking the rest of the body will close an active navbar-dropdown. @@ -85,7 +85,9 @@ document.addEventListener('DOMContentLoaded', () => { // Common event handlers for bulma modals. (document.querySelectorAll(".modal-background, .modal-close, .photo-modal") || []).forEach(node => { const target = node.closest(".modal"); - if (target.classList.contains("vue-managed")) return; + if (target.classList.contains("vue-managed") || target.classList.contains("nonshy-important-modal")) { + return; + } node.addEventListener("click", () => { target.classList.remove("is-active"); }); @@ -131,21 +133,21 @@ document.addEventListener('DOMContentLoaded', () => { // Reveal all blurred images on click. (document.querySelectorAll(".blurred-explicit") || []).forEach(node => { node.addEventListener("click", e => { - if (node.classList.contains("blurred-explicit")) { - node.classList.remove("blurred-explicit"); - if (node.tagName !== "VIDEO") { - e.preventDefault(); - e.stopPropagation(); - } + if (node.classList.contains("blurred-explicit")) { + node.classList.remove("blurred-explicit"); + if (node.tagName !== "VIDEO") { + e.preventDefault(); + e.stopPropagation(); } + } }); // Video tag: autoplay is disabled when blurred, onClick doesn't fire, // set the handler for onPlay. node.addEventListener("play", e => { - if (node.classList.contains("blurred-explicit")) { - node.classList.remove("blurred-explicit"); - } + if (node.classList.contains("blurred-explicit")) { + node.classList.remove("blurred-explicit"); + } }); }); }); diff --git a/web/static/js/right-click.js b/web/static/js/right-click.js new file mode 100644 index 0000000..1e8e2ea --- /dev/null +++ b/web/static/js/right-click.js @@ -0,0 +1,17 @@ +// Right-click button handler, to dissuade downloading. +document.addEventListener('DOMContentLoaded', () => { + const $modal = document.querySelector("#rightclick-modal"), + $button = $modal.querySelector("button"), + cls = 'is-active'; + + console.log("register right clicks"); + console.log($modal, $button); + + document.addEventListener('contextmenu', (e) => { + $modal.classList.add(cls); + e.preventDefault(); + }); + $button.addEventListener('click', () => { + $modal.classList.remove(cls); + }); +}); diff --git a/web/templates/base.html b/web/templates/base.html index 0dfc4d3..c202ac5 100644 --- a/web/templates/base.html +++ b/web/templates/base.html @@ -368,6 +368,42 @@ {{template "like-modal"}} + + + {{end}}