I am writing a chrome extension and I am trying to pass an object back from the main page to the script context. I cannot access window 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);
Script Entered
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 script you enter retrieves information from the page, binds it to the object specified as a global variable, and then finally fires the onclick event for input.
It all works. However, when the click event fires and fires currentItem.onclick () and tries to access the .listOfCourses window, it does not see the variable. I am confused why I can no longer see my global variables.
Any help would be greatly appreciated!
javascript google-chrome-extension
chris75898
source share