javascript - Chrome Extension - Passing object from page to context script -
I am writing a Chrome extension and I am struggling to return an object from the main page to the reference script. . I can not access the variables in the window.
Context script
// Store data on / off from Chrome storage Var hiddenInput = document.createElement ("Input") ; Hidden Input.setAttribute ("Type", "Text"); HiddenInput.setAttribute ("id", "__HIDDEN__RESULT__"); // turn on page var currentItem = document.body.appendChild (hiddenInput); // Event is currently being clicked in currentItem.onclick = function () {// global variable window.listOfCourses and get it chrome storage. Paste in storage.value.set ({'dataVault': window.listOfCourses}); }; // In the script inject script 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);
Injection script
function process data () {window.listOfCourses = []; (I = 0; i & lt; window.completedData.length; i ++) {// Get the data and add to window.listOfCourses} var myElement = document.getElementById ("__HIDDEN__RESULT__") myElement.click () ; }
The injected script pulls information from the page, sticks it in the object set as a global variable, then the onclick event is removed for the input at the end.
All of this work, however, when click firefalls and runs currentItem.onclick () and tries to access window.listOfCourses, then this variable is not Sees. I am confused that I can not see my global variable anymore.
Any help would be greatly appreciated!
The global variable of a content script and a page-level injection script.
The content script is executed in a special environment that is isolated world . They have access to the DOM of the page, but they are not for any JavaScript variables or actions created by the page . It sees every content script as if there is no other JavaScript running on that page running on it. The same is true in reverse: JavaScript running on page can not call any function or content can not use any variable defined by the script.
Thrust mine.
On your content script, you do not need to add additional DOM elements. You just need a custom DOM event.
// Listen to content script // event window. AddEventListener ("sepse", function (evt) {/ * message is in evt.detail}, false); // page reference var message = {/ * whatever; /}; Var Event = New Custom Event ("Segment", {Expansion: Message}); Window.dispatchEvent (event);
See for more information.
Comments
Post a Comment