From 40d0592b32498b0e7a49420b5c450d83e33fcf89 Mon Sep 17 00:00:00 2001 From: Adriaan <1079135+adriaandotcom@users.noreply.github.com> Date: Tue, 24 Jun 2025 19:05:34 +0200 Subject: [PATCH] feat(auto-events): allow cmd-click to open in new tab --- dist/latest/auto-events.js | 4 +-- src/auto-events.js | 41 ++++++++++++++--------- test/unit/auto-events-cmdclick.test.js | 45 ++++++++++++++++++++++++++ 3 files changed, 72 insertions(+), 18 deletions(-) create mode 100644 test/unit/auto-events-cmdclick.test.js diff --git a/dist/latest/auto-events.js b/dist/latest/auto-events.js index ad6fe1f..f143c39 100644 --- a/dist/latest/auto-events.js +++ b/dist/latest/auto-events.js @@ -1,4 +1,4 @@ -/* Simple Analytics - Privacy-first analytics (docs.simpleanalytics.com/script; 2025-06-13; 7048; v12) */ +/* Simple Analytics - Privacy-first analytics (docs.simpleanalytics.com/script; 2025-06-13; 644f; v12) */ -function r(t,e){var a,n;t.hasAttribute("data-simple-event")||(a=!1,h.downloads&&/^https?:\/\//i.test(t.href)&&new RegExp("\\.("+(h.downloadsExtensions||[]).join("|")+")$","i").test(t.pathname)?a="download":h.outbound&&/^https?:\/\//i.test(t.href)&&t.hostname!==m.location.hostname?a="outbound":h.emails&&/^mailto:/i.test(t.href)&&(a="email"),a&&(e?(n="saAutomatedLink(this, '"+a+"');",t.hasAttribute("target")&&"_self"!==t.getAttribute("target")||(n+=" return false;"),t.setAttribute("onclick",n)):t.addEventListener("click",function(){g(t,a)})))}function e(){try{for(var t=document.getElementsByTagName("a"),e=0;eDoc'; + const dom = new JSDOM(html, { + url: "https://simpleanalytics.com/", + runScripts: "outside-only", + pretendToBeVisual: true, + }); + const context = dom.getInternalVMContext(); + vm.runInContext( + "window.sa_event = function(){}; window.sa_event_loaded = true;", + context + ); + const script = readFileSync("dist/latest/auto-events.js", "utf8"); + vm.runInContext(script, context); + const link = dom.window.document.getElementById("doc"); + const normalEvent = new dom.window.MouseEvent("click", { + bubbles: true, + cancelable: true, + }); + const metaEvent = new dom.window.MouseEvent("click", { + bubbles: true, + cancelable: true, + metaKey: true, + }); + + const normalReturn = dom.window.saAutomatedLink( + link, + "outbound", + normalEvent + ); + const metaReturn = dom.window.saAutomatedLink(link, "outbound", metaEvent); + + expect(normalEvent.defaultPrevented).to.equal(true); + expect(metaEvent.defaultPrevented).to.equal(false); + expect(normalReturn).to.equal(false); + expect(metaReturn).to.equal(true); + }); +});