2

I've been researching all over the place and I can't find anything. I find it hard to believe that I'm the first person to think about trying to do this, but I wonder if it's even possible.

I want to clear cookies, app cache, local storage, everything associated with a site using a bookmarklet that I can leave on my toolbar.

Is that possible?

1 Answers1

8

It is possible, and it is actually really easy to implement a simple solution.

It can be done following these steps:

  1. Create a bookmark and give it a descriptive name. For example: "Clear Storage".
  2. Change the URL so it is like javascript:(function(){ [YOUR CODE] })();
  3. Replace [YOUR CODE] with one line of JavaScript that does the desired clearing.

For example:

  • To clear the local storage, use localStorage.clear();. The bookmark URL would be like:

    javascript:(function(){ localStorage.clear() })();
    

    or just

    javascript:localStorage.clear();
    
  • To delete cookies, use any of the solutions on this question. I opted for Craig Smedley's solution because it is already a one liner (and it was ready for a bookmarklet). The bookmark URL would be like:

    javascript:(function(){ document.cookie.split(";").forEach(function(c) { document.cookie = c.replace(/^ +/, "").replace(/=.*/, "=;expires=" + new Date().toUTCString() + ";path=/"); }) })();
    

Now, to make multiple of these operations just with one click on the bookmark, just concatenate them (with only one javascript:). For example, a URL for the bookmark that clears local storage and delete cookies would be like:

javascript:(function(){ localStorage.clear();var c = document.cookie.split("; "); for (i in c) { document.cookie =/^[^=]+/.exec(c[i])[0]+"=;expires=Thu, 01 Jan 1970 00:00:00 GMT"; }; })();

You just need to find the code that you like the most or better fits your needs, and save it on the bookmark. As it will probably be too large for the bookmark size, you may want to place the code in a file somewhere and apply it as described on this comment.

You can see a demo of a bookmark that deletes the localStorage on this JSFiddle.

Community
  • 1
  • 1
Alvaro Montoro
  • 28,081
  • 7
  • 57
  • 86