0
votes

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!!

1
Please edit to include a complete minimal reproducible example. 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 minimal reproducible example", What topics can I ask about here?, and How to Ask. Try using a snippet.Makyen
The reason that a minimal reproducible example 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 minimal reproducible example that duplicates the problem with such questions. Without a minimal reproducible example 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
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 minimal reproducible example, we have to guess at what exactly you are doing.Makyen
@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 codeVipin
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 minimal reproducible example and a detailed description of your testing so we can duplicate your issue. Is this a private browsing issue?Makyen

1 Answers

0
votes

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;