Access Tabs in Firefox with C ++ XPCOM Extension

What XPCOM interfaces should I use to detect opening, closing, and switching tabs, as well as retrieving the associated URL from the Firefox extension? I saw code instances that control tabs in JS, but what about from C ++?

+3
source share
1 answer

You can write a small JS component that will redirect tab events to your C ++ component using nsIObserverService.

In C ++ code, you can use this snippet to register your component as an observer for user-defined events that are used to redirect tab events.

NS_IMETHODIMP MyCppComponent::Observe(nsISupports *aSubject,
    const char *aTopic,
    const PRUnichar *aData)
{
    if( !strcmp( aTopic, "xpcom-startup" ) )
    {
        nsCOMPtr<nsIObserverService> observerService = 
            do_GetService( "@mozilla.org/observer-service;1" );
        observerService->AddObserver( this, "my-tab-open", false );
        observerService->AddObserver( this, "my-tab-close", false );
        observerService->AddObserver( this, "my-tab-select", false );
    }
    else if( !strcmp( aTopic, "my-tab-open" ) )
    {
        /* . . . */
    }
    else if( !strcmp( aTopic, "my-tab-close" ) )
    {
        /* . . . */
    }
    else if( !strcmp( aTopic, "my-tab-select" ) )
    {
        /* . . . */
    }

    /* . . . */
}

JS , ++.

function tabOpened(event) {
    var obsSvc = CC["@mozilla.org/observer-service;1"].
        getService(CI.nsIObserverService);
    obsSvc.notifyObservers(event.target.linkedBrowser.contentWindow,
        "my-tab-open", "some data");
}

function tabClosed(event) {
    var obsSvc = CC["@mozilla.org/observer-service;1"].
        getService(CI.nsIObserverService);
    obsSvc.notifyObservers(event.target.linkedBrowser.contentWindow,
        "my-tab-close", "some data");
}

function tabSelected(event) {
    var obsSvc = CC["@mozilla.org/observer-service;1"].
        getService(CI.nsIObserverService);
    obsSvc.notifyObservers(event.target.linkedBrowser.contentWindow,
        "my-tab-select", "some data");
}

function contentWndLoad(event) {
    var obsSvc = CC["@mozilla.org/observer-service;1"].
        getService(CI.nsIObserverService);
    var browser = getMostRecentBrowserWindow().getBrowser();

    browser.tabContainer.addEventListener("TabOpen", tabOpened, false);
    browser.tabContainer.addEventListener("TabClose", tabClosed, false);
    browser.tabContainer.addEventListener("TabSelect", tabSelected, false);
}

MyJsComponent.prototype = {

    /* . . . */

    observe: function(aSubject, aTopic, aData) {
        switch(aTopic) {
            case "xpcom-startup":
                var obsSvc = CC["@mozilla.org/observer-service;1"].
                    getService(CI.nsIObserverService);
                obsSvc.addObserver(this, "toplevel-window-ready", false);
                break;

            case "toplevel-window-ready":
                aSubject.addEventListener("load", contentWndLoad, false);
                break;
        }
    }

    /* . . . */
}

, . , , TabClose ... , .

+5

Source: https://habr.com/ru/post/1723195/