簡體   English   中英

Firefox擴展:在Firefox主窗口內獲取頁面偏移

[英]Firefox Extension: Get page offset inside main Firefox window

是否可以從擴展程序內部獲取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...                        |
|                                                                |
.                                                                .
.                                                                .
.                                                                .

是的,運行此代碼來獲取偏移量,您是說頁面偏移量正確嗎?

gBrowser.contentDocument.getBoundingClientRect().left

您只需要訪問主頁的dom,就可以在其中進行任何操作

基本引導程序的示例: 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;
            }
    }
}

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM