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.