Skip to content

Instantly share code, notes, and snippets.

@mt33 mt33/gist:6071040
Last active Dec 20, 2015

Embed
What would you like to do?
WCM and CF toggles for CQ/AEM author environments(1) and (2) bookmarklet code for toggling ?wcmmode=disabled and cf#/ from the current browser location.(3) accomplishes the same as (1) but uses a cookie so that a) we don't interfere with the URL and b) the setting is persistent when navigating.Useful for CQ development.
// Note: to make a bookmarklet, take the minified code, place it within the anonymous function
// and paste into the bookmark location
javascript:(function(){/* minifed goes here */}());
// ------------------------------------------------------------------
// (0) Toggle '/editor.html' to the location
// ------------------------------------------------------------------
javascript:(function(){var e="editor.html/";var t=window.location.href;var n="";if(t.indexOf(e)!=-1){n=t.replace(e,"")}else{n=t.replace("content/",e+"content/")}window.location=n})();
// ------------------------------------------------------------------
// (1) Toggle '(?|&)wcmmode=disabled' to the location
// ------------------------------------------------------------------
(function(){
var WCMMODE_DISABLED = 'wcmmode=disabled';
// Creates the path by combining the path and query string parameters
var createURL = function(path, query_string, hash) {
var url = path;
var qs = [];
for (var i=0;i<query_string.length;i++) {
if (query_string[i] != '')
qs.push(query_string[i]);
}
if(qs.length != 0 && !(qs.length == 1 && qs[0] == '')) {
url += '?' + qs.join('&');
}
url += hash;
return url;
};
var hash = window.location.hash;
var parameters = window.location.search.substring(1).split('&');
var path = window.location.pathname;
if(parameters.indexOf(WCMMODE_DISABLED) != -1) { // present,
parameters.pop(WCMMODE_DISABLED); // so remove it
} else {
parameters.push(WCMMODE_DISABLED);
}
var url = createURL(path, parameters, hash);
window.location = url;
}());
// Minified
var d="wcmmode=disabled";var a=function(m,k,l){var h=m;var g=[];for(var j=0;j<k.length;j++){if(k[j]!=""){g.push(k[j])}}if(g.length!=0&&!(g.length==1&&g[0]=="")){h+="?"+g.join("&")}h+=l;return h};var f=window.location.hash;var c=window.location.search.substring(1).split("&");console.log(c);var e=window.location.pathname;if(c.indexOf(d)!=-1){c.pop(d)}else{c.push(d)}var b=a(e,c,f);console.log(b);window.location=b;
// ------------------------------------------------------------------
// (2) Toggle 'cf#/' to the beginning of the location
// ------------------------------------------------------------------
(function(){
var CONTENT_FINDER = 'cf#/';
var href = window.location.href;
var url = "";
if(href.indexOf(CONTENT_FINDER) != -1) {
url = href.replace(CONTENT_FINDER,''); // remove it
} else {
url = href.replace('content/', CONTENT_FINDER + 'content/'); // add it
}
window.location = url;
}());
// Minified
var e="cf#/";var t=window.location.href;var n="";if(t.indexOf(e)!=-1){n=t.replace(e,"")}else{n=t.replace("content/",e+"content/")}window.location=n;
// ------------------------------------------------------------------
// (3) Toggle 'wcmmode' cookie between 'disabled' and 'edit'
// ------------------------------------------------------------------
function get_cookie (cookie_name) {
var results = document.cookie.match ( '(^|;) ?' + cookie_name + '=([^;]*)(;|$)' );
if (results)
return ( unescape ( results[2] ) );
else
return null;
}
function set_cookie (name, value, exp_y, exp_m, exp_d, path, domain, secure) {
var cookie_string = name + "=" + escape ( value );
if (exp_y) {
var expires = new Date ( exp_y, exp_m, exp_d );
cookie_string += "; expires=" + expires.toGMTString();
}
if (path)
cookie_string += "; path=" + escape ( path );
if (domain)
cookie_string += "; domain=" + escape ( domain );
if (secure)
cookie_string += "; secure";
document.cookie = cookie_string;
}
function delete_cookie (cookie_name) {
var cookie_date = new Date ( ); // current date & time
cookie_date.setTime ( cookie_date.getTime() - 1 );
document.cookie = cookie_name += "=; expires=" + cookie_date.toGMTString();
}
if (get_cookie('wcmmode') == 'edit' || get_cookie('wcmmode') == 'design' || get_cookie('wcmmode') == 'preview' )
set_cookie('wcmmode', 'disabled');
else
set_cookie('wcmmode', 'edit');
location.reload();
// Minified:
function get_cookie(e){var t=document.cookie.match("(^|;) ?"+e+"=([^;]*)(;|$)");if(t)return unescape(t[2]);else return null}function set_cookie(e,t,n,r,i,s,o,u){var a=e+"="+escape(t);if(n){var f=new Date(n,r,i);a+="; expires="+f.toGMTString()}if(s)a+="; path="+escape(s);if(o)a+="; domain="+escape(o);if(u)a+="; secure";document.cookie=a}function delete_cookie(e){var t=new Date;t.setTime(t.getTime()-1);document.cookie=e+="=; expires="+t.toGMTString()}if(get_cookie("wcmmode")=="edit"||get_cookie("wcmmode")=="design"||get_cookie("wcmmode")=="preview")set_cookie("wcmmode","disabled");else set_cookie("wcmmode","edit");location.reload()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.