Chrome extension Desktop notification - google-chrome

I am working on a chrome extension for desktop notification.Is there any way by which I can close the desktop notification after a specified time ?

If you have a reference of the notification object, you can use notification.cancel instead:
setTimeout(function() {
notification.cancel();
}, 5000);
References:
Chrome extension development: auto close the notification box
http://dev.chromium.org/developers/design-documents/desktop-notifications/api-specification

You can close it by running window.close() from notification's javascript (assuming your notification is a separated HTML file). So something like this:
setTimeout(function() {
window.close();
}, 5000);

for me this worked
setTimeout(function() {
notification.close();
}, 2000);

Related

application cache issues with android browser

I'm into application cache related work in HTML5. I've added addCacheListeners() in body onload. This works fine with mobile safari and chrome, but NOT with android browser. when it comes to the android browser, error event is fired.
function addCacheListeners(){
var appCache=window.applicationCache;
if(appCache!== 'undefined'){
alert("defined");
appCache.addEventListener('checking', function(e){
}, false);
appCache.addEventListener('progress', function(e){
}, false);
appCache.addEventListener('updateready', function(e) {
alert("update is ready");
if (appCache.status == appCache.UPDATEREADY){
appCache.swapCache();
updateappInfo();
}
}, false);
appCache.addEventListener('noupdate', function(e){
updateappInfo();
}, false);
appCache.addEventListener('error', function(e){
alert("error" + e.message);
}, false);
appCache.addEventListener('cached', function(e){
alert("cached");
updateappInfo();
}, false);
}
if (appCache=== 'undefined'){alert("undefined");}
}
Any idea with regards to this?
Thanks a bunch.
This issue is with only Android browser as it does not support ‘.manifest’ extension without having MIME type configuration in the hosted server.
Therefore, tried out adding ‘.manifest’ as one of the MIME types, but this was failed as it does not support as a MIME type which is already attached with another reference.
Adding ‘.appcache’ or ‘apache’ was made it functional.
<mimeMapfileExtension=".apache"mimeType="text/cache-manifest" />
(source: Load cache manifest file)

chrome.tabCapture.capture returned stream is undefined

I've got some js code in a chrome background extension of the following :
function handleCapture(stream) {
console.log('content captured');
console.log("backround.js stream: ", stream);
alert(stream);
// localStream = stream; // used by RTCPeerConnection addStream();
// initialize(); // start signalling and peer connection process
}
function captureCurrentTab() {
console.log('reqeusted current tab');
chrome.tabs.getSelected(null, function(tab) {
console.log('got current tab');
var selectedTabId = tab.id;
chrome.tabCapture.capture({
audio : false,
video : true
}, handleCapture);
});
}
However, when this is ran, the "handleCapture" variable "stream" that is passed in is always undefined? Is this to be expected or is there something that I am missing here?
Also, I've confirmed that my manifest.json contains the capture permission and I am using chrome canary Version 31.0.1607.1 canary Aura.
Thanks,
Mike
I had this same issue when I was trying to drive a tabCapture purely from a background script, I found this on the tabCapture reference page:
Captures the visible area of the currently active tab. This method can only be used on the currently active page after the extension has been invoked, similar to the way that activeTab works. Note that Chrome internal pages cannot be captured.
My understanding is that this means you need to drive it from a browserAction for your extension, like so:
chrome.browserAction.onClicked.addListener(function(request) {
chrome.tabs.getSelected(null, function(tab) {
chrome.tabCapture.capture({audio: true, video: true}, callback);
});
});
That's what worked for me.
You should probably provide some constraints to make it work. See:
http://developer.chrome.com/extensions/tabCapture.html#type-MediaStreamConstraint
The capture param you provided is a MediaTrackConstraint, see:
http://dev.w3.org/2011/webrtc/editor/getusermedia.html#mediastreamconstraints
that is also a simple JS object, where you should set some mandatory options, see:
http://dev.w3.org/2011/webrtc/editor/getusermedia.html#idl-def-MediaTrackConstraints
So the following should help, if you set all the needed settings in mandatory object:
chrome.tabCapture.capture({
audio : false,
video : true,
videoConstraints: {
mandatory: {
width: { min: 640 },
height: { min: 480 }
}
}
}, handleCapture);

Detect if browser extension is installed for Opera

