0

Trying to access chrome.runtime.sendMessage in an iframe, but it is showing that

chrome.runtime is undefined

chrome.runtime.sendMessage("kbfjlfcddgkokfgifbohnjfpcnkknpbf", { getVersion: true },
    function (response) {
       console.log(response);
    }
);
kvk30
  • 1,133
  • 1
  • 13
  • 33
Madhu M
  • 1
  • 1
  • 3
  • What error are you getting, please specify it? [Link of similar problem](https://stackoverflow.com/questions/13266192/accessing-iframe-from-chrome-extension?lq=1) – kvk30 Oct 09 '18 at 14:03
  • i have alredy put the error i am getting – Madhu M Oct 10 '18 at 07:47
  • Have you checked the link? – kvk30 Oct 10 '18 at 07:52
  • yes tat is diffrent issue my issue is chrome.runtime value is undefined inside iframe js – Madhu M Oct 10 '18 at 07:59
  • [Check this link](https://stackoverflow.com/questions/30226216/chrome-extension-sending-message-from-iframe-to-event-page-then-to-content-scrip) – kvk30 Oct 10 '18 at 13:07

1 Answers1

0

Based from this thread, if you're inserting JavaScript into a page with a <script> tag, it executes in the page's context.

Sometimes it is desirable: that's the only way to access page-level JavaScript objects.

But in your case it means that the code does not have access to Chrome APIs, as it is "the same" as the page's code.

You need to look into communicating between page-level and context scripts, or between page-level and background (spoiler, in most cases needs a context script proxy anyway).

Also from this page, by adding a breakpoint, or debugger statement, it causes that value to be undefined. Try to refresh the page then open after page is loaded and see if the chrome.runtime works correctly.

Community
  • 1
  • 1
abielita
  • 13,147
  • 2
  • 17
  • 59