D
D
DmitriyH2013-12-05 22:06:35
Browser extensions
DmitriyH, 2013-12-05 22:06:35

Google Chrome Extension: display data in a new tab on button click

In the developed extension for Google Chrome, it is necessary to implement the following functionality: by clicking on the button from browser_action, display a popup, and by clicking on a link in the popup, open a new tab and display some dynamically generated information from scripts there.
The question is: what is the generally accepted way to implement such functionality? Interested in a set of parts of the extension and how they interact (ie the main names of classes/methods and keywords).
Before asking a question, I tried many options, but in the end I came across various kinds of restrictions.
Example of what I tried:
- in popup.js I open a new tab ("about:blank", "chrome://newtab", "chrome://extension/my_extension_page.html", "http:://something.com /"
- in the callback of creating a tab, I subscribe to the tab content update event
- the tab update handler, when "completed" I make the assumption that content_script.js has already started (in the manifest it is registered as on_document_start) and subscribed to custom messages via chrome. runtime.onMessage.addListener.
- I send a message from popup.js to the content_script.js of the created tab, in the content of the message I pass the information that needs to be displayed on the created tab
- the expected result: the custom message handler will work in popup.js, get the necessary information and update the contents of the tab.
It was not possible to go this way due to the following facts:
- content_script.js is not injected into about:blank, chrome::/extension/page.html
- when creating a new tab in chrome::/newtab, the onUpdate message is not received for the created tab. Even though content_script.js is injected into such a tab, it is injected too late. Therefore, the message from popup.js arrives too early in content_script.js and is thus skipped.
- when you open a real-life web page, everything works, but I think it's a terrible crutch and I don't think it's the only way to implement it.
I also tried to open some page from extension-a, but it didn't work to subscribe to custom messages: chrome.runtime.onMessage doesn't seem to be available for it. I don't want to use the crutch in the form of "wait a second while content_script is injected in a new tab".
I haven't figured out how to do this with background.js, because according to the documentation, chrome.tabs.sendMessage() is only sent to content_scripts.

Answer the question

In order to leave comments, you need to log in

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question