I have created browser extension/add-on for Chrome, Firefox, Safari and IE using Crossrider framework
Now since Crossrider does not provide support for Opera, I have created a native extension for the same.
My problem is how to detect if a user(using Opera), visiting our website, has the extension installed or not ?
Similar Question is answered. Is there any better option?
Or any simple way to check(crossbrowser) if an extension is installed or not so that I don't have to use Crossrider.API?
Or is there anything similar to window.navigator.plugins for extensions?
You can communicate with your page using simple content script for your domain only using window.postMessage function.
Content script code:
window.addEventListener("message", function(event) {
if (event.source !== window) return; // We only accept messages from ourselves
switch (event.data.type) {
case 'get_info': window.postMessage({type: "info", browser: 'opera'}, "*"); break;
}
}, false);
Your page code:
window.addEventListener('load', function () {
window.addEventListener("message", function (event) {
switch (event.data.type) {
case 'info': console.log(`browser is ${event.data.browser}`); break;
}
});
window.postMessage({type: "get_info"}, "*");
});

Get the urls of all the tabs in all windows using chrome extension

Is this possible for chrome extension to get all the URLs in all tabs using chrome extension ?
I have got the url of current Tab using this code
chrome.tabs.getSelected(null, function(tab) {
tabUrl = tab.url;
alert(tabUrl);
});
We need the following permissions in manifest.json file
"permissions": [
"tabs"
]
My question is to find out the URLs in all tabs ?
You could do something like this:
chrome.windows.getAll({populate:true},function(windows){
windows.forEach(function(window){
window.tabs.forEach(function(tab){
//collect all of the urls here, I will just log them instead
console.log(tab.url);
});
});
});
With chrome.tabs.query method, you can also simply do,
chrome.tabs.query({},function(tabs){
console.log("\n/////////////////////\n");
tabs.forEach(function(tab){
console.log(tab.url);
});
});

Open a "Help" page after Chrome extension is installed first time

I am new to Chrome extension. I have a question about how to make the extension to open a "Help" page automatically after installation. Currently, I am able to check whether the extension is running the first time or not by saving a value into localStorage. But this checking is only carried out when using click the icon on the tool bar. Just wondering if there is a way that likes FF extension which uses the javascript in to open a help page after the installation. Thanks.
Edit:
Thanks for the answer from davgothic. I have solved this problem.
I have another question about the popup. My extension checks the url of current tab,
if OK(url){
//open a tab and do something
}
else{
//display popup
}
Is it possible to show the popup in this way?
Check this updated and most reliable solution provided by Chrome: chrome.runtime Event
chrome.runtime.onInstalled.addListener(function (object) {
let externalUrl = "http://yoursite.com/";
let internalUrl = chrome.runtime.getURL("views/onboarding.html");
if (object.reason === chrome.runtime.OnInstalledReason.INSTALL) {
chrome.tabs.create({ url: externalUrl }, function (tab) {
console.log("New tab launched with http://yoursite.com/");
});
}
});
Add this to your background.js I mean the the page you defined on manifest like following,
....
"background": {
"scripts": ["background.js"],
"persistent": false
}
...
UPDATE: This method is no longer recommended. Please see Nuhil's more recent answer below.
I believe what you need to do is put something like this into a script in the <head> section of your extension's background page, e.g. background.html
function install_notice() {
if (localStorage.getItem('install_time'))
return;
var now = new Date().getTime();
localStorage.setItem('install_time', now);
chrome.tabs.create({url: "installed.html"});
}
install_notice();
As of now (Aug 2022) the right way to execute code on first install or update of an extension using Manifest V3 is by using the runtime.onInstalled event.
This event is documented here: https://developer.chrome.com/extensions/runtime#event-onInstalled
There is one example for this exact case in the docs now:
https://developer.chrome.com/docs/extensions/reference/tabs/#opening-an-extension-page-in-a-new-tab
Note: This example above is wrong as the callback function parameter is Object with the key reason and not reason directly.
And another example here (this one is correct but does not open a tab):
https://developer.chrome.com/docs/extensions/reference/runtime/#example-uninstall-url
chrome.runtime.onInstalled.addListener((details) => {
if (details.reason === chrome.runtime.OnInstalledReason.INSTALL) {
// Code to be executed on first install
// eg. open a tab with a url
chrome.tabs.create({
url: "https://google.com"
});
} else if (details.reason === chrome.runtime.OnInstalledReason.UPDATE) {
// When extension is updated
} else if (details.reason === chrome.runtime.OnInstalledReason.CHROME_UPDATE) {
// When browser is updated
} else if (details.reason === chrome.runtime.OnInstalledReason.SHARED_MODULE_UPDATE) {
// When a shared module is updated
}
});
This code can be added to a background service worker: https://developer.chrome.com/docs/extensions/mv3/migrating_to_service_workers/
It would be better to place a "version" number so you can know when an extension is updated or installed.
It has been answered here:
Detect Chrome extension first run / update
All you need to do is adding the snippet below to your background.js file
chrome.runtime.onInstalled.addListener(function (object) {
chrome.tabs.create({url: `chrome-extension://${chrome.runtime.id}/options.html`}, function (tab) {
console.log("options page opened");
});
});