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.
Related
UPDATED WITH REASONING
Goal: Run chrome web driver in background without showing an app icon in the dock or application bar. FYI: I can already run chrome headless, but I have not been able to hide or not show the chrome icon in my app bar.
Reason: I am building an application that accesses various websites in parallel and I would not like the chrome app icons to display for the synchronous tasks that are taking place. I want to run these tasks in the background.
Current Code with chrome options that will display headless chrome
chrome_options = Options()
chrome_options.add_argument('headless')
chrome_options.add_argument('window-size=1200x600')
driver = webdriver.Chrome(executable_path=executable_text, chrome_options=chrome_options)
Research
list of chrome options: https://peter.sh/experiments/chromium-command-line-switches/
one of many posts explaining how to do headless chrome: Selenium - chrome Driver fail to start in background (without a start-up window)
How can I hide the chromedriver app icons from displaying? Is there a chrome option I can set in addition to setting the 'headless' chrome option? Is this more of an OS setting that needs to be made to run chromedriver in the background? I am successfully able to run multiple chromedrivers in headless mode but it would be nice to NOT show the chrome icons in the dock or application tray similar to how phantomjs does not launch an app icon.
^In the image above those are the chromedrivers that run in headless mode displaying in my dock and ideally for my program I would not even want to display those icons since it is all background processing anyways.
Update (this worked for me to hide the chromedriver icons at least on my Mac):
I was not able to find a Chrome Option to accomplish hiding the chromedriver icons that appear in the dock. However, I was able to edit the Info.plist file for Chrome as part of my program to do the trick (of hiding chromedriver icons) using the LSBackgroundOnly key.
In the Info.plist for Chrome, programmatically, I entered:
<key>LSBackgroundOnly</key>
<string>1</string>
On my Mac using python3 code I used os.system to execute a terminal command using defaults to enter the LSBackgroundOnly key when the program is executing and then at the end of program execution I delete the LSBackgroundOnly key from the Info.plist.
like this:
1)
defaults write /Applications/Google\ Chrome.app/Contents/Info.plist LSBackgroundOnly -string '1'
2)
defaults delete /Applications/Google\ Chrome.app/Contents/Info.plist LSBackgroundOnly
AS INFO: This is tricky because your normal chrome app may start up in background mode if you don't add/delete LSBackgroundOnly properly during program execution. In my experience, worst case scenario you may have to manually remove LSBackgroundOnly from the Info.plist file then restart your computer to get Chrome out of background mode.
I remove the LSBackgroundOnly key from the Info.plist file in my program after program execution because the key only needs to be in the file when the chromedrivers are launched. After that I want to be able to use regular Chrome app without issues of it opening in background mode. But, if you understand your program execution and handle your exceptions, this will definitely work to hide the icons correctly and there will be nothing different in using your regular Chrome app.
Happy hacking.
This is not possible using Selenium. I don't really understand why you want to do this. Is there a real purpose, or it's just you don't want it to show? Even if the Chrome app shows on the dock, in headless mode, it works pretty good.
However, if you want your icon to not appear in the dock when opened, then you need to disable it by modifying the Info.plist file for that application.
In your Info.plist for Chrome, add this
<key>LSUIElement</key>
<true/>
This will prevent your app icon from appearing in dock, when working. I found this out a long time ago and this is the post where I saw it.
PS - I have not tested it for Chrome app. Please test at your own peril.
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 created a chrome extension which opens a new tab window,plays youtube videos and collects the network statistics.
What I currently do is to launch the chrome and then click the extension icon to run the extension. Now I want to automate the process of launching chrome and the extension simultaneously from command line.
Please guide me how I can open and run the extension on chrome from command line.
I have tried --load-and-launch-app and --load-extension.
You can move the logic of your extension in your background.js page. In this way when chrome starts, it will run your code in background.js without clicking extension icon.
For starting chrome use -
start chrome
I am able to launch Chrome browser with extension in the browser. But I am not able to interact with the extension.
When I launch Chrome browser manually, I go to chrome://extensions and configure command for that extension and set short cut like "Alt+H" to launch the extension from short cut and after hitting Alt+H the extension gets invoked.
But when I launch chrome browser with extension using watir webdriver, the configure command setting for that extension is not set. After I set that manually again to "Alt+H" and try to invoke the extension on the browser which is launched using watir-webdriver, I don't see extension getting invoked.
Please let me know how can I interact with the chrome extension using watir-webdriver.
I do not think watir can interact with the extensions. It can only interact with the page. But, I could be wrong. I had moderate success with Sikuli when I needed to do something like that.
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.