I tried everything but nothing seems to work for me. I tried to detect the closing of the browsing by detecting window closing like the code below but that does not work for me. What am I doing wrong and is there another solution to detect Chrome closing?
chrome.windows.onRemoved.addListener(function(windowId){
alert("Browser exit!");
});
Your general problem is that quitting the browser means that browser can no longer run your script to check if it has quit yet -- it's like trying to ask a person if they've died: the answer (if you get one) is only ever going to be "no".
One solution you might try is a long-running background page with the background permission:
Makes Chrome start up early and and shut down late, so that apps and extensions can have a longer life.
When any installed hosted app, packaged app, or extension has "background" permission, Chrome runs (invisibly) as soon as the user logs into their computer—before the user launches Chrome. The "background" permission also makes Chrome continue running (even after its last window is closed) until the user explicitly quits Chrome.
This permission allows the Chrome to keep running your extension even after all browser windows have closed. Normally, Chrome can only do things when it has windows open, so we run into the problem of Chrome being unable to detect when all windows have closed (as explained above). With the background permission, Chrome retains the ability to run extension code even when all windows have closed.
i'm just looking to get control when my extension exits not the entire browser; my extension uses a text box exclusively, so i set its onblur property to a function and put my exit code there; probably not what you're looking for, but it works for me!
the chrome developer website recommends not using background pages due to "memory and other resources they consume";
i agree a more consistent way to get control on extension exit would be useful; it is an html page that otherwise seems integrated with the dom;
chrome does not provide any API to detect chrome browser is going close. However, we can detect chrome browser opening event inside background page when using chrome extension.
background.js
chrome.runtime.onStartup.addListener(function () {
console.log("extension started: " + Date.now());
});
this event will fire when chrome browser will start. next you can use any trick to identify the chrome browser was exit.
Related
Edit: Thank you wOxxOm. It worked :)
chrome.tabs.move(XXX,{windowId:YYY, index:ZZZ})
Somehow I did not see the move option :(
I am writing simple Google Chrome web extension and I want to manipulate the tabs and windows. I actually want to be able to move a tab between the windows. The tab will have programatically spawned dynamic page, that I do not want to reload. The windowId is not listed in the modifiable properties chrome.tabs.
Am I missing something or it is currently not supported? What are the workarounds?
p.s. In extreme case also powershell or .Net might be acceptable (I have total control of the target PC). Currently spawning clearly distinguishable iframe over the web pages (background script injected and message listener) but the iframe gets reloaded, which I want to avoid.
p.p.s. Workaround might be to detect the currently active window and spawn new tab in the active window (if required), but this would require to save the page state, close the inactive tab and spown new one in the active window (this should be possible)
p.p.p.s. Main target is Google Chrome, but want it also for Microsoft Edge. Hopefully also for other browsers.
Edit: Thank you wOxxOm. It worked :)
chrome.tabs.move(XXX,{windowId:YYY, index:ZZZ})
Somehow I did not see the move option :(
I recently blocked quite a lot of ads across my entire network using AdGuard. Unfortunately, AdGuard does not prevent pop-up windows from opening. Although the advertising page is not called up, I get a popup which tells me that the requested page cannot be reached.
That's why I wanted to write a chrome extension that closes these popups automatically. Unfortunately, I fail to save the value of a checkbox in chrome.storage that is supposed to activate / deactivate the extension.
could someone help me here?
:EDIT
Okay i managed to store my value but now i'm running into the problem that i need to run a script when a page can't be loaded and i got no idea how i can do that. :S
Any Ideas? Is that even possible?
Whenever a chrome extension is installed, Chrome shows a welcome message popup:
Is there any way for this to be disabled when building the extension?
I believe there is no way to do this, since obviously it's the default behavior of chrome, which means to let user know there is a new extension installed, how to manage extensions and to prevent extension is silently installed in background.
When building extension by ourselves, we can do something when extension is first installed, by listening to chrome.runtime.onInstalled event, usually we will popup a web page or navigate user to specific sites. However, we can determine if we need this behavior, but we can't change the default behavior of chrome browser extension.
chrome.runtime.onInstalled.addListener(function() {...});
Source developer.chrome.com
I do believe that this is a good place to start.
I have a chrome extension that runs good when the chrome is opened. The problems is when the chrome application is closed.
I need to make the extension to run always - wheather the chrome browser is opened or closed.
Is there any way to make it happen that the extension will run even when chrome is closed?
This is exactly what the background permission does:
Makes Chrome start up early and and shut down late, so that apps and extensions can have a longer life.
When any installed hosted app, packaged app, or extension has "background" permission, Chrome runs (invisibly) as soon as the user logs into their computer—before the user launches Chrome. The "background" permission also makes Chrome continue running (even after its last window is closed) until the user explicitly quits Chrome.
Simply add "background" to the permissions listed in your extension's manifest.json, and it will continue running before Chrome opens and after Chrome is closed.
I have developed a chrome extension. The extension itself works fine and fast.
But when I start the browser and click on the toolbar icon of my extension it takes about 2 seconds for the popup to appear and to show its content (this happens anytime the browser is restarted).
Any idea what causes this and how to fix that?
Tip 1:
Use your popup page for rendering exclusively. It should be as light as possible. All the heavy loading/processing (localStorage, XMLHttpRequests, blocking javascript) must be done in the background page.
The background page is loaded when Google Chrome starts. Basically, it allows you to execute code and keep a page always running (although the popup is not present). For instance, streaming audio in a html5 tag with no popup.
Note: If you are not using a background page yet, you should be taking a look to message passing first.
Tip 2: Warning: This could fail
I haven't tested this yet, but maybe using the HTML5 manifest.cache can help you preventing loading again resources stored locally. But beware, this is HTML5 and is prone to changes and unstability across versions. (also, I am not completely sure that the cached resources will be loaded in memory before the popup is opened)
Hope it helps!