Reputation: 108
As the title states, is it possible to access the methods defined in a google extension from another google extension? The method I am trying to access can be called from the Google Console, however, when I try to access the method from my background page with the following code
chrome.tabs.executeScript(ns.tracsCopyFromTabId, {
code: "alert(methodName)"
}
it gives me the following error:
Unchecked runtime.lastError while running tabs.executeScript: Cannot access a chrome-extension:// URL of different extension
I believe the method I'm trying to access is defined in their content-script? Has anyone ever tried and successfully do this?
Upvotes: 1
Views: 276
Reputation: 77531
The error + your comment about not needing to change the context signifies that you're probably not actually injecting in a regular webpage that runs content scripts, but into an extension page (with visible URL chrome-extension://...
).
Anything in such a page is not a content script by definition. Even if that code is also shared with a content script used somewhere else.
Note that if you're using (but not showing) allFrames: true
for injection, you may be accidentally injecting in an extension-page frame, leading to that error.
The problem is that you can't declare host permissions for chrome-extension://
pages. Therefore you can't inject content scripts into those pages.
It's not possible to define permissions to do that from an extension alone (to make an extension you can distribute that "just works"), but you can do it locally with Chrome flags + chrome.debugger
API.
You will need chrome://flags/#silent-debugger-extension-api
enabled and will need to use the Debugger Protocol to execute your code.
Sadly, the chrome://flags/#extensions-on-chrome-urls
flag does not allow injection into this protocol either, so you have to use Debugger.
If you're actually trying to reach code in a content script in a regular webpage, you can't call it from your own content script because every extension gets its own isolated JS context.
You'd need chrome.debugger
again.
Sometimes you're dealing with code that's injected in the webpage itself from a content script.
Then you should be able to access it with the method you describe by making the same content script -> page code leap. See the link above.
That doesn't explain the error you're getting though.
Upvotes: 1