Say, there are 10 tabs in Firefox browser window.
How can I add a tab in after 2nd tab by a Firefox extension code?
gBrowser.addTab method only appends to the tab list.
There is no easy, direct way of doing what you want. If you really want to open a tab directly at a specific index then you can take a look at the code for gBrowser.addTab() and the code for gBrowser.moveTabTo(); copy them and modify them to do what you want. Note that this code is in an XML representation of JavaScript. Thus, you will need to reformat it a bit if you want to use it.
However, the easy way to do this is to open the tab, gBrowser.addTab(). Then, move it to the index that you desire, gBrowser.moveTabTo().
The following code will do what you want. When I attached this code to a button, the tab visually appeared to open at the index specified. It did not open first at the end of the tabs and then appear to move. There was no user noticeable difference between doing this, adding then moving, instead of actually adding the tab at the specified index.
function handleButtonCommandEvent(event) {
let window = event.view;
//Create the window variable if it does not exist. It should
// already be defined from event.view.
// This should work from any Firefox context.
if (typeof window === "undefined") {
//If there is no window defined, get the most recent.
var window=Components.classes["#mozilla.org/appshell/window-mediator;1"]
.getService(Components.interfaces.nsIWindowMediator)
.getMostRecentWindow("navigator:browser");
}
//Test addTabAtIndex()
addTabAtIndexInWindow(window, 2, "http://www.ebay.com/")
}
/**
* Open a tab in specified window at index.
*/
function addTabAtIndexInWindow(window, index, URL, referrerURI, charset, postData,
owner, allowThirdPartyFixup ) {
//Get the gBrowser for the specified window
let winGBrowser = window.gBrowser;
//Open a new tab:
let newTab = winGBrowser.addTab(URL, referrerURI, charset, postData,
owner, allowThirdPartyFixup );
//Immediately move it to the index desired:
winGBrowser.moveTabTo(newTab,index);
}
Related
Do we have to create new popup if google crome updated or we can continue with the older one.
No one is sure what you're asking, but I'm going to take a stab at it. I believe you're asking if you have to use a new popup every time an advertisement is changed? If that is the case, the answer is no, you don't always have to have a new popup. HOWEVER, if the window was closed by the user, a new popup will have to be created. The following code will bring a named window to the front:
function GetAdWindow() {
// Change the window.open parameters to your liking
var AdWindow = window.open("", "AdWindow", "toolbar=no,location=no,status=no,menubar=no,scrollbars=yes,resizable=no,width=500,height=500");
return AdWindow;
}
After that, you have to determine if the page is blank, or already has an advertisement on it.
function UpdateAd(){
var AdWindow = GetAdWindow();
// If the AdWindow wasn't populated (meaning it was closed)
if (AdWindow.location.href === "about:blank") {
AdWindow.location = /*ADVERTISEMENT URL*/
} else {
// DO WHATEVER YOU WANT IF THE WINDOW HAD CONTENT
}
}
If you asking if your popup will go away if Chrome has an update, the answer is yes. Chrome as a whole will shot down and close all of the windows, then start back up clean.
I have a html page. In the body of the page I am calling onload event which calls javascript function to open a pop up window. here is the code:
var newWindow = null;
function launchApplication()
{
if ((newWindow == null) || (newWindow.closed))
{
newWindow = window.open('abc.html','','height=960px,width=940px');
}
}
when I move to another page, and come back to that page again, popup reopens, although it is already opened. Please guide me to proper direction so that if pop up is already open then it should not open again. I tried document.referred but it requires the site online, currently I am working offline.
newWindow = window.open('abc.html','com_MyDomain_myWindowForThisPurpose','height=960px,width=940px');
Give the window a name. Basing the name on your domain like this, prevents the chances of you picking a name someone else happened to choose.
Never make up a name that begins with _, those are reserved for special names the browser treats differently (same as with the "target" attribute of anchor elements).
Note that if the window of that name was opened with different options (e.g. different height), then it'll keep those options. The options here will only take effect if there is no window of that name, so you do create a new one.
Edit:
Note that the "name" is of the window, not of the content. It doesn't affect the title (newWindow.document.title will affect that, as of course will code in abc.html). It does affect other attempts to do stuff across windows. Hence another window.open with the same name will reuse this window. Also a link like clicky! will re-use it. Normal caveats about browsers resisting window-opening in various scenarios (popup-blocking) apply.
To open a window and keep a reference to it between page refresh.
var winref = window.open('', 'MyWindowName', '');
if(winref.location.href === 'about:blank'){
winref.location.href = 'http://example.com';
}
or in function format
function openOnce(url, target){
// open a blank "target" window
// or get the reference to the existing "target" window
var winref = window.open('', target, '');
// if the "target" window was just opened, change its url
if(winref.location.href === 'about:blank'){
winref.location.href = url;
}
return winref;
}
openOnce('http://example.com', 'MyWindowName');
You can check if the window is open or closed by re-assigning a reference to it when it closes. Example:
var newWindow;
var openWindow = function(){
newWindow = newWindow || window.open('newpage.html');
newWindow.focus();
newWindow.onbeforeunload = function(){
newWindow = null;
};
};
Use the "closed" property: if a window has been closed its closed property will be true.
https://developer.mozilla.org/en-US/docs/Web/API/Window/closed
When you move on another page (on the same domain), you can re-set the window.open variable with popup page like this :
https://jsfiddle.net/u5w9v4gf/
Step to try :
Click on Run (on jsfiddle editor).
Click on Try me (on preview).
Click on Run to move on another page, the variable will be re-set.
Code :
window.currentChild = false;
$("#tryme").click(function() {
if (currentChild) currentChild.close();
const child = window.open("about:blank", "lmao", 'width=250,height=300');
currentChild = child;
//Scrope script in child windows
child.frames.eval(`
setInterval(function () {
if (!window.opener.currentChild)
window.opener.currentChild = window;
}, 500);
`);
});
setInterval(function() {
console.log(currentChild)
if (!currentChild || (currentChild && currentChild.closed))
$("p").text("No popup/child. :(")
else
$("p").text("Child detected !")
}, 500);
i am developing a thunderbird extension.i want to get the mailitem content in home window and compose mail window.How can i achieve this?
Regards
Sanju
When you are interested in how something is done in one of the Thunderbird windows, the way to figure out how it is implemented in the XUL DOM is to install the [add-on][2] [DOM Inspector][3] and use it to investigate what the contents of the DOM looks like. You probably also want, the [Element Inspector][4] add-on which is a very useful addition to the DOM Inspector (shift-right-click opens the DOM Inspector to the element clicked). You might also find [Stacked Inspector][5] helpful.
The other thing to do is find an extension that does something in the same general area in which you are wanting to work. Then download that extension and see how they did the thing you are interested in.
Your question does not provide enough information to give you an exact, detailed response. We need to know the context in which you are running. Was the script that is being run launched as part of a UI event from the main window? A UI event from the compose window?
If the script was launched from a UI event in the compose window, you can get access to the message content with:
let editor = document.getElementById("content-frame");
let editorDocument = editor.contentDocument;
let messageBody = editorDocument.getElementsByTagName("body")[0];
This should work, but I have not verified it:
let messageBody = document.getElementById("content-frame").contentDocument.body;
As to the home window: The message content is located in a <browser id="messagepane"> element. Once you have the tab, you should be able to find the <browser> from there.
In Firefox, you can find the <browser> element with:
//Create some common variables if they do not exist.
// This should work from any Firefox context.
// Depending on the context in which the function is being run,
// this could be simplified.
if (typeof window === "undefined") {
//If there is no window defined, get the most recent.
var window=Components.classes["#mozilla.org/appshell/window-mediator;1"]
.getService(Components.interfaces.nsIWindowMediator)
.getMostRecentWindow("navigator:browser");
}
if (typeof document === "undefined") {
//If there is no document defined, get it
var document = window.content.document;
}
if (typeof gBrowser === "undefined") {
//If there is no gBrowser defined, get it
var gBrowser = window.gBrowser;
}
//Get the current tab & browser.
let tab = gBrowser.selectedTab;
let browserForTab = gBrowser.getBrowserForTab( tab );
It should be similar in Thunderbird.
I'm trying to create a Chrome extension that will pop up a new window in a position relative to the recently focused window. I don't think I'm using the chrome.windows.getCurrent or chrome.windows.getLastFocused methods properly to do this. Every time I do, I get an undefined alert when I try to show a property of that window.
In my background.js file, I have:
chrome.pageAction.onClicked.addListener(showPopup);
function showPopup() {
var left = chrome.windows.getCurrent(function (w) {
w.left - 200;
// also tried: return w.left - 200;
});
alert(left); // undefined
}
Reading the chrome.windows API docs left me confused on how to actually return an attribute of a window. Can anyone shed some light here?
I'm not sure if there's another (or better) way to do this, but I got this to work:
chrome.pageAction.onClicked.addListener(getCurrentInfo);
function getCurrentInfo() {
chrome.windows.getCurrent(showPopup);
}
function showPopup(win) {
alert(win.left); // correct pixel count from left
}
My original attempt was wrong because I was trying to return a value from the get() callback function and place that into a variable. This, essentially evaluated to var left = chrome.windows.get(winId, 790), which is an invalid function call. Rather than that, my call to chrome.windows.create() needed to be inside the callback function.
Also, a miss before was that the click listener calls a function with a tab parameter, not a window parameter. So I needed the getCurrentInfo() function to pass in a tab and then chrome.windows.getCurrent() to pass in window info.
I have been trying to figure this one out for a while, but when I try to reference a window that I opened the handle is always undefined.
It is worth noting that this is being used in a userscript and here is the snippet in question:
var donateWindow;
// ######################################################################
// # Show the donation popup and open a window to paypal site
// ######################################################################
function showDonateWindow()
{
if (window.confirm("Question here"))
{
if (! (typeof(donateWindow) == 'undefined' || donateWindow.closed)) window.donateWindow.close();
window.donateWindow = window.open("http://somesite.com/","tabName");
}
}
Any help on this would be very appreciated. It would seem no matter what I do window.open returns the value "undefined".
My goal is to have a popup shown, but if one is already open it should just replaced the old one. This works as expected in FF, but for the life of me I can not get it going in Chrome.
Why are you trying to close existing window before open a new one? you don't need to do that.
if you just use the same name for window when you open it, it will replace the existing one if there is.
this means you don't need to look for if there is an opened window.
function showDonateWindow()
{
if (window.confirm("Question here"))
{
window.open("http://somesite.com/","donateWindowName");
}
}