How to quit Electron app on Mac? - macos

Below is stock code from the Electron website (https://electron.atom.io/docs/tutorial/quick-start/):
app.on('window-all-closed', () => {
if (process.platform !== 'darwin') {
app.quit()
}
})
On Mac OS 10.12.4, when the above is called, it will not close the app. Only the window. Adding app.quit() above the conditional does close the app. Did they leave something out specific to Mac OS X that prevents the app from closing?

You will want to learn how to read comments in code. Here's the full excerpt from the page you linked:
// Quit when all windows are closed.
app.on('window-all-closed', () => {
// On macOS it is common for applications and their menu bar
// to stay active until the user quits explicitly with Cmd + Q
if (process.platform !== 'darwin') {
app.quit()
}
})
The two lines above the if-statement correlate to the if-statement. This basically says that we are not quitting the app on macOS because this is a common functionality found in other apps.

Related

tabs onUpdated event not detected on Safari extension?

I am trying to develop a simple web extension/addon under Safari, which is using the tabs onUpdated event. I used the Safari XCRUN converter: https://developer.apple.com/documentation/safariservices/safari_web_extensions/converting_a_web_extension_for_safari
What I am trying to do is :
Open new tab on Google Scholar with set prefs params, from "options.js" script (Options page code below)
Listen for this tab to be updated and ready (e.g. tab status is complete)
Then, inject a content script that will simulate the user click on save button (i.e. on GScholar page)
Then remove the listener, and wait 1,5s (for GS tab to reload and finish saving) in order to finally close this tab.
// Detect browser language
const gsUrl = currentBrowser.i18n.getUILanguage().includes("fr")
? GSCHOLAR_SET_PREFS_FR_URL
: GSCHOLAR_SET_PREFS_COM_URL;
// Listener to detect when the GS tab has finished loading
const gsTabListener = (tabId, changeInfo, tabInfo) => {
if (changeInfo.url && changeInfo.url.startsWith(GSCHOLAR_HOST)) {
currentBrowser.tabs.executeScript(
tabId,
{
code: `document.getElementsByName("save")[0].click();`,
},
() => {
currentBrowser.tabs.onUpdated.removeListener(gsTabListener);
setTimeout(() => currentBrowser.tabs.remove(tabId), 1500);
}
);
}
};
currentBrowser.tabs.onUpdated.addListener(gsTabListener); // Add tab listener
currentBrowser.tabs.create({
url: `${gsUrl}?inst=${gScholarInstIdList.join("&inst=")}&save=#2`,
active: false,
}); // Open GS tab according to browser language
The problem is that it works well on Chrome/Edge/Firefox (on MacOS), but not on Safari : the GS tab is opended but isn't closed and nothing happens :-/
PS:
It seems tabs onUpdated event is well supported on Safari according to MDN.
https://developer.mozilla.org/en-US/docs/Mozilla/Add-ons/WebExtensions/API/tabs/onUpdated
I have also tried webNavigation onCompleted event, but same !
Developing on : MacBookAir under MacOS Monterey 12.4, Safari 15.4 (17613.2.7.18), XCode 13.3.1 (13E500a), extension is bundled with Webpack 5.68.0 (e.g. building all assets files).
I really don't see what I am doing wrong and why wouldn't this tab event be intercepted ?
Thanks for your feedback.
After debugging I finally sloved this by noticing that in fact the events were triggered, but missed because of the availability and values of parameters passed into callabck (changeInfo, details) depending on the browser we're on.
So I switched from onUpdated to webNavigation.onCompleted API, which is better suited to our need (tab page fully loaded) and whose parameter is simple and consistent across browsers :-)
const uiLanguage = currentBrowser.i18n.getUILanguage().includes("fr")
? "fr"
: "com"; // Detect browser language
const gsUrl = `${GSCHOLAR_SETTINGS_HOST}.${uiLanguage}`;
// Listener to detect when the GS tab has finished loading
const gsTabListener = (details) => {
if (details && details.url && details.tabId) {
if (details.url.startsWith(`${gsUrl}/scholar_settings?`)) {
currentBrowser.tabs.executeScript(details.tabId, {
code: `document.getElementsByName("save")[0].click();`,
});
} else if (details.url.startsWith(`${gsUrl}/scholar?`)) {
currentBrowser.webNavigation.onCompleted.removeListener(
gsTabListener
);
currentBrowser.tabs.remove(details.tabId);
}
}
};
currentBrowser.webNavigation.onCompleted.addListener(gsTabListener); // Add GS tab listener
currentBrowser.tabs.create({
url: `${gsUrl}/scholar_settings?inst=${gScholarInstIdList.join(
"&inst="
)}&save=#2`,
active: false,
}); // Open GS tab according to browser language

