1

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.

Sina
  • 607
  • 7
  • 22
  • Can you be more specific about where the functions are? Background_pages, content_scripts, browser_action popups? – abraham Dec 08 '11 at 00:22
  • Its from a background page with a browser_action extension type. updated the question with specifics. – Sina Dec 08 '11 at 01:01
  • Is the page with the function you need to run an extension page or a page on the web? If it is a page on the web can you modify the code or do you have no control over what is returned from the servers? – abraham Dec 08 '11 at 02:10

1 Answers1

3

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.

Mihai Parparita
  • 4,236
  • 1
  • 23
  • 30
  • Perfect answer. Even though being an experienced developer I completely forgot about running inline JS. Thanks Mihai :) – Sina Dec 13 '11 at 19:09