yossharel
yossharel

Reputation: 1929

How To Make Page To Interact With Background Page?

My Chrome extension has a background page that execute script on current tab.

The script change elements on current tab by adding code to existing elements to call function 'myFunction' defined at background page when 'onClick' events occur. The problem is that exception is thrown that 'myFunction' is not defined on current tab.

what is the best way to enable this interaction? to enable current page to go to function defined on background page?

Thanks in advance!

Upvotes: 0

Views: 159

Answers (2)

shrunyan
shrunyan

Reputation: 456

As mentioned in the first answer, "Without more information, it's difficult to help you more.", but for your second question it sounds like what you need is a reference to the function defined in your background page. This can be achieved with the getBackgroundPage function. The code looks like this;

var bgPage = chrome.extension.getBackgroundPage();
bgPage.myFunction();

Upvotes: 0

Métoule
Métoule

Reputation: 14472

The background page is executed in an independent context, and thus its functions can't be directly executed in the currently opened tab.

What you need is a content script executed on all the tabs, that then communicates with the background page, using the message passing mechanism.

Without more information, it's difficult to help you more.

Upvotes: 1

Related Questions