LG WebOS 3.0 TV App Exit Button Close App and

I am developing a TV app for LG 4K TVs in webOS 3.0.
self_evaluation_checklist_3.4.xlsx lists a requirement for Exit button behavior as below.
"For webOS 3.0, pressing the EXIT button the app is completely closed and does not remain on the Recent list."
I have been searching but I haven't had any luck finding the API call to close the app completely and also removes the app from the Recent list.
All I could find is webOS.platformBack(); but that only takes back to the Home screen of the TV and doesn't close the app.
How can I close the app completely and don't list the app in the Recent list?
This is correct method ( webOS.platformBack(); ).
At least our app uses the same method for all 3 generations of WebOS and has never been declined by LG QA Center for this.
To exit the app and leave it in the Recent list I used the following:
const APPLICATION_MANAGER_SERVICE = 'luna://com.webos.applicationManager';
const TV_APP_ID = 'com.webos.app.livetv';
function sendAppToBackground() {
webOS.service.request(APPLICATION_MANAGER_SERVICE, {
method: 'launch',
parameters: { id: TV_APP_ID },
onSuccess(response) {
if (response.returnValue === false) {
console.error(`Error sending Application to background and bringing TV Application with ID ${TV_APP_ID} to the foreground.`);
forciblyExitApp();
}
},
onFailure(error) {
console.error(error);
forciblyExitApp();
},
});
}
function forciblyExitApp() {
window.close();
}

How to run on browser start

I'm new to Firefox addon development.
I have the following code:
var windows = require("sdk/windows").browserWindows;
windows.on("open", function() {
// do stuff
});
But this only runs for windows created after the browser is started, not the first one.
How can I fix this?
I know I could just copy the code outside of the open event, but then it also runs when the addon is installed and I don't want that.
I found the answer here.
exports.main = function (options, callbacks) {
if (options.loadReason == "startup") {
// do stuff
}
};

Firefox add-on triggers code in each open window

I have a Firefox add on that displays a pop-up when it sees a certain response header. This works fine when I have a single window. However, when I have multiple windows open, and one of the tabs in a window triggers the pop-up code in my add-on (due to the presence of said header) I get a pop-up in each of my open windows. For example, if I have 3 windows open, I get 3 different pop ups, one for each windows. Is this the default behavior, and is there an easy in-built way to fix this using their SDK.
Edit:
I have the following code:
Util.requestBlock(httpChannel) {
/*load response headers here*/
if (responseHeaders.includes("header_xyz"))
alert("show popup");
}
Util.monitor = function(w) {
this.obsService = Components.classes['#mozilla.org/observer-service;1'].getService(Components.interfaces.nsIObserverService);
this.obsService.addObserver(this, 'http-on-examine-response', false);
}
Util.monitor.prototype = {
'observe': function(subject, topic, data). {
if (topic == 'http-on-examine-response'). {
var channel = subject.QueryInterface(Components.interfaces.nsIHttpChannel);
var block_response = new Util.RequestBlock(channel);
}
},
};
The Util.monitor adds an observer. Whenever a response is received, the "Observe" function is called.
var windows = require("window-utils");
for (window in windows.browserWindowIterator)
doToWindow(window);

How to test if window is currently focused

Is there a way to test if a window is currently focused? I know I can do this with addEventListener of activated on the window, and use a global var to track it, but the thing is I need to test this on startup of my addon, so this is before a chance to add any event listeners.
Doing window == Services.wm.getMostRecentWindow(null) will not work because other application windows may be over it. So I guess I need to know if firefox is the currently top most window.
tldr: I need to test if the current window is focused, meaning not just of any other firefox windows, but of all windows on the OS.
With the add-on sdk:
require("sdk/window/utils").isFocused(window)
Without the add-on sdk:
function isFocused(window) {
const FM = Cc["#mozilla.org/focus-manager;1"].
getService(Ci.nsIFocusManager);
let childTargetWindow = {};
FM.getFocusedElementForWindow(window, true, childTargetWindow);
childTargetWindow = childTargetWindow.value;
let focusedChildWindow = {};
if (FM.activeWindow) {
FM.getFocusedElementForWindow(FM.activeWindow, true, focusedChildWindow);
focusedChildWindow = focusedChildWindow.value;
}
return (focusedChildWindow === childTargetWindow);
}
isFocused(window);

Resources