Skip to content

Instantly share code, notes, and snippets.

@filipbech
Last active August 14, 2020 12:09
Show Gist options
  • Save filipbech/7d20ddeee9dccd785632f6e0aabbe21d to your computer and use it in GitHub Desktop.
Save filipbech/7d20ddeee9dccd785632f6e0aabbe21d to your computer and use it in GitHub Desktop.
POC Umbraco edit bookmarklet
/*
Install from here: https://codepen.io/filipbech/full/RwarLVL
Before this becomes external, we should probably make the bookmarklet a loader and have the
source somewhere remote, so we can update it withot people having to re-install
*/
(async () => {
const pageIdMetaTag = document.querySelector('meta[name=umb-page-id]');
const backOfficeUrlMetaTag = document.querySelector('meta[name=umb-backoffice-url]');
let backofficeUrl = location.origin+'/umbraco'; //default
if(backOfficeUrlMetaTag && backOfficeUrlMetaTag.getAttribute('content')) {
backofficeUrl = backOfficeUrlMetaTag.getAttribute('content');
};
const go = id => window.open(`${backofficeUrl}/#/content/${ id ? `content/edit/${id}` : ''}`);
if(pageIdMetaTag && pageIdMetaTag.getAttribute('content')) {
return go(pageIdMetaTag.getAttribute('content'));
}
const locationParts = location.pathname.replace(/^\/|\/$/g,'').split('/');
const query = locationParts[locationParts.length-1];
const cookies = document.cookie
.split(';')
.map(v => v.split('='))
.reduce((acc, v) => {
acc[decodeURIComponent(v[0].trim())] = decodeURIComponent(v[1].trim());
return acc;
}, {});
const response = await fetch(`${backofficeUrl}/backoffice/UmbracoApi/Entity/SearchAll?query=${query}`, {
headers: {
accept: 'application/json, text/plain, */*',
'x-umb-xsrf-token': cookies['UMB-XSRF-TOKEN']
},
"mode": "cors",
"credentials": "include"
});
if(response.status === 404) {
return alert('Doesnt seem to be an umbraco site');
}
if(!query && document.body.dataset.siteid) {
//empty frontpage (so "/" pathname, use uno specific siteId if present)
return go(document.body.dataset.siteid);
}
if(response.status === 200) {
const resultAsText = await response.text();
const result = JSON.parse(resultAsText.substr(6));
if(result && result.Content && result.Content.results) {
const page = result.Content.results.find(res => res.metaData.Url === location.pathname);
if(page) {
// Umbraco page found - go to that one
return go(page.id);
}
}
}
// most like an umbraco site that is unauthorized (or down)
return go(null);
})();
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment