admin管理员组

文章数量:1026989

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?

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?

Share Improve this question asked Apr 22, 2015 at 19:37 user4154355user4154355 2
  • You can use Firecookie, addons.mozilla/en-US/firefox/addon/firecookie – Rahul Tripathi Commented Apr 22, 2015 at 19:39
  • I want to do a lot more than just clear cookies, so I don't think that is what I'm looking for. – user4154355 Commented Apr 27, 2015 at 13:07
Add a ment  | 

1 Answer 1

Reset to default 7

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 ment.

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

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?

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?

Share Improve this question asked Apr 22, 2015 at 19:37 user4154355user4154355 2
  • You can use Firecookie, addons.mozilla/en-US/firefox/addon/firecookie – Rahul Tripathi Commented Apr 22, 2015 at 19:39
  • I want to do a lot more than just clear cookies, so I don't think that is what I'm looking for. – user4154355 Commented Apr 27, 2015 at 13:07
Add a ment  | 

1 Answer 1

Reset to default 7

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 ment.

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

本文标签: javascriptBookmarklet for clearing appcachecookieslocalstorageStack Overflow