0

I have been bashing my head on the wall for the last 4 hours.

Within a chrome extension, I have the following:

    console.log(dmain); // this outputs "thispc"
    console.log(typeof dmain); // this outputs "string"
    console.log(_activeDomains); // this outputs (2) ["thispcX", "Xthispc"]
    console.log(typeof _activeDomains); // this outputs "object"
    console.log("isarray:"+Array.isArray(_activeDomains)); // this outputs isarray:true

    // I discovered that _activeDomains.push(dmain.toString()+"X"); worked, so tried adding and removing the "X"
    var tmp = dmain.toString()+"X";
    tmp = tmp.substr(0,tmp.length-1);
    console.log("tmp="+tmp);
    _activeDomains.push(tmp);

    console.log(dmain); // this outputs "string"
    console.log(_activeDomains); // This is where it goes weird
    console.log(_activeDomains.length); // this outputs 3
    console.log(typeof _activeDomains); // this outputs object
    console.log("isarray:"+Array.isArray(_activeDomains));
    chrome.storage.local.set({"ctiDomains": _activeDomains},function(){
        console.log("done");
        chrome.storage.local.get(["ctiDomains"], function (result){
            console.log(result); // this outputs ctiDomains: (2) ["thispcX", "Xthispc"]
            console.log("isarray:"+Array.isArray(_activeDomains));
        });

The convoluted nature of it is the current state after my many attempts to get it to work. I have tried renaming variables and storage names in case they were reserved words.

The bit where it goes weird outputs this:

(3) ["thispcX", "Xthispc", "thispc"]

but when I expand it, it shows this:

(3) ["thispcX", "Xthispc", "thispc"]
  0: "thispcX"
  1: "Xthispc"
  length: 2
  __proto__: Array(0)

I have created another extension trying to replicate the problem with various ways of populating the array and it all works fine. Here is the full content script of this simpler extension:

$("body").append(`<input type="button" onclick="window.postMessage({action:'toggleCTI','domain':location.hostname})" />`);

window.addEventListener("message", handleMessageFromPage, false);

chrome.storage.local.set({"foo1": ["abc","def"]});

var foo2 = [];
foo2.push("xyz");
chrome.storage.local.set({"foo2": foo2});

var foo3 = ["jkl","mno"];
chrome.storage.local.set({"foo3": foo3});

var foo4 = [];

chrome.storage.local.get(["foo4"], function(result){
    foo4.push("kkk");
    chrome.storage.local.set({"foo4": foo4});
});

function handleMessageFromPage(evt)
{
    foo4.push(evt.data.domain);
    chrome.storage.local.set({"foo4": foo4});
}

foo4 mimics what my problem script is doing, but in this simpler script it works.

So, I deleted the content script of my real extension and re-created it, thinking there must be something corrupt in it, but that made no difference either. Have also restarted chrome.

Has anybody come across anything like this before?

Am running the latest version of chrome: Version 88.0.4324.190 Problem was there on the previous version also.

Am using Manifest Version 2 for the extension that works and for the one that doesn't

Any help would be great, I'm running out of hair to pull out.

Graham
  • 7,807
  • 20
  • 69
  • 114
  • console.log is quite an awkward way to debug code, you can set breakpoints and use devtools debugger fully to inspect the actual state of variables/DOM and to run the code line by line. – wOxxOm Mar 01 '21 at 15:52
  • ok, I put a break on the .push and stepped through from there, and it now works. So is this some sort of timing issue? – Graham Mar 01 '21 at 16:00
  • 1
    Devtools evaluates console output only when you expand it but the preview is generated immediately. – wOxxOm Mar 01 '21 at 16:03
  • cheers - wish I'd posted this 4 hours ago :D – Graham Mar 01 '21 at 16:05
  • Strictly speaking this is bad UX/UI design of devtools. You can suggest them to try to find a more prominent way of informing. Currently they just show an `(i)` icon that almost no one would ever click. – wOxxOm Mar 01 '21 at 16:11

0 Answers0