Chrome extension not storing value to chrome.storage.local?

Viewed 33

I'm trying to build an extension to monitor the xhr portion of the devtools network tab. With some help , I have been able to get the requests to be displayed on the service-worker console and I can log the Post requests. My next step is to send some data to chrome.storage.local, so it can be read later by a content script. However although I am getting

  console.log(point);

outputted in the service worker console, I am not seeing any output in either console containing:

console.log('Value is set to ' + point)

No errors seen though. What am I doing wrong?

manifest.json:

{
 "manifest_version": 3,
 "version": "1.0",
 "name": "Hello World!",
   "description": "Learning how to make a chrome extension!",
 "icons": {
"16": "images/puppy16.png",
"48": "images/puppy48.png",
"128": "images/puppy128.png"
},
"action": {
"default_icon": "images/puppy.png",
"default_popup": "popup.html"
},
"background": {
"service_worker": "background.js"
},
"host_permissions": ["*://*.cnn.com/", "],
"permissions": ["webRequest", "activeTab","tabs"],
"content_scripts": [
{
  "matches": ["*://cnn.com/*"],
  "js": ["contentScript.js"]
  }
 ]
}

In my background.js:

(function () {

    var point;

    chrome.webRequest.onBeforeRequest.addListener(
        function (details) {
     
            // Use this to decode the body of your post
            const postedString = decodeURIComponent(String.fromCharCode.apply(null,
                new Uint8Array(details.requestBody.raw[0].bytes)));
            console.log(postedString)
            const body = JSON.parse(postedString);
            point = body.CenterPoint.Point;
            console.log(point);
        },
        { urls: [url] },
        ["requestBody"]

    );
    chrome.storage.local.set({ 'key': point }, function () {
        console.log('Value is set to ' + point);
    });

})();
1 Answers

Based on comments I have the following working code:

chrome.webRequest.onBeforeRequest.addListener( function (details) {

        // Use this to decode the body of your post
        const postedString = decodeURIComponent(String.fromCharCode.apply(null,
            new Uint8Array(details.requestBody.raw[0].bytes)));
        console.log(postedString)
        const body = JSON.parse(postedString);
        point = body.CenterPoint.Point;
        console.log(point);
        chrome.storage.local.set({ 'key': point }, function () {
            console.log('Value is set to ' + point);
        });


    },
    { urls: [url] },
    ["requestBody"]

);
Related