pop

Count the number of windows open

I am trying to count the number of Tabs that are open in my google chrome browswer with javascript. Does anyone know how to do this? I wrote some javascript that I want repeated 10 times and then stop. Upon the completion of 1 iteration, I open a new window using:

5 Answers 5

It’s a good thing that webpages are sandboxed so that other websites can’t access them. If they’re windows that you’ve opened using window.open you can save the reference you receive to the window:

of course you could push this to an array if you’re opening a large number of windows.

var wins = []; //looping stuff here wins.push(window.open(url[i]); 

I think the loop is just fine, but if you want to keep track,

var winList = new Array(); var count = 10; for(var i=0; i < count; i++)< winList[i] = window.open("http://www.test.com"); >

This way, you can keep references to your windows.

So I couldn’t figure out a way doing what I wanted so I took a different approach. I said, lets see if google has the option in chrome to limit the number of tabs opened and I found someone wrote an extension to do exactly that. I don’t know how he did but it definitely works. Controlled multi-tab browsing

Look at the Google Chrome Extensions Developer Guide , in particular the Tabs page and the getAllInWindow function

chrome.tabs.getAllInWindow(integer windowId, function callback) 

Where the callback function receives an array of tabs. Meaning you can get its length.

Читайте также:  Класс в нескольких файлах java

And if the tabs you want to keep track of are possibly in different windows, then you need to look at the Windows page and the getAll function

chrome.windows.getAll(object getInfo, function callback) 

Use this to iterate over all windows and call getAllInWindow . And you’re all set.

Источник

Javascript: How to get a list of all open windows

(each window has a unique name) And then you refresh the page. The 3 popup windows are still open. Is there a way to list the names of all of the open windows and close them? This is not a duplicate question. In this question the browser is being refreshed, so you cannot simply use a global array to keep track of child windows. This is not a duplicate question.

No there is not, unless the URLs are from the same domain. Than there might be a possibility to communicate between those windows, but I don’t think you can programmatically close them, since you lost the reference to the windows.

Then they could write to localStorage, as suggested here. Maybe have the child windows listen to changes and have them close themselves.

localStorage would work. I was hoping there might be a property in the window object which I’ve yet to find.

I don’t think so. When you reloaded the page you are creating a new JS environment which has nothing to do with the previous environment, even if it was the same page.

2 Answers 2

So the questions is closed, I’ll post an answer based on the comments and research.

Firstly, to all who commented, thank you for helping.

Answer: There is not a built-in object which tracks opened windows and persists from page load to page load.

As Felix Kling pointed out, using localStorage is a possible work-around.

Try postMessage to communicate between existing windows within the same domain. That’s how i’m going to try and solve the same problem. See: https://developer.mozilla.org/en-US/docs/Web/API/Window/postMessage

        
total:

          

Источник

windows.getAll()

Gets information about all open windows, passing them into a callback.

This is an asynchronous function that returns a Promise .

Syntax

let gettingAll = browser.windows.getAll( getInfo // optional object ) 

Parameters

object . This controls what windows.Window objects are retrieved.

boolean . Defaults to false . If set to true , each windows.Window object will have a tabs property that contains a list of tabs.Tab objects representing the tabs in that window. The Tab objects will contain the url , title and favIconUrl properties only if the extension’s manifest file includes the «tabs» permission or host permissions that match the tab’s URL.

An array of windows.WindowType objects. If set, the windows.Window objects returned will be filtered based on their type. If unset the default filter is set to [‘normal’, ‘panel’, ‘popup’] , with ‘panel’ window types limited to the extension’s own windows.

Return value

A Promise that will be fulfilled with an array of windows.Window objects, representing all windows that match the given criteria. If any error occurs, the promise will be rejected with an error message.

Browser compatibility

BCD tables only load in the browser

Examples

Log the URLs for the tabs across all «normal» browser windows. Note that you’ll need the «tabs» permission or matching host permissions to access tab URLs.

function logTabsForWindows(windowInfoArray)  for (const windowInfo of windowInfoArray)  console.log(`Window: $windowInfo.id>`); console.log(windowInfo.tabs.map((tab) => tab.url)); > > function onError(error)  console.error(`Error: $error>`); > browser.browserAction.onClicked.addListener((tab) =>  browser.windows .getAll( populate: true, windowTypes: ["normal"], >) .then(logTabsForWindows, onError); >); 

Example extensions

Note: This API is based on Chromium’s chrome.windows API. This documentation is derived from windows.json in the Chromium code.

Found a content problem with this page?

This page was last modified on Mar 7, 2023 by MDN contributors.

Your blueprint for a better internet.

Источник

How to get the references of all already opened child windows

I want to get the references of all already opened child windows. is there any way? I am not using child = window.open(. ) just using window.open(. ) and opening multiple child windows.

4 Answers 4

If you don’t want to change your current code, you can simply override window.open() function:

var openedWindows = []; window._open = window.open; // saving original function window.open = function(url,name,params) < openedWindows.push(window._open(url,name,params)); // you can store names also. >

Run this code before calling window.open() . All the references to the opened windows will be stored in openedWindows array. You can access them anywhere you want

@Govind: A caveat for this solution — it is a hack and best avoided to prevent future maintenance issues. See: stackoverflow.com/questions/6223449/…

sure! to avoid future issues, it’s better to rename the function I wrote to something like window.open2() 🙂 And to change all the existing .open() calls to .open2() . But it seems @Govind doesn’t want to change anything in his codes.

The problem is, when we refresh the main window, we lose all data in openedWindows. Any idea to avoid this?

In order to not break any code that relies on the default behaviour, this changed window.open() should also return the reference after storing it in the array.

You should actually do window._open.apply(window, arguments) since passing in undefined for the arguments and not passing in anything yields different behavior. I believe explicitly passing in undefined will disable all window features

I don’t believe you can, unless you know the windows’ names, which I’m guessing you don’t. (If you know their names, you can use window.open(«», «name») to get a reference to them.)

The better option is, of course, to remember the reference returned from window.open in the first place — but you know that. 🙂

@Govind: Understood. Unfortunately, barring saving the return value or knowing the names assigned to them so you can retrieve them later with window.open(«», «name») , I don’t think there’s a third alternative.

Ok, I used the answers to this question in Oracle CRM onDemand to disable a select in a popup window executing the script from the parent window, and it worked! (I have no control over the generation of popup windows, they are opened by the application framework)

Context: In a detail page the user can add some info by clicking in a magnifying glass icon >>> a new window opens containing a search form, but a select is disturbing the administrator: If the user change its default value he/she will gain access to forbidden records!! Oh my God!

First Approach: Disable that select now!!

Attempt: I found the image’s onclick attrib with my browser’s dev tools (F12). There was a openAssocPopup method, and then i knew the name of the child window: ‘OccamPopup1’ 🙂

Okay! So let’s do some magic (executed at the parent window):

window.open("","OccamPopup1").document.getElementById("frmSearch.AQ").setAttribute("disabled", true); 

I think this may help, as this question helped to me too. You were right. Now i’m trying to wrap the child’s document object within the parent’s jQuery object so i can gain access to the entire child’s DOM. but this is another story.

Источник

Оцените статью