Search code examples
javascriptgoogle-chromegoogle-chrome-extension

access website localStorage from chrome extension popup open


I want to read the localStorage value of the open tab in the chrome extension when I open the popup.html

I already tried with chrome.storage.local and chrome.storage.sync but I am getting undefined

chrome.storage.local.get(['key'], function(result) {
    console.log('Value currently is ' + result.key);
});

also tried chrome.tabs.executeScript as well and gives undefined only.

var fromPageLocalStore = chrome.tabs.executeScript(tab.id, { code: `localStorage['branch_session_first']` });

Can you please help me to get the active tab's localStorage value using the chrome extension?


Solution

  • I solution for this question by using sendMessage and onMessage Listener functions.

    below is the code

    content.js

    chrome.runtime.sendMessage({
        info: localStorage['key'] //get from tab's local storage
    });
    

    background.js

    chrome.runtime.onMessage.addListener(function (message, sender, sendResponse) {
        localStorage['key'] = message.key; //store into extension's local storage
    });