mpa-enhancer
Advanced tools
Comparing version 2.1.0 to 2.1.1
@@ -14,3 +14,3 @@ // @ts-check | ||
function hasAttr(el, name) { | ||
return el.hasAttribute(name) | ||
return el?.hasAttribute(name) | ||
} | ||
@@ -51,5 +51,6 @@ | ||
|| query(`[name="${name}"]`) | ||
if (!active || hasAttr(active, 'mpa-skip-focus')) return | ||
run('focus', active) | ||
run('select', active) | ||
if (!hasAttr(active, 'mpa-skip-focus')) { | ||
run('focus', active) | ||
run('select', active) | ||
} | ||
@@ -68,3 +69,3 @@ if (!hasAttr(doc.body, 'mpa-skip-scroll') | ||
function run(method, el) { | ||
el[method] && el[method]() | ||
el && el[method] && el[method]() | ||
} | ||
@@ -71,0 +72,0 @@ |
{ | ||
"name": "mpa-enhancer", | ||
"version": "2.1.0", | ||
"version": "2.1.1", | ||
"description": "Minimalist JavaScript to make your MPA work that much better", | ||
@@ -5,0 +5,0 @@ "main": "src/mpa.js", |
@@ -14,2 +14,6 @@ # MPA Enhancer | ||
**2.1.1**: | ||
Fixed no element to focus on skipping scroll. | ||
**2.1.0**: | ||
@@ -16,0 +20,0 @@ |
@@ -14,3 +14,3 @@ // @ts-check | ||
function hasAttr(el, name) { | ||
return el.hasAttribute(name) | ||
return el?.hasAttribute(name) | ||
} | ||
@@ -51,5 +51,6 @@ | ||
|| query(`[name="${name}"]`) | ||
if (!active || hasAttr(active, 'mpa-skip-focus')) return | ||
run('focus', active) | ||
run('select', active) | ||
if (!hasAttr(active, 'mpa-skip-focus')) { | ||
run('focus', active) | ||
run('select', active) | ||
} | ||
@@ -68,3 +69,3 @@ if (!hasAttr(doc.body, 'mpa-skip-scroll') | ||
function run(method, el) { | ||
el[method] && el[method]() | ||
el && el[method] && el[method]() | ||
} | ||
@@ -71,0 +72,0 @@ |
@@ -1,1 +0,1 @@ | ||
(()=>{let t=document,a=window,r=t.querySelector.bind(t);function i(e,l){return e.hasAttribute(l)}function o(){return new URL(t.location.href).pathname.replace(/\/$/,"")}let c=null;a.addEventListener("click",e=>{c=e.target}),a.addEventListener("beforeunload",()=>{let e=t.activeElement,l=t.activeElement===t.body?c:e;localStorage.pageLocation=JSON.stringify({href:o(),y:a.scrollY,height:t.body.scrollHeight,active:{id:l?.id,name:l?.getAttribute("name")}})});function s(){if(r("[autofocus]"))return;let e=localStorage.pageLocation;if(!e)return;let{y:l,height:d,href:f,active:{id:g,name:h}}=JSON.parse(e),n=t.getElementById(g)||r(`[name="${h}"]`);!n||i(n,"mpa-skip-focus")||(u("focus",n),u("select",n),!i(t.body,"mpa-skip-scroll")&&f===o()&&l&&a.scrollTo({top:l+t.body.scrollHeight-d}))}function u(e,l){l[e]&&l[e]()}s()})(); | ||
(()=>{let l=document,a=window,n=l.querySelector.bind(l);function o(e,t){return e?.hasAttribute(t)}function r(){return new URL(l.location.href).pathname.replace(/\/$/,"")}let c=null;a.addEventListener("click",e=>{c=e.target}),a.addEventListener("beforeunload",()=>{let e=l.activeElement,t=l.activeElement===l.body?c:e;localStorage.pageLocation=JSON.stringify({href:r(),y:a.scrollY,height:l.body.scrollHeight,active:{id:t?.id,name:t?.getAttribute("name")}})});function s(){if(n("[autofocus]"))return;let e=localStorage.pageLocation;if(!e)return;let{y:t,height:d,href:f,active:{id:g,name:h}}=JSON.parse(e),i=l.getElementById(g)||n(`[name="${h}"]`);o(i,"mpa-skip-focus")||(u("focus",i),u("select",i)),!o(l.body,"mpa-skip-scroll")&&f===r()&&t&&a.scrollTo({top:t+l.body.scrollHeight-d})}function u(e,t){t&&t[e]&&t[e]()}s()})(); |
43149
1409
29