I have a Electron App which, on the press of a button, opens a new Window. Said Window has another button with the id of "closeBtn". Now i try to handle it in the js associated to said element with the following code
const electron = require("electron")
const path = require("path")
const remote = electron.remote
const closeBtn = document.getElementById("closeBtn")
closeBtn.addEventListener("click", function(e) {
var window = remote.getCurrentWindow();
window.close();
})
But somehow that doesn´t work, and the new window is not closing on the Button Press. What am i doing wrong?
Fixed it by adding webPreferences {nodeIntegration: true} to the values of my newly generated Window.
Related
It works perfectly fine in Chrome as it opens a new tab on meta key press and click. Please see code at
jsfiddle.
function simulateClick() {
var event = new MouseEvent('click', {
'view': window,
'bubbles': true,
'cancelable': true,
'metaKey': true
});
var a = document.createElement("a");
a.href = "http://jsfiddle.net/";
var canceled = !a.dispatchEvent(event);
}
Second, the metaKey (Mac OS) press has no effect in Firefox. The button click displays alert irrespective of meta key press (Mac OS).
I am trying to open a new tab right next to the active tab in a firefox add-on. Here is my code:
var tabs = require("sdk/tabs");
tabs.open("http://www.google.com");
This opens the new tab at the end of the tab list. I couldn't figure out how to force it to position the new tab immediately after the active tab.
Get the index of the current tab, then set the index of the new tab to that + 1
var tabs = require("sdk/tabs");
var index = tabs.activeTab.index;
tabs.open("http://www.google.com");
tabs.activeTab.index = index + 1;
Alternatively, if you look at the docs, you'll see that there's a constructor parameter called
inBackground: boolean. If present and true, the new tab will be opened to the right of the active tab and will not be active.
By combining this with the onOpen event, you can achieve the desired effect:
var tabs = require("sdk/tabs");
tabs.open({
url: "http://www.google.com",
inBackground: true,
onOpen: function(tab) {
tab.activate();
}
});
I haven't tested either of these, so some debugging might be needed.
Another way is using lower-level APIs like gtranslate does:
const { getMostRecentBrowserWindow } = require('sdk/window/utils')
const browser = getMostRecentBrowserWindow().gBrowser
const tab = browser.loadOneTab(url, {relatedToCurrent: true})
browser.selectedTab = tab
Note that it might not work with e10s.
I am trying to open a new window and close its parent window, so that I can't return to previous window and keep the application more efficient.
However calling close on parent window causes the new window to be closed as well, regardless if I call it before opening the new window or after.
What happens is new window is being opened and then closed right away.
I tried to open a new window on the event listener close for parent window... but that doesn't help.
Code example:
`app.js:`
var loginWin = Ti.UI.createWindow({
url:login.js
});
loginWin.open();
`login.js:`
var win = Ti.UI.currentWindow;
var btn = Ti.UI.createButton({.....});
btn.addEventListener('click',function(e){
//putting win.close() also causes the problem
var appCoreWindow = Ti.UI.createWindow({
url:"core.js"
});
win.close();
appCoreWidnow.open();
});
win.add(btn);
You are creating Window object inside eventhandler and when function finishes garbage collector removes all local variables from it, including appCoreWindow.
Try this instead:
var win = Ti.UI.currentWindow;
var appCoreWindow = Ti.UI.createWindow({ url:"core.js" });
var btn = Ti.UI.createButton({.....});
btn.addEventListener('click',function(e){
appCoreWidnow.open();
win.close();
});
win.add(btn);
Is there something in firefox addon through which we can register a callback which gets invoked when the addon is closed by clicking the x button on the left?
What I need is, when a user closes the addon bar using the x button, my extension loaded on that bar should be notified. Now what happens is, even though the user closes the addon bar, it is not getting closed; instead it just hides.
If we can be informed through a callback that the user has clicked on x button, then i could listen to that in the extension.
Yes sir there absolutely is: MutationObserver.
Copy paste this to a scratchpad file in Browser envirnoment and then as addon bar is closed and opened you will see a message.
// select the target node
var win = Services.wm.getMostRecentWindow('navigator:browser');
var target = win.document.querySelector('#addon-bar');
// create an observer instance
var observer = new win.MutationObserver(function(mutations) {
mutations.forEach(function(mutation) {
if (mutation.attributeName == 'collapsed') {
Services.prompt.alert(null,'title','addon bar toggled it WAS = ' + mutation.oldValue);
}
});
});
// configuration of the observer:
var config = { attributes:true, attributeOldValue:true };
// pass in the target node, as well as the observer options
observer.observe(target, config);
// later, you can stop observing
//observer.disconnect();
The easiest way to do this is to attach a command handler to the button in question. If your code runs inside the browser window, this will do:
var closeButton = document.getElementById("addonbar-closebutton");
closeButton.addEventListener("command", function(event) {
// Add-on bar is being closed, do something
}, false);
Note that this code is bound to stop working very soon as the add-on bar is being removed from Firefox.
I am developing an iOS app in Appcelerator and I want to switch between windows with the use of a swipe event listener. How can I do this? The below code does not work. The current window that I "start" from contains a table.
var window = Ti.UI.currentWindow;
window.addEventListener('swipe', function() {
// Create the new window
var win = Titanium.UI.createWindow({
title: 'Contacts',
url:'contacts_simple.js'
});
// Animate the page turn
Titanium.UI.currentTab.open(win, {animated:false});
});
I think the problem is with the Ti.UI.currentWindow. Depending on the context you're working in it may not be valid.
Google 'appcelerator currentWindow', but here's a related link:
http://developer.appcelerator.com/question/5391/currentwindow
This will not be optimal and dynamic, but to start with, try referencing the window implicitly. Meaning if you did something like
var window_x = Ti.UI.createWindow({});
try
window_x.addEventListener('swipe', function() {...});