I have a simple Chrome developer extension which injects JavaScript into a target page.
It accesses the DOM no problem but will not allow me to access the variables.
<head>
<script>
window.myVar = 10;
</script>
</head>
<body>
<div id="testDiv">
some div content
</div>
</body>
Calling this snippet:
chrome.tabs.executeScript(
chrome.devtools.inspectedWindow.tabId,
{
code: scriptToExecute,
frameId: targetId
},
function(response) {
let result = [];
if(response && response.length > 0) {
result = response[0];
}
results.value = result;
}
);
This works:
scriptToExecute = document.getElementById('testDiv').innerHTML;
This returns null:
scriptToExecute = window.myVar;
Is there some way to access the variables or is it prevented for security or other reasons?