Sina
Sina

Reputation: 607

Communicate between isolated worlds (extension js and webpage js) on chrome extension

As you may know, js files on chrome extensions and pages cannot directly access each other and they run on isolated worlds.

However, I want to access some of the functions on a page and call those functions from the plugin.

And I do not want to make my own version of those functions.

I'm wondering if this is possible... Would appreciate all the answers.

EDIT: The functions are on the background page. Its a browser action extension.

more info: Basically, I have a context menu which creates a tab to submit url to a page. however, re-opening the page makes many tabs and opening so many pages takes time. So I am willing to call the javascript function on the page directly from the extension if one instance of the page is open already. And I have access to the page permission-wise.

TL;DR: Need a way to call a javascript function on a page (without copying it locally) from an extension.

Upvotes: 1

Views: 1426

Answers (1)

Mihai Parparita
Mihai Parparita

Reputation: 4236

Assuming you know the tab ID of the page whose function you want to call, you can use chrome.tabs.update(tabId, {url: 'javascript:functionNameHere()'}); from your extension page. This works because javascript: URLs are mapped to script execution in the main (i.e. page, not isolated) world.

Upvotes: 3

Related Questions