Communication between background page and popup page in a Chrome Extension - javascript

I'm currently trying to write an extension for Google Chrome, which can be used to upload files.
There are two pages: the background page and the popup page.
The popup page appears when you click the icon right of the omni-bar. You can specify the file you want to upload using the standard HTML <input type='file' ... />.
After selecting the file, and clicking "Upload", the name(+path) of the file should be sent to the background page. This, because the popup can be closed by the user by simply clicking somewhere else on the screen, which closes the page.
When the popup is active, and the background page is uploading the file to the server, the popup should also recieve the progress of uploading(0-100%) from the background page, and display this information. When finished, the user should see the URL.
The problem is, I don't know how to communicate between these two pages. The documentation isn't very clear about how this works. A thing I've tried, is making a function on the background page, called upload(filename), and put this code in the popup page:
var BGPage = chrome.extension.getBackgroundPage();
BGPage.upload(the_filename);
But it didn't work, the function wasn't called.
Does anyone know how I can send the filename from the popup page to the background page, and how to retrieve upload status(and eventually the link) from the background page, via the popup page?
Thanks in advance!

Define it as a variable.
background.js
upload = function(fileName) {
console.log('Uploading', fileName);
}
popup.html
<script src="popup.js"></script>
popup.js
var BGPage = chrome.extension.getBackgroundPage();
BGPage.upload(the_filename);
That should work. If it doesn't, then check your inspector for the popup and background page:
Popup Inspector: Right click on the popup and choose Inspect
Background Inspector In your extension settings page chrome://extensions, turn on developer mode (check top right), and click on background.js.
It will open the inspector, then click on console to see the error messages in the console to assist you further, doing what I stated above should work, I do it all the time.

Related

Chrome Extension: Is there a way to execute popup.js before clicking on the extension icon?

What I'm trying to do:
I'm building a chrome extension that has a popup, an options page, and content scripts. So far, I'm able to store preferences set on the options page and get them using popup.js. The popup.js makes a couple of public API calls to get some information X.
What I'm stuck on:
I need to be able to run/execute popup.js when the DOM loads, before the extension icon is clicked, so that information X can be injected into the DOM via the content scripts.
My question:
Is there a way to execute popup.js before clicking on the extension icon? (Right now, the content scripts loads fine displaying information X, after the the extension icon is clicked)
chrome.storage is fully supported for content scripts, so there is no need to communicate the user preferences from the options page to the content scripts via popup.js or background.js.
With this in mind, I'm able to access user preferences directly to make the API calls in the content scripts, using storage.sync

Can a Google Chrome Extension send a message from an extension webpage back to a website page and also reopen that page?

