I have the below script running on my script.js file and everything runs as expected but it does not recognise the dataLayer variable?
chrome.tabs.query({ active: true, currentWindow: true }).then(function (tabs) {
var activeTab = tabs[0];
var activeTabId = activeTab.id;
return chrome.scripting.executeScript({
target: { tabId: activeTabId },
func: function() {
console.log(dataLayer);
return JSON.stringify(dataLayer);
},
});
}).then(function(results) {
var datalayer = JSON.parse(results[0].result);
console.log(datalayer);
}).catch(function(error) {
console.log('There was an error: \n' + error.message);
});
The result I get is:
Uncaught ReferenceError: dataLayer is not defined at <anonymous>:2:23 at <anonymous>:4:9 (anonymous) @ VM896:2 (anonymous) @ VM896:4
but when I type "dataLayer" into that windows console, it recognises it? Console Screenshot