I'm writing a chrome extension and I'm struggling to pass an object back from the main page to the context script. I can't seem to access the window's variables.
ContextScript
//STORE DATA TO CHROME STORAGE ON EVENT
//create hidden input
var hiddenInput = document.createElement("input");
hiddenInput.setAttribute("type", "text");
hiddenInput.setAttribute("id", "__HIDDEN__RESULT__");
//add input to page
var currentItem = document.body.appendChild(hiddenInput);
//event to be fired on click
currentItem.onclick = function() {
//get the global variable window.listOfCourses and stick it in storage
chrome.storage.local.set({'dataVault' : window.listOfCourses});
};
//inject script into page
var s = document.createElement("script");
s.src = chrome.extension.getURL("gradebook.js");
s.onload = function() {this.parentNode.removeChild(this);};
(document.head||document.documentElement).appendChild(s);
Injected Script
function processData()
{
window.listOfCourses = [];
for (i=0; i < window.completedData.length; i++)
{
//get data and add to window.listOfCourses
}
var myElement = document.getElementById("__HIDDEN__RESULT__")
myElement.click();
}
The injected script pulls information from a page, sticks it in an object set as global variable, then finally it fires the onclick event for the input.
All of this works. However, when the click event fires and runs currentItem.onclick() and tries to access window.listOfCourses, it doesn't see the variable. I'm confused why I'm not able to see my global variables anymore.
Any help would be greatly appreciated!
See Question&Answers more detail:
os 与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…