Desktop notification from background script - google-chrome-extension

I'm beginner in chrome extensions development. I'm trying to show desktop notifications when my background script finishes some of its methods but i don't know how i can request permissions for this. Is there any way to accomplish this task?
It can be done using content script, but I want to show notifications by background script, without background page.

You need to add notifications to your manifest.json file:
"permissions": ["tabs", "notifications","management"]
You can then call
webkitNotifications.createNotification('images/message48.png', title, message);

Related

Method to autoupdate Chrome Extension badge

I'm building a Chrome extension that makes the badge show a number returned from an API. I have the code working fine, but I have it listening for DOMContentLoaded, so it only updates when the user opens up the extension.
I would like the extension to check the API every time the browser loads a page. I do not need to change anything in the page, I just want to use it for timing.
I'm not sure what I should be using, should I be using background pages, event pages, or something else? What would be the best way to go about this?
Thanks in advance!
The api you want for “every time the browser loads a page” is chrome.tabs.onUpdated. You’d have:
chrome.tabs.onUpdated.addListener(function(tabId,changeInfo,tab) {
chrome.browserAction.setBadgeText({"text":"ABCD","tabId":tabId});
});
An easy approach for development is to use a background page, get it working, and then figure out what changes you need to convert it to an event page. With this stub however, nothing is stopping you from making it an event page.

Google Slides viewer finish event

i'm using google slide in a embedded iframe, I've published it and I'm able to autostart but i can only autoloop or stop it once finished.I'd like, for example, to change iframe content once finished, there's a way or an api for google viewer to send an event once my presentation is finished.
Thanks
You can write a short/simple chrome extension that will do that.
Or use this extension: https://chrome.google.com/webstore/detail/auto-reload/ofojbjgaaddibdfpmmjeonahgbacejid

What can you do in background scripts that you can't do in other js files?

*By other js files, I mean the files you include in popup.html.
The following code works, so why should I use a background script?
Content script
chrome.extension.onMessage.addListener(
function(request, sender, sendResponse) {
//Some code
}
);
Script included in popup.html
chrome.tabs.query({active:true,windowId: chrome.windows.WINDOW_ID_CURRENT},
function(tab) {
chrome.tabs.sendMessage(tab[0].id, {method: "someMethod"},
function(response){
//Some code
});
});
This
Background pages live as long as the Chrome browser is active (and even longer if the "background" permission is set). Popup pages are only active when the badge's popup is open. The popup can only be opened by the user.
The background page's document is never visible, whilst the popup page becomes visible on click of the badge's button.
Besides that, there's no difference between background pages and popup pages. They run in the same extension's process, and have access to the same set of APIs.
If your extension only needs to be active while the popup is active, you don't need a background page. To save the state of the popup, just use the synchronous localStorage or the asynchronous chrome.storage API. When the variables you use are too complex to be stored using either API, a background page may be useful.
One example where the use of a background page is beneficial:
Imagine an extension that downloads a huge text file from a server. The creation of the resource is very resource-intensive for the server. Technically, everything can be done from within the popup. However, offloading the task to the background page allows the user to do other tasks while the file is downloading (if you use a popup only, the download will stop when the user closes the popup).
Though you didn't ask, I'd like to make you aware of event pages. They are similar to background pages, with one difference: Event pages are automatically closed when the extension is idle. In other words, event pages are only active when needed! By doing this, your extension will profit from the advantages of background pages without needlessly wasting the user's memory.
My last example is also a perfect example of when an event page has to be used. Besides doing the http request on behalf of the popup, the background page does nothing. If you use an event page instead of a background page, you get the best of both worlds: the popup page can be closed without interrupting the download, and the extension will not waste memory.
Documentation
Learn more about Background pages and Event pages
"Popup" in this answer refers to the optional panel of the chrome.browserAction or chrome.pageAction API, set by declaring the "default_popup" key in the manifest file, or programatically using the setPopup method.

Calling chrome.browserAction.setIcon from content script the way it is done in background script

I am making an extension for chrome. It fetches data from webpages and emails it via local email client. I have a toolbar button which user has to click to invoke the script.
My script works for a few selected urls. I want my toolbar button to change icon based on whether the url is among our list or not. For example for site1 it should be redicon.png and for site2 it should be blueicon.png. I can change button icon using chrome.browserAction.setIcon. But the problem is that this API does not work in content script. It works fine in the background.js file but not in content.js. Kindly tell me how to achieve this.
I know using pageAction instead would do the trick but my client requirement is that the toolbar icon should change rather than appear and disappear.
What you need to read about is message passing. You are right, content scripts have limited chrome API. However, you can contact background page from content script and tell it to execute anything from chrome API for you. First, you need to create a listener on a background page that will be waiting for messages and then send a message from a content script.

Message Passing variables for a chrome extension

I've read the message passing page repeatedly and tried multiple variations of it and I'm assuming I'm making a small mistake.
What I'm trying to do: I want to grab the background color of a page with a content script and then send that information to my popup (not a background page; I don't know if that matters) and then allow the popup to (simple example) write that variable in the popup.
Content scripts run in the same process as the webpage (the "tab" process). Popups run in the same process as the background page (the "extension" process).
It sounds like you want the popup to send a request to the content script using chrome.tabs.sendRequest. The content script should be listening for requests using chrome.extension.onRequest.
Or, you might do it the other way around: the content script sends the request to the extension using chrome.extension.sendRequest and the popup listens for requests using chrome.extension.onRequest. When you send a request from a tab to the extension, any page in the extension process (popups or background) can listen for that request.
See the message passing tutorial which includes examples that you can copy and paste.

Resources