Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
javascript bookmarklet to edit cookies on a page
(function (w, d) {
var id = (new Date()).valueOf(),
itemTemplate = "<label>"
+ "<code>{name}=</code>"
+ "<input id='Cookie{name}{index}{id}' type='text' value='{value}' style='font-family:monospace;width:50%' />"
+ "</label>"
+ "<input type='button' value='Update' onclick='window.UpdateCookie{id}(\"{name}\", {index});' />"
+ "<input type='button' value='Delete' onclick='window.DeleteCookie{id}(\"{name}\");' />"
+ "<br/>",
formatTemplate = function (name, value, index) {
name = name.replace(/'/, '&apos;');
value = value.replace(/'/, '&apos;');
return itemTemplate.replace(/\{id\}/gi, id)
.replace(/\{name\}/gi, name)
.replace(/\{value\}/gi, value)
.replace(/\{index\}/gi, index);
},
saveCookie = function (name, value) {
// if path or domain or expires are needed, just append to value before calling
d.cookie = name + "=" + value;
},
div = d.createElement("div"),
body = d.getElementsByTagName("body")[0],
outputHTML = "",
y = new Date(),
e = "",
domain = "; domain=",
p = "; path=",
// set domains
ds = d.domain.split("."),
domainTLD = ds[ds.length - 1],
domainString = domainTLD,
domainArray = ["", domainTLD],
// set paths
ps = w.location.pathname.split("/"),
pathString = "",
pathArray = ["", "/"],
i = 0, l = 0;
y.setDate(y.getDate() - 1);
e = "; expires=" + y.toGMTString();
for (i = ds.length - 1; i--;) {
domainString = ds[i] + "." + domainString;
domainArray.push(domainString);
}
for (i = 1, l = ps.length; i < l; i++) {
pathString += "/" + ps[i];
pathArray.push(pathString);
}
w["SaveCookie" + id] = function (name, value) {
saveCookie(w.encodeURIComponent(name), value);
w.alert("Saved.\n" + name + "=" + value + "\n\n" + d.cookie);
};
w["UpdateCookie" + id] = function (name, index) {
var newValue = d.getElementById("Cookie" + name + index + id).value;
saveCookie(name, newValue);
w.alert("Updated.\n" + name + "=" + newValue + "\n\n" + d.cookie);
};
w["DeleteCookie" + id] = function (name) {
// try to remove it using different methods and variations etc:
var originalLength = d.cookie.length;
for (i = 0, l = domainArray.length; i < l; i++) {
for (var j = 0, m = pathArray.length; j < m; j++) {
var value = e +
((domainArray[i] !== "") ? domain + domainArray[i] : "") +
((pathArray[j] !== "") ? p + pathArray[j] : "");
saveCookie(name, value);
}
}
w.alert( ((d.cookie.length < originalLength) ? "Deleted! :-)" : "Not Deleted! :-(") + "\n" + name + "\n\n" + d.cookie);
};
if (d.cookie && d.cookie !== '') {
var split = d.cookie.split(';');
for (i = 0, l = split.length; i < l; i++) {
var pair = split[i].split("="),
name = pair[0].replace(/^\s+/, ''),
value= w.decodeURIComponent(split[i].substring(pair[0].length + 1));
outputHTML += formatTemplate(name, value, i);
}
}
// add new cookie fields:
outputHTML += "<input id='CookieNewName" + id + "' type='text' value='' placeholder='name' style='font-family:monospace;width:25%' />"
+ "="
+ "<input id='CookieNewValue" + id + "' type='text' value='' placeholder='value' style='font-family:monospace;width:50%' />"
+ "<input type='button' value='Add' onclick='window[\"SaveCookie" + id + "\"](document.getElementById(\"CookieNewName" + id + "\").value, document.getElementById(\"CookieNewValue" + id + "\").value);' />";
div.innerHTML = outputHTML;
body.appendChild(div);
})(window, window.document);
javascript:(function(a,b){var%20c=(new%20Date).valueOf(),d="<label><code>{name}=</code><input%20id='Cookie{name}{index}{id}'%20type='text'%20value='{value}'%20style='font-family:monospace;width:50%'%20/></label><input%20type='button'%20value='Update'%20onclick='window.UpdateCookie{id}(\"{name}\",%20{index});'%20/><input%20type='button'%20value='Delete'%20onclick='window.DeleteCookie{id}(\"{name}\");'%20/><br/>",e=function(a,b,e){return%20a=a.replace(/'/,"&apos;"),b=b.replace(/'/,"&apos;"),d.replace(/\{id\}/gi,c).replace(/\{name\}/gi,a).replace(/\{value\}/gi,b).replace(/\{index\}/gi,e)},f=function(a,c){b.cookie=a+"="+c},g=b.createElement("div"),h=b.getElementsByTagName("body")[0],i="",j=new%20Date,k="",l=";%20domain=",m=";%20path=",n=b.domain.split("."),o=n[n.length-1],p=o,q=["",o],r=a.location.pathname.split("/"),s="",t=["","/"],u=0,v=0;j.setDate(j.getDate()-1),k=";%20expires="+j.toGMTString();for(u=n.length-1;u--;)p=n[u]+"."+p,q.push(p);for(u=1,v=r.length;u<v;u++)s+="/"+r[u],t.push(s);a["SaveCookie"+c]=function(c,d){f(a.encodeURIComponent(c),d),a.alert("Saved.\n"+c+"="+d+"\n\n"+b.cookie)},a["UpdateCookie"+c]=function(d,e){var%20g=b.getElementById("Cookie"+d+e+c).value;f(d,g),a.alert("Updated.\n"+d+"="+g+"\n\n"+b.cookie)},a["DeleteCookie"+c]=function(c){var%20d=b.cookie.length;for(u=0,v=q.length;u<v;u++)for(var%20e=0,g=t.length;e<g;e++){var%20h=k+(q[u]!==""?l+q[u]:"")+(t[e]!==""?m+t[e]:"");f(c,h)}a.alert((b.cookie.length<d?"Deleted!%20:-)":"Not%20Deleted!%20:-(")+"\n"+c+"\n\n"+b.cookie)};if(b.cookie&&b.cookie!==""){var%20w=b.cookie.split(";");for(u=0,v=w.length;u<v;u++){var%20x=w[u].split("="),y=x[0].replace(/^\s+/,""),z=a.decodeURIComponent(w[u].substring(x[0].length+1));i+=e(y,z,u)}}i+="<input%20id='CookieNewName"+c+"'%20type='text'%20value=''%20placeholder='name'%20style='font-family:monospace;width:25%'%20/>=<input%20id='CookieNewValue"+c+"'%20type='text'%20value=''%20placeholder='value'%20style='font-family:monospace;width:50%'%20/><input%20type='button'%20value='Add'%20onclick='window[\"SaveCookie"+c+'"](document.getElementById("CookieNewName'+c+'").value,%20document.getElementById("CookieNewValue'+c+"\").value);'%20/>",g.innerHTML=i,h.appendChild(g)})(window,window.document);
@dieseltravis

This comment has been minimized.

Copy link
Owner Author

dieseltravis commented May 31, 2012

@CamiloMM

This comment has been minimized.

Copy link

CamiloMM commented Sep 6, 2013

The "UI" is abysmal, but it's a lot better than going to the console just to set a cookie, so thanks.

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.