0

I'm creating a firefox extension using Add-on SDK, which will display all the tab details including memory, title and url. I have tried to get the tab title using require("sdk/tabs"); tab package.

Below is the sample code:

popup.js

<body>
    <ul>
        <li>
            <a href="#" tabindex="1" id = "tab_list"> Settings</a>
        </li>
    </ul>
    <script type="text/javascript" src="popup.js"></script>
</body>

popup.js

var tab_button = document.getElementById("tab_list");
tab_button.addEventListener("click", function() {
    addon.port.emit("GET_TABS");
});

main file: index.js

const buttons = require("sdk/ui/button/action");
const { TogglePanel } = require("popupPanel");

var button = buttons.ActionButton({
    id: "mem-tools",
    label: "tabs info listing",
    icon: {
        "16": "./tab.png",
    },
    onClick: handleClick
});

let togglePanel = new TogglePanel();

function handleClick(state) {
    togglePanel.panel.show({position:button});
}

Panel file: popupPanel.js

var Panel = require('sdk/panel');
var self = require('sdk/self');
var { MemoryHandler } = require('memory-handler');

var memoryHandler = new MemoryHandler();
function TogglePanel() {

    this.panel = new Panel.Panel({
        width: 150,
        height: 200,
        contentURL: self.data.url("popup.html")
    });
    this.panel.port.on("GET_TABS", function() {
        memoryHandler.getAllTabs();
    });
}
exports.TogglePanel = TogglePanel;

memory-handler.js

    var tabs = require('sdk/tabs');

    function MemoryHandler() {

        return {

            getAllTabs: () => {
                for(let tab of tabs) {
                    console.log(tab.title);
                }
            }

        }
    }
    exports.MemoryHandler = MemoryHandler;

This code only fetching all tab titles from the main window and child window, but not from all other new window's tabs which is opening using _blank attribute.

Note: we can easily recreate the issue just create an html page and use the below code:

 <a href="http://www.someurl.com" target="_blank">Visit me</a> 

The page open using the "_blank" attribute is not coming under the tabs array.

Any help is appreciated. Thanks in advance!!

Vipin
  • 847
  • 1
  • 10
  • 21
  • Please [edit] to include a *complete* [mcve]. Questions seeking debugging help ("**why isn't this code working?**") must include: ►the desired behavior, ►a specific problem or error *and* ►the shortest code necessary to reproduce it **in the question itself**. Questions without a clear problem statement are not useful to other readers. See: "**How to create a [mcve]**", [What topics can I ask about here?](http://stackoverflow.com/help/on-topic), and [ask]. Try using a [snippet](https://blog.stackoverflow.com/2014/09/introducing-runnable-javascript-css-and-html-code-snippets/). – Makyen Oct 14 '16 at 15:16
  • The reason that a [mcve] is required is that we want to help. It is **much** easier to help if we don't have to recreate all the code needed to duplicate the problem. This is code that you already have. So, please help us to help you and provide a *complete* [mcve] that duplicates the problem with such questions. Without a [mcve] the amount of effort required to even begin to help you is **much** higher which *significantly* reduces the number of people willing/able to help you. Even if we put out the extra effort, we have to *guess* at significant portions of what your problem might be. – Makyen Oct 14 '16 at 15:17
  • For instance, we need to know how what you assigned to `tabs`. In addition, you say you used `require(sdk/tab)` which is invalid. You might have meant `require(sdk/tabs)` (note plural `tabs`). However, without an actual [mcve], we have to *guess* at what *exactly* you are doing. – Makyen Oct 14 '16 at 15:19
  • @Makyen Thank you for the reply, I'm getting the list of all the tabs title which is opened on the main window. But when I open a new window instead of opening a tab from main window, that is not coming in the tabs object. I will provide to give some more clarification in the code – Vipin Oct 14 '16 at 17:13
  • Brief testing indicates this works fine as written (logs to console the `title` of all tabs in all windows that were open when `for(let tab of tabs) {` is executed). We are going to to need a **complete** [mcve] and a *detailed* description of your testing so we can duplicate your issue. Is this a [private browsing](http://stackoverflow.com/a/38793196/3773011) issue? – Makyen Oct 14 '16 at 18:48
  • @Makyen, You can easily recreate the issue just create an html page and use the below code: Visit me The page open using the "_blank" attribute is not coming under the tabs array. Thanks in Advance!! – Vipin Oct 17 '16 at 08:54

1 Answers1

0

We can get all the titles from all the window tabs by creating a custom array.

index.js

 var memoryHandler = new MemoryHandler();
 tabs.on('ready', function(tab) {
     memoryHandler.addTabDetails({id: tab.id ,title: tab.title, url: tab.url});
 });

If you want to get the title which is setting by using javascript after page load, you can inject a mutation observer code in the content script of tab

memory-handler.js

var presentTabs = []
function MemoryHandler() {

    return {
        addTabDetails: (tab_array) => {
            presentTabs.push(tab_array);
        }

    }
}
exports.MemoryHandler = MemoryHandler;
Vipin
  • 847
  • 1
  • 10
  • 21