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.