Created
August 26, 2016 14:40
-
-
Save suture/17e7568e10bd743de2e8999426c0a416 to your computer and use it in GitHub Desktop.
Admin Edit Link for Shopify
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<!-- /snippets/admin-edit-link.liquid --> | |
{% capture CFH %}{{ content_for_header }}{% endcapture %}{{ CFH }} | |
{% if CFH contains 'admin_bar_iframe' %} | |
{% assign admin = true %} | |
{% endif %} | |
{% if admin %} | |
<script> | |
(function(){ | |
var lang = { | |
'no':'Sorry, but you cannot use this here.', | |
'not_json':'That does not look like json.' | |
}; | |
var isJson = function(a) { | |
try { JSON.parse(a) } catch (e) { return false } | |
return true; | |
}; | |
var nope = function(a){ | |
if(typeof a === 'undefined'){ var a = lang.no } | |
console.warn(a) | |
return false | |
} | |
var getJsonPath = function(){ | |
return [location.protocol, '//', location.host,location.pathname,'.json'].join('') | |
}; | |
var getJson = function(u,c){ | |
var req = new XMLHttpRequest(); | |
req.open('GET', u, false); | |
req.onreadystatechange = function() { | |
if (req.readyState == 4 && req.status == 200) { | |
if(typeof c === 'function'){ | |
if(!isJson(req.responseText)){ return nope(lang.not_json) } | |
var json = JSON.parse(req.responseText); | |
c(json,Object.keys(json)[0]); | |
} | |
} | |
}; | |
req.send(); | |
}; | |
var button = document.createElement('a'); | |
button.id = 'edit-link'; | |
button.style = ''; | |
button.innerHTML = 'EDIT'; | |
button.addEventListener('click',function(e){ | |
e.target.blur(); | |
if(location.pathname === '/'){ return nope() } | |
if(location.pathname.split('/')[1] === 'blogs'){ | |
/* fallback to the tracking script data, should it exist */ | |
if(typeof __st !== 'undefined' && __st.s){ | |
location.href = [location.protocol, '//', location.host,'/admin/articles/',__st.s.split('-')[1]].join(''); | |
return false; | |
} | |
return nope() | |
} | |
getJson(getJsonPath(),function(a,b){ | |
location.href = [location.protocol, '//', location.host,'/admin/',b+'s/',a[b].id].join(''); | |
}) | |
return false; | |
}); | |
document.getElementsByTagName('body')[0].appendChild(button); | |
})() | |
</script> | |
{% else %} | |
<!-- User is not an admin --> | |
{% endif %} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment