2

I have an object WIO on a webpage which i want to access in my chrome extension. I have followed this example and injected my script.js in the page using first method.

I can successfully read WIO object in script.js. But the problem is i can't access it in popup.html.

Any suggestions will be great appreciated.

Here is manifest.json

{
  "manifest_version": 2,
  "name": "Campaign Analyzer",
  "description": "Analyze campaign data",
  "version": "0.1",
  "icons": { "128": "icon_128.png" },
  "browser_action": {
    "default_popup": "popup.html"
  },
  "permissions": ["activeTab", "<all_urls>"],
  "content_scripts": [
    {
      "matches": ["<all_urls>"],
      "js": ["jquery-3.4.1.min.js", "inject.js"]
    }
  ],
  "web_accessible_resources": ["script.js"]
}

inject.js

s.src = chrome.runtime.getURL("script.js");
s.onload = function() {
  this.remove();
};
(document.head || document.documentElement).appendChild(s);

script.js

setTimeout(function() {
  console.log(WIO.sessionInfo.context);
}, 6000);
Lorem Ipsum
  • 337
  • 2
  • 14
  • 1
    The answer in the linked topic should work so I guess you've adapted it incorrectly. Add your code and a proper [MCVE](/mcve) to your question. Or use a different [example](/a/46870005). More info: [here](/a/9517879). – wOxxOm Dec 18 '19 at 13:24
  • @wOxxOm I have an object **WIO** in the page which i need to access. I have injected my script.js using this [example](https://stackoverflow.com/questions/9515704/insert-code-into-the-page-context-using-a-content-script/9517879#9517879) you linked in the comment. It outputs the WIO object but incorrectly. Also, if i log WIO.sessionInfo key it return undefined. – Lorem Ipsum Dec 20 '19 at 11:29
  • It probably means there are properties inside that page variable that aren't JSON-compatible (string, number, boolean, null, or arrays/objects of these types). Instead of transferring the entire object, only send the select fields you want. Or send JSON.parse(JSON.stringify(WIO)) – wOxxOm Dec 20 '19 at 11:34
  • JSON parsing and stringify doesn't work either – Lorem Ipsum Dec 20 '19 at 13:09
  • 1
    I suggested two things but you replied only to one. Anyway, this is as far as guessing can go. The approach itself is correct so now you need to debug it to see what actually happens. For example add console.log for the object inside the script element code. – wOxxOm Dec 20 '19 at 13:16

0 Answers0