【问题标题】:Firefox Extension: Get page offset inside main Firefox windowFirefox 扩展:获取 Firefox 主窗口内的页面偏移量
【发布时间】:2014-02-17 04:01:42
【问题描述】:

是否可以从扩展内部获取 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...                        |
|                                                                |
.                                                                .
.                                                                .
.                                                                .

【问题讨论】:

    标签: firefox firefox-addon xpcom


    【解决方案1】:

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

    这个有一些额外的东西,比如导入的Services模块,你需要导入这个模块才能使用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;
                }
        }
    }
    

    【讨论】:

    • 它工作正常并且已经帮了很多忙,但是 gBrowser 的 contentDocument 似乎未定义,因为当我尝试使用 aDOMWindow.gBrowser.contentDocument 访问它时它会引发错误。
    • 它使用aHTMLDocument.body.getBoundingClientRect() 工作。非常感谢!
    • 啊,你应该先检查 aDOMWindow 是否有 gBrowser。没有选项卡的窗口有时没有 gBrowser(例如打开的弹出窗口)。所以在那种情况下到一个DOMWindow.document。这就是为什么你在我的 if 语句中看到 if (aDOMWindow.gBrowser &amp;&amp; aDOMWindow.gBrowser.tabContainer) {
    【解决方案2】:

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

    gBrowser.contentDocument.getBoundingClientRect().left

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

    【讨论】:

    • 嗯,这是一个新手问题,但我有一个引导扩展,我很难从 Startup() 方法中获取 gBrowser,所以我还没有尝试你的建议。
    • 要获取最新的 Firefox 窗口(不是 html dom 窗口),请执行以下操作:var aDOMWindow = Services.wm.getMostRecentWindow('navigator:browser') 并获取 gBrowser 转到 aDOMWindow.gBrowser 将在下面的完整帖子中发布一些详细信息
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-01-21
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2010-09-29
    相关资源
    最近更新 更多