Grego
Grego

Reputation: 2250

How to programatically remove all local storage from chrome extension of a specified domain

I would like to with my chrome extension remove all localStorage of a specified domain

when I open the google chrome , I can remove it by going to settings -> All cookies and then find the local storage by the domain name, and delete it manually.

Here is what I get:

server.myserver.test.com

Local Storage

Source: https://server.myserver.test.com
Disk Size:  5,0 KB
Last modification:  Saturday...

What would I need to do to do this inside my chrome extension, assuming that I have permission for the specified servers to access its cookies.

Thank you!

Upvotes: 5

Views: 9135

Answers (4)

Eugene
Eugene

Reputation: 1142

It looks like it can't be done with Chrome Extension's API. So you'll have to do it manually. Create a content.js script with:

localStorage.clear();

Then in your manifest.json tell it to load the file at the "document_start":

  "content_scripts": [
    {
      "run_at": "document_start",
      "matches": ["<all_urls>"],
      "js": ["content.js"]
    }
  ],

In this case, content.js script will be injected and executed earlier than the website's scripts.

If you want to remove local storage only on the specific domains, you can check for a domain in your content.js file or use matches in manifest.json.

For example, a code below will remove local storage on *.microsoft.com:

const domain = window.location.hostname.split('.').slice(-2).join('.');
if ('microsoft.com' == domain) {
    localStorage.clear();
}

Upvotes: 0

Vijay Kumbhani
Vijay Kumbhani

Reputation: 742

to used below function

 chrome.storage.local.clear()

Upvotes: 1

Lucky
Lucky

Reputation: 17345

You can run a user script from grease monkey..and clear all the localStorage elements using..

// ==UserScript==
// @name        clearLocalStorage
// @namespace   stackoverflow.com
// @include     http://stackoverflow.com/
// @version     1
// ==/UserScript==
localStorage.clear();

Upvotes: 0

saroyanm
saroyanm

Reputation: 758

I think you can run content script for that specific Domain and remove local storage data by calling:

localStorage.clear();

Chrome Cookies API removes only cookies.

Upvotes: 6

Related Questions