Is it possible to get the offset of the page/window in the main Firefox window from inside an extension? To make things clear: It's not a DOM related question, I think it's an XPCOM question. I'll try to give an idea of what I need:

[Firefox]------------------------------------------------[ - # X ]
|    Tab1 Title    |   Tab2 Title    |                           |
------------------------------------------------------------------
| http://address.com/                                        | > |
------------------------------------------------------------------
* <- (X, Y) Offset of the page inside the main Firefox window    |
|                                                                |
|                                                                |
|                        Page contents...                        |
|                                                                |
.                                                                .
.                                                                .
.                                                                .
有帮助吗?

解决方案

Examples of basic bootstrap: https://gist.github.com/Noitidart/9025999

This one has some extra stuff like the Services module imported, you need to import this module to use Services.wm; But to import Services you need the component Cu. see the top of the gist here:

https://gist.github.com/Noitidart/9026493

Now Services.wm.getMostRecentWindow('navigator:browser') gets only one window. If you want to iterate through all browser windows copy paste this. I include how to iterate over each each HTML window in the tabs in each browser as well.

const {interfaces: Ci,  utils: Cu} = Components;
Cu.import('resource://gre/modules/Services.jsm');
//on this line can do Services.wm.getMostRecentBrowser('navigator:browser').gBrowser.contentDocument to get the currently focused tab document of the most recent browser window OR or continue to the loop below and it will do all windows and all tabs.

let XULWindows = Services.wm.getXULWindowEnumerator(null);
while (XULWindows.hasMoreElements()) {
    let aXULWindow = XULWindows.getNext();
    let aDOMWindow = aXULWindow.QueryInterface(Ci.nsIInterfaceRequestor).getInterface(Ci.nsIDOMWindowInternal || Ci.nsIDOMWindow);
    //aDOMWindow.gBrowser can be accesed here
    //to go through all tabs in the gBrowser do this:
    if (aDOMWindow.gBrowser && aDOMWindow.gBrowser.tabContainer) {
            var tabs = aDOMWindow.gBrowser.tabContainer.childNodes;
            for (var i = 0; i < tabs.length; i++) {
                Cu.reportError('DOING tab: ' + i);
                var tabBrowser = tabs[i].linkedBrowser;
                var aHTMLWindow = tabBrowser.contentWindow;
                var aHTMLDocument = aHTMLWindow.document;
            }
    }
}

其他提示

yes run this code to get the offset, you mean page offset right?

gBrowser.contentDocument.getBoundingClientRect().left

you just access the dom of the main page and you can do anything there

许可以下: CC-BY-SA归因
不隶属于 StackOverflow
scroll top