Firefox扩展:在主Firefox窗口中获取页面偏移量

时间:2014-02-17 04:01:43

标签: firefox firefox-addon xpcom

是否可以从扩展程序内获取Firefox主窗口中页面/窗口的偏移量?为了说清楚:这不是与DOM相关的问题,我认为这是一个XPCOM问题。我会试着想一下我需要什么:

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

2 个答案:

答案 0 :(得分:0)

是运行此代码来获取偏移量,你的意思是页面偏移权吗?

gBrowser.contentDocument.getBoundingClientRect().left

您只需访问主页面的dom,就可以在那里做任何事情

答案 1 :(得分:0)

基本引导程序示例: https://gist.github.com/Noitidart/9025999

这个有一些额外的东西,比如导入的服务模块,你需要导入这个模块才能使用Services.wm;但要导入服务,您需要组件Cu。在这里看到要点的顶部:

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

现在,Services.wm.getMostRecentWindow('navigator:browser')只获得一个窗口。如果要遍历所有浏览器窗口,请复制粘贴此内容。我还包括如何在每个浏览器的选项卡中迭代每个HTML窗口。

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;
            }
    }
}