I am building a Chrome Extension for ,my Bookmarks app.
A bookmark record has 4 images fields.
Each image filed has these options to set an image:
Generate a screenshot of the viewport of the webpage
select an image scraped from the webpage HTML
Select a region of the webpage screen and then edit it with annotations
Orginally I had a modal style app that loaded in the popup.html file when the extension button was clicked in the toolbar. Because I added the ability to select a region for the screenshot image though I now have to move that modal into the actual webpage DOM.
The reason is that when a user select to select a region screenshot from the modal, it has to close the modal/hide it and show the screen to make the selection. On completion there is no way to re-open the popup from the menu bar!
So I am moving the modal to the webpage. Clicking the toolbar button will then build and show the modal. Then when user selects to select region for 1 of the 4 image fields, I will hide the modal and on completion I can simply show the hidden modal and populate a text input filed with the newly generated and uploaded image.
I am now trying to go a step further though. After selecting the image region, instead of showing the Modal window right away. I want to instead show the image in an editor style page which would be an HTML webpage that belonged to the extension (like this chrome-extension://mcbpblocgmgfnpjjppndjkmgjaogfceg/fsCaptured.html). It would open this in a new tab.
The user would then annotate with text, shapes, etc in the editor and then upload file.
This is where I am stuck. When I get the URL back from the uploaded image, I need to then close this new tab window and go back to the main webpage that had the app modal window and where the screenshot selection was made from. I would need to then open/show the modal again and fill in the text input for the new image upload field.
So my question here is...
Is it possibble for my extension to pass a message from my chrome-extension://mcbpblocgmgfnpjjppndjkmgjaogfceg/fsCaptured.html page with the editor back to the webpage the modal is on?
I would need to:
Pass the image URL back to the webpage with a message
Close the extension editor page/tab
go back to/bring focus to the webpage with the modal on it
Is this even possibble? I realize you can send messages from background scripts to content scripts and content scripts back to background scripts.
UPDATE
For the first part. Perhaps the extension webpage would send a message to the background.js and then the background.js could send a message to the webpage content script. This would solve that part of the process!
Would just need to figure out if the extension webpage can close itself and then bring focus to the original webpage?
Update 2
Looks like I figured it out.
You can make a tab selected/active with:
chrome.tabs.update(tabId, {selected: true});
I believe my custom extension page can send a message to the background.js
and then the background.js could send a message to the webpage content script with the modal window and have it then update the image fields in that modal.
Next from my custom extension page I should be able to call chrome.tabs.update(tabId, {selected: true}); after it sends the above message to the content script which would then close the extension page and bring focus back to my webpage with the modal in it.

kango extension popup wont open

I'm making a cross browser extension using Kango. I'm sending a message from the content script to the background page and I want that a popup displays when the message is received by the background script. Here's what I have so far
background script:
kango.addMessageListener('Content2Background', function(event) {
kango.ui.browserButton.setPopup({url:'popup.html', width: 710, height:510});
console.log(event.data);
});
and in the content script:
kango.dispatchMessage('Content2Background', "Hey");
This runs, the console displays the message in the console, but the popup is not enabled. Any ideas why?
It turns out that setPopup just adds the popup to the button. So clicking it again is what opens the popup. However, any message listeners or senders previously attached to the button will be lost. So beware or be square.

window.history.back not working on new window with Firefox

I have a page with a link that opens a new window with a div and an iframe. The user can navigate through the web that it's inside the iframe and the div shows information.
The user wants an option to go back on pages that he visited inside the iframe, so I put an image on the div to allow that option. The image has the onClick event that calls history.back() method.
The problem that I have it's that in Firefox when I try to go a page back, the iframe stills showing the page that I'm seeing but if I try in Chrome it works great, I can visit all the sections that has my page and I can go back clicking on that image.
I tried to use the browser's context menu but in Firefox does nothing.

Google Chrome Extension: How can I view a background page via an iframe on the popup page?

I've been trying for a while now and can't get this to work.
Basically, when the extension is enabled, I want a persistent page (That will hold a socket connection regardless of whether the Browser Action icon is clicked).
The background page functions in this way. Now, I want this background page to show in the popup page (via iframe or any alternative so that it's not another instance of the Background page.
I get something like this when I try:
"The webpage at chrome-extension://invalid/ might be temporarily down or it may have moved permanently to a new web address."
Any ideas on a direction I could try?
The real background page is hidden, and cannot be shown (the Dev Tools for the background page can be opened by clicking on the background.html link at chrome://extensions/, in Developer mode).
From the popup, you can directly access methods and properties from the background page, using chrome.extension.getBackgroundPage(). This can be used to maintain a (session-)persistent state of your extension's popup window.
When you try to load the background page in a frame/tab, a new instance of the page is shown. This, however, is not a background page. It's treated as a normal HTML file within your extension.
If you want the url for the background page I'd recommend using chrome.extension.getURL('background.html')
But I don't recommend this method for what you're doing, though I suppose it would work. I would instead recommend using some simple message passing. With this, you're popup/browser action will actually hold your html for your UI, and send a message to the background page asking for the live web socket stuff from the background page.
These are the docs on message passing: http://code.google.com/chrome/extensions/messaging.html
Have fun and good luck! It's not bad at all, let me know if you have questions though

Categories

Resources