background.js only works when opened DevTools in chrome extensions - google-chrome-extension

background.js
(function () {
chrome.windows.onRemoved.removeListener();
chrome.windows.onRemoved.addListener(function (windowid) {
alert(0);
});
})()
manifest.json
{
"name": "Merge Windows1",
"version": "1.0.2",
"description": "Merges all of the browser's windows into the current window",
"background": {
"persistent": false,
"scripts": ["background.js"]
},
"permissions": [
"input", "http://*/*", "tabs"
],
"manifest_version": 2
}
When I opened the DevTools by click "background page" in Chrome Extensions, the code in background.js works and alert when close browser. Otherwise no alert box.

Related

`chrome.browserAction.setIcon()` Is not not persistent after tab reload?

When extension toolbar icon set via chrome.pageAction.setIcon() the icon reverted to default after tab reload (F5)
In the documentation it says:
If you want to create an icon that isn't always active, use a page
action instead of a browser action.
Which suggests that the icon supposed to be persistent..
Am I missing something?
Sample extension:
manifest.json
{
"background": {
"persistent": true,
"scripts": [ "background.js" ]
},
"browser_action": {},
"manifest_version": 2,
"name": "test",
"permissions": [ "tabs" ],
"version": "0.0.1"
}
background.js
chrome.tabs.getSelected( tab =>
{
chrome.browserAction.setIcon({
path: "data:image/bmp;base64,Qk0eAAAAAAAAABoAAAAMAAAAAQABAAEAGAAAAP8A", //red square icon
tabId: tab.id
});
});

Chrome extension is loading the wrong icon on Chrome startup

The following manifest is supposed to load the off.png browser action icon, but when I shut Chrome down while the on.png icon is active, it loads the same on icon automatically for some reason when I start up Chrome.
Is this some sort of a bug?
Manifest:
{
"background": {
"scripts": ["js/bg/bg.js" ],
"persistent": false
},
"browser_action": {
"default_icon": "style/off.png",
"default_title": "__MSG_mainTitle__"
},
"default_locale": "en",
"description": "__MSG_description__",
"icons": {
"128": "style/on.png"
},
"manifest_version": 2,
"name": "__MSG_name__",
"offline_enabled": true,
"version": "2.5.5"
}
bg.js:
chrome.browserAction.onClicked.addListener(function(){
chrome.browserAction.setIcon({path: "/style/" + 'on' + ".png"})
});

How to open a mailto link with a click on a Chrome extension icon?

With the following code I try to open a mailto: link in a tab if the user clicks on the Chrome extension icon. However, upon click on the icon, no action is being performed.
manifest.json
{
"manifest_version": 2,
"name": "Extension",
"description": "Description",
"version": "1.0",
"browser_action": {
"default_icon": "icon.png"
},
"permissions": [
"tabs"
]
}
popup.js
chrome.browserAction.onClicked.addListener(function tab) {
var emailUrl = "mailto:address#domain.com";
chrome.tabs.create({ url: emailUrl });
};
There are multible issues with your code:
You never require you popup.js.
You JavaScript code is invalid.
I recommend using an eventPage to trigger the E-Mail link:
manifest.json
{
"manifest_version": 2,
"name": "Extension",
"description": "Description",
"version": "1.0",
"browser_action": {
"default_icon": "icon.png"
},
"background": {
"scripts": ["eventPage.js"],
"persistent": false
}
}
eventPage.js
chrome.browserAction.onClicked.addListener(tab => {
chrome.tabs.create({ url: 'mailto:address#domain.com' })
})

How to display a popup window just when chrome extension is installed

I need to know how to display a pop up window when an extension is installed.What I am trying to ask is that when I install my extension,at that moment itself, a popup window should be opened asking for a username and password.How can I do that?I am not familiar with this issue.
Here is my manifest.json
{
"name": "Calpine Extension",
"version": "1.0",
"description": "Log on to calpinemate",
"manifest_version": 2,
"browser_action": {
"default_icon": "icon_128.png"
},
"background": {
"persistent": false,
"scripts": ["background.js"]
},
"browser_action": {
"default_title": "Calpine Extension",
"default_icon": "calpine_not_logged_in.png"
},
"permissions": [
"*://blog.calpinetech.com/test/index.php",
"alarms",
"notifications"
],
"web_accessible_resources": [
"/icon_128.png"]
}
Try this:
chrome.runtime.onInstalled.addListener(function (details) {
if (details.reason == "install") { //reason ( enum of "install", "update", or "chrome_update" )
//Show the PopUp
}
});
http://developer.chrome.com/extensions/runtime.html#event-onInstalled

Chrome extensions - unload event page immediately

I'm new to Chrome extension developing. I've written an extension that shows a notification every time a new tab is created. It works, however if I open a tab and immediately open another tab, the notification will be shown only for the first tab.
On the Chrome extension documentation I read this:
Once the event page has been idle a short time (a few seconds), the chrome.runtime.onSuspend event is dispatched.
Apparently it explains why there was no notification for the second tab. Is it possible to unload the event page immediately (i.e. once the notification is shown), without waiting a few seconds?
This is my code:
manifest.json
{
"name": "test",
"version": "1.0",
"description": "notification test",
"manifest_version": 2,
"permissions": [ "tabs", "notifications" ],
"background": { "scripts": ["background.js"], "persistent": false }
}
background.js
var notification = webkitNotifications.createNotification(
'48.png',
'hello',
'this is a test'
);
chrome.tabs.onCreated.addListener(function(tab) {
notification.show();
});
Code a)
manifest.json
{
"name": "test",
"version": "1.0",
"description": "notification test",
"manifest_version": 2,
"permissions": [ "tabs", "notifications" ],
"background": { "scripts": ["background.js"], "persistent": false },
"web_accessible_resources": [
"48.png"
]
}
background.js
function notify(tab){
console.log(tab);
var notification = webkitNotifications.createNotification(
'48.png',
'hello',
'this is a test'
);
notification.show();
}
chrome.tabs.onCreated.addListener(notify);
In this solution you will see two notifications if new tab is created first time/immediately after another tab because of https://code.google.com/p/chromium/issues/detail?id=162543. But here you are struck to event pages instead of background pages.
Code b)
By Making event page to background page you can get desired functionality running; However this do not use event page if you are particular about it.
manifest.json
{
"name": "test",
"version": "1.0",
"description": "notification test",
"manifest_version": 2,
"permissions": [ "tabs", "notifications" ],
"background": { "scripts": ["background.js"], "persistent": true },
"web_accessible_resources": [
"48.png"
]
}
background.js
function notify(tab){
console.log(tab);
var notification = webkitNotifications.createNotification(
'48.png',
'hello',
'this is a test'
);
notification.show();
}
chrome.tabs.onCreated.addListener(notify);

